Java Tutorial/SWT/ColorDialog — различия между версиями

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

Версия 17:44, 31 мая 2010

17. Choosing a Color

  1. SWT provides a class called RGB that stores RGB values.
  2. RGB class holds the data that can represent a color.
  3. SWT provides a class called Color that represents an actual color.



import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.widgets.Display;
public class ColorCreate {
  public static void main(String[] args) {
    Display display = new Display();
    RGB rgb = new RGB(255, 0, 0); // pure red
    Color color = new Color(display, rgb);
    System.out.println(color.getBlue());
    display.dispose();
  }
}





17. Customizing the Color Selection Dialog

import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.widgets.ColorDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class ColorDialogSetInitColor {
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    ColorDialog dlg = new ColorDialog(shell);
    dlg.setRGB(new RGB(0, 0, 255));
    RGB rgb = dlg.open();
    if (rgb != null) {
      Color color = new Color(shell.getDisplay(), rgb);
      System.out.println(color.getRGB());
      
      color.dispose();
    }
    display.dispose();
  }
}





17. Get Selected Color from ColorDialog

import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.widgets.ColorDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class ColorDialogCreateGetColor {
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    ColorDialog dlg = new ColorDialog(shell);
    RGB rgb = dlg.open();
    if (rgb != null) {
      Color color = new Color(shell.getDisplay(), rgb);
      
      System.out.println(color.getRGB());
      
      color.dispose();
    }
    display.dispose();
  }
}





17. Set Label Background to a color selected in ColorDialog

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.RGB;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.ColorDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
public class ColorDialogButtonActionSetLabelBackground {
  public static void main(String[] args) {
    Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("Color Chooser");
    shell.setLayout(new GridLayout(2, false));
    final Label colorLabel = new Label(shell, SWT.NONE);
    colorLabel.setText("Color");
    Button button = new Button(shell, SWT.PUSH);
    button.setText("Color...");
    button.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent event) {
        Color color = new Color(shell.getDisplay(), new RGB(0, 255, 0));
        ColorDialog dlg = new ColorDialog(shell);
        dlg.setRGB(colorLabel.getBackground().getRGB());
        dlg.setText("Choose a Color");
        RGB rgb = dlg.open();
        if (rgb != null) {
          color.dispose();
          color = new Color(shell.getDisplay(), rgb);
          colorLabel.setBackground(color);
          color.dispose();
        }
      }
    });
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}