Java by API/org.eclipse.swt.widgets

Материал из Java эксперт
Версия от 14:16, 31 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

FontDialog: open()

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.graphics.FontData;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FontDialog;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class MainClass {
  public static void main(String[] a) {
    final Display d = new Display();
    final Shell s = new Shell(d);
    s.setSize(300, 300);
    s.setText("A ColorDialog Example");
    s.setLayout(new FillLayout(SWT.VERTICAL));
    final Text t = new Text(s, SWT.BORDER | SWT.MULTI);
    final Button b = new Button(s, SWT.PUSH | SWT.BORDER);
    b.setText("Change Color");
    b.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent e) {
        FontDialog fd = new FontDialog(s, SWT.NONE);
        fd.setText("Select Font");
        fd.setRGB(new RGB(0,0,255));
        FontData defaultFont = new FontData("Courier",10,SWT.BOLD);
        fd.setFontData(defaultFont);     
        FontData newFont = fd.open();
        if(newFont==null)
            return;
        t.setFont(new Font(d, newFont));
        t.setForeground(new Color(d, fd.getRGB()));
        
      }
    });
    s.open();
    while (!s.isDisposed()) {
      if (!d.readAndDispatch())
        d.sleep();
    }
    d.dispose();
  }
}





FontDialog: setFontList(FontData[] fontData)

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 MainClass {
  public static void main(String[] a) {
    Display display = new Display();
    // Create the main window
    final Shell shell = new Shell(display);
    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) {
        // Create the color-change dialog
        FontDialog dlg = new FontDialog(shell);
        Font font = null;
        Color color = null;
        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();
        }
      }
    });
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}





FontDialog: setRGB(RGB rgb)

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 MainClass {
  public static void main(String[] a) {
    Display display = new Display();
    // Create the main window
    final Shell shell = new Shell(display);
    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) {
        // Create the color-change dialog
        FontDialog dlg = new FontDialog(shell);
        Font font = null;
        Color color = null;
        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();
        }
      }
    });
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}





FontDialog: setText(String text)

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.graphics.FontData;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FontDialog;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class MainClass {
  public static void main(String[] a) {
    final Display d = new Display();
    final Shell s = new Shell(d);
    s.setSize(300, 300);
    s.setText("A ColorDialog Example");
    s.setLayout(new FillLayout(SWT.VERTICAL));
    final Text t = new Text(s, SWT.BORDER | SWT.MULTI);
    final Button b = new Button(s, SWT.PUSH | SWT.BORDER);
    b.setText("Change Color");
    b.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent e) {
        FontDialog fd = new FontDialog(s, SWT.NONE);
        fd.setText("Select Font");
        fd.setRGB(new RGB(0,0,255));
        FontData defaultFont = new FontData("Courier",10,SWT.BOLD);
        fd.setFontData(defaultFont);     
        FontData newFont = fd.open();
        if(newFont==null)
            return;
        t.setFont(new Font(d, newFont));
        t.setForeground(new Color(d, fd.getRGB()));
        
      }
    });
    s.open();
    while (!s.isDisposed()) {
      if (!d.readAndDispatch())
        d.sleep();
    }
    d.dispose();
  }
}





new FontDialog(Shell parent, int style)

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.graphics.FontData;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FontDialog;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class MainClass {
  public static void main(String[] a) {
    final Display d = new Display();
    final Shell s = new Shell(d);
    s.setSize(300, 300);
    s.setText("A ColorDialog Example");
    s.setLayout(new FillLayout(SWT.VERTICAL));
    final Text t = new Text(s, SWT.BORDER | SWT.MULTI);
    final Button b = new Button(s, SWT.PUSH | SWT.BORDER);
    b.setText("Change Color");
    b.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent e) {
        FontDialog fd = new FontDialog(s, SWT.NONE);
        fd.setText("Select Font");
        fd.setRGB(new RGB(0,0,255));
        FontData defaultFont = new FontData("Courier",10,SWT.BOLD);
        fd.setFontData(defaultFont);     
        FontData newFont = fd.open();
        if(newFont==null)
            return;
        t.setFont(new Font(d, newFont));
        t.setForeground(new Color(d, fd.getRGB()));
        
      }
    });
    s.open();
    while (!s.isDisposed()) {
      if (!d.readAndDispatch())
        d.sleep();
    }
    d.dispose();
  }
}