Java Tutorial/SWT — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 15:22, 31 мая 2010
- ApplicationWindow
- Browser
- BusyIndicator
- Button
- Button Event
- CLabel
- CTabFolder
- CTabItem
- Canvas
- Caret
- Clipboard
- ColorDialog
- Combo
- Combo Event
- CommonDialog
- Composite
- ControlEditor
- CoolBar
- CoolItem
- Custom Layout
- DateTime
- Decorations
- Device
- Dialog
- DirectoryDialog
- Display
- ExpandBar
- File Tree
- FileDialog
- FillLayout
- FocusEvent
- FormLayout
- GridData
- GridLayout
- Group
- ImageRegistry
- JFace Introduction
- KeyEvent
- Label
- Layout Basics
- Link
- List
- List Event
- Menu
- MenuEvent
- MenuItem
- MenuItem Event
- MessageBox
- MouseEvent
- PasswordField
- PopupList
- PopupMenu
- PrintDialog
- PrinterData
- Program
- ProgressBar
- RowLayout
- SWT AWT Swing
- SWT Basics
- SWT Color
- SWT Cursor
- SWT Drag Drop
- SWT Event
- SWT Image
- SWT NO Layout
- SWT Thread
- SWT Timer
- Sash
- Sash Event
- SashForm
- Scale
- Screen Capture
- ScrollBar
- ScrollBar Event
- ScrolledComposite
- Separator
- Shell
- Shell Event
- Slider
- Slider Event
- Spinner
- Spinner Event
- Splash Screen
- StackLayout
- StyledText
- StyledText Action
- StyledText Event
- StyledText Format
- StyledText LineStyle
- StyledText Style
- System Tray
- TabFolder
- TabItem
- TabSequence
- Table
- Table Cursor
- Table Editor
- Table Event
- Table Renderer
- Table Sort
- TableColumn
- TableItem
- Text
- Text Event
- TextLayout
- ToolBar
- ToolItem
- ToolTip
- Tooltip Balloon
- Tracker
- Tree
- Tree Editor
- Tree Event
- TreeColumn TreeTable
- TreeItem
- TreeViewer
- UI Auto
- ViewForm
- WIN32
- Widget
- WindowManagers
Содержание
17. Choosing a Font
SWT provides the FontDialog class to display the common font selection dialog. FontDialog"s open() method returns a FontData object (or null if the user cancels the dialog), which you can use to create a Font.
SWT uses two classes to represent fonts:
- Font represents the onscreen font, and
- Font objects represent operating system resources, and you must dispose any you create.
- FontData represents the data used to construct the onscreen font.
- FontData objects contain only data, and aren"t operating system resources, so they aren"t disposed.
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FontDialog;
import org.eclipse.swt.widgets.Shell;
public class FontSelectionDialogDisplay {
public static void main(String[] args) {
Display display = new Display();
final Shell shell = new Shell(display);
FontDialog dlg = new FontDialog(shell);
FontData fontData = dlg.open();
if (fontData != null) {
Font font = new Font(shell.getDisplay(), fontData);
font.dispose();
}
display.dispose();
}
}
Caution: Don"t dispose a font while your application is still using it.
17. Construct a FontData object
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class FontDataConstruct {
public static void main(String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setText("Canvas Example");
shell.setLayout(new FillLayout());
Canvas canvas = new Canvas(shell, SWT.NONE);
canvas.addPaintListener(new PaintListener() {
public void paintControl(PaintEvent e) {
Font font = new Font(shell.getDisplay(), new FontData("Helvetica", 18, SWT.NORMAL));
e.gc.setFont(font);
e.gc.drawText("My Text", 0, 0);
font.dispose();
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
}
Create Larger Font
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class FontLargerCreate {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
Text text = new Text(shell, SWT.MULTI|SWT.BORDER);
text.setBounds(10, 10, 150, 150);
Font initialFont = text.getFont();
FontData[] fontData = initialFont.getFontData();
for (int i = 0; i < fontData.length; i++) {
fontData[i].setHeight(24);
}
Font newFont = new Font(display, fontData);
text.setFont(newFont);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
newFont.dispose();
display.dispose();
}
}
17. To use the font selection dialog to change both the font and the color of a label
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FontDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
public class FontDialogColorFontData {
public static void main(String[] args) {
Display display = new Display();
final Shell shell = new Shell(display);
shell.setText("Font Chooser");
shell.setLayout(new GridLayout(2, false));
final Label fontLabel = new Label(shell, SWT.NONE);
fontLabel.setText("The selected font");
Button button = new Button(shell, SWT.PUSH);
button.setText("Font...");
button.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
Font font = null;
Color color = null;
FontDialog dlg = new FontDialog(shell);
if (font != null)
dlg.setFontList(fontLabel.getFont().getFontData());
if (color != null)
dlg.setRGB(color.getRGB());
if (dlg.open() != null) {
if (font != null)
font.dispose();
if (color != null)
color.dispose();
font = new Font(shell.getDisplay(), dlg.getFontList());
fontLabel.setFont(font);
color = new Color(shell.getDisplay(), dlg.getRGB());
fontLabel.setForeground(color);
shell.pack();
if (font != null)
font.dispose();
if (color != null)
color.dispose();
}
}
});
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
}
17. Using the FontRegistry
- FontData class is a lightweight class describing system fonts.
- A FontData to a Font is analogous to an ImageDescriptor to an Image.
- A FontData class describes the following properties of a Font:
- name
- height
- style: combination of SWT.NORMAL, SWT.ITALIC, and SWT.BOLD
The Font itself is resource-based, i.e., if you create it, you must dispose of it. You can use FontRegistry to manage allocation and disposal of the fonts.
import org.eclipse.jface.resource.FontRegistry;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class FontRegistry {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell();
shell.setLayout(new GridLayout(1, false));
FontRegistry fontRegistry = new FontRegistry(display);
fontRegistry.put("button-text", new FontData[] { new FontData("Arial", 9, SWT.BOLD) });
fontRegistry.put("code", new FontData[] { new FontData("Courier New", 10, SWT.NORMAL) });
Text text = new Text(shell, SWT.MULTI | SWT.BORDER | SWT.WRAP);
text.setFont(fontRegistry.get("code"));
text.setForeground(display.getSystemColor(SWT.COLOR_BLUE));
text.setText("");
GridData gd = new GridData(GridData.FILL_BOTH);
gd.horizontalSpan = 2;
text.setLayoutData(gd);
Button executeButton = new Button(shell, SWT.PUSH);
executeButton.setText("Execute");
executeButton.setFont(fontRegistry.get("button-text"));
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
}