Java Tutorial/SWT

Материал из Java эксперт
Перейти к: навигация, поиск

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:

  1. Font represents the onscreen font, and
  2. Font objects represent operating system resources, and you must dispose any you create.
  3. FontData represents the data used to construct the onscreen font.
  4. FontData objects contain only data, and aren"t operating system resources, so they aren"t disposed.



   <source lang="java">

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();
 }

}</source>



Caution: Don"t dispose a font while your application is still using it.


17. Construct a FontData object

   <source lang="java">

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();
 }

}</source>





Create Larger Font

   <source lang="java">

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();
 }

}</source>





17. To use the font selection dialog to change both the font and the color of a label

   <source lang="java">

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();
 }

}</source>





17. Using the FontRegistry

  1. FontData class is a lightweight class describing system fonts.
  2. A FontData to a Font is analogous to an ImageDescriptor to an Image.
  3. A FontData class describes the following properties of a Font:
  1. name
  2. height
  3. 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.



   <source lang="java">

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();
 }

}</source>