Java by API/javax.swing/JColorChooser — различия между версиями

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

Текущая версия на 14:19, 31 мая 2010

JColorChooser: addChooserPanel(AbstractColorChooserPanel p)

import java.awt.Color;
import java.awt.ruponent;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.Icon;
import javax.swing.JColorChooser;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.colorchooser.AbstractColorChooserPanel;
import javax.swing.colorchooser.ColorSelectionModel;
public class MainClass {
  public static void main(String[] a) {
    final JColorChooser colorChooser = new JColorChooser();
    SystemColorChooserPanel newChooser = new SystemColorChooserPanel();
    colorChooser.addChooserPanel(newChooser);
    ActionListener okActionListener = new ActionListener() {
      public void actionPerformed(ActionEvent actionEvent) {
        System.out.println(colorChooser.getColor());
      }
    };
    ActionListener cancelActionListener = new ActionListener() {
      public void actionPerformed(ActionEvent actionEvent) {
        System.out.println("Cancel");
      }
    };
    final JDialog dialog = JColorChooser.createDialog(null, "Change Button Background", true,
        colorChooser, okActionListener, cancelActionListener);
    dialog.setVisible(true);
  }
}
class SystemColorChooserPanel extends AbstractColorChooserPanel implements ItemListener {
  JComboBox comboBox;
  String labels[] = { "BLACK", "BLUE", "CYAN"};
  Color colors[] = { Color.BLACK, Color.BLUE, Color.CYAN};
  private void setColor(Color newColor) {
    int position = findColorPosition(newColor);
    comboBox.setSelectedIndex(position);
  }
  private int findColorLabel(Object label) {
    String stringLabel = label.toString();
    int position = -1;
    for (int i = 0, n = labels.length; i < n; i++) {
      if (stringLabel.equals(labels[i])) {
        position = i;
        break;
      }
    }
    return position;
  }
  private int findColorPosition(Color color) {
    int position = colors.length - 1;
    int colorRGB = color.getRGB();
    for (int i = 0, n = colors.length; i < n; i++) {
      if ((colors[i] != null) && (colorRGB == colors[i].getRGB())) {
        position = i;
        break;
      }
    }
    return position;
  }
  public void itemStateChanged(ItemEvent itemEvent) {
    int state = itemEvent.getStateChange();
    if (state == ItemEvent.SELECTED) {
      int position = findColorLabel(itemEvent.getItem());
      if ((position != -1) && (position != labels.length - 1)) {
        ColorSelectionModel selectionModel = getColorSelectionModel();
        selectionModel.setSelectedColor(colors[position]);
      }
    }
  }
  public String getDisplayName() {
    return "SystemColor";
  }
  public Icon getSmallDisplayIcon() {
    return new MyIcon(Color.BLUE);
  }
  public Icon getLargeDisplayIcon() {
    return new MyIcon(Color.GREEN);
  }
  protected void buildChooser() {
    comboBox = new JComboBox(labels);
    comboBox.addItemListener(this);
    add(comboBox);
  }
  public void updateChooser() {
    Color color = getColorFromModel();
    setColor(color);
  }
}
class MyIcon implements Icon {
  Color myColor;
  public MyIcon(Color myColor) {
    this.myColor = myColor;
  }
  public int getIconWidth() {
    return 16;
  }
  public int getIconHeight() {
    return 16;
  }
  public void paintIcon(Component c, Graphics g, int x, int y) {
    g.setColor(myColor);
    g.drawRect(0, 0, 16, 16);
  }
}





JColorChooser: createDialog(Component c, String t, boolean m, JColorChooser c, ActionListener ok, ActionListener cancel)

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JColorChooser;
import javax.swing.JDialog;
import javax.swing.JLabel;
public class MainClass {
  public static void main(String[] a) {
    final JColorChooser colorChooser = new JColorChooser();
    final JLabel previewLabel = new JLabel("I Love Swing", JLabel.CENTER);
    previewLabel.setFont(new Font("Serif", Font.BOLD | Font.ITALIC, 48));
    previewLabel.setSize(previewLabel.getPreferredSize());
    previewLabel.setBorder(BorderFactory.createEmptyBorder(0, 0, 1, 0));
    colorChooser.setPreviewPanel(previewLabel);
    ActionListener okActionListener = new ActionListener() {
      public void actionPerformed(ActionEvent actionEvent) {
        System.out.println("OK Button");
        System.out.println(colorChooser.getColor());
      }
    };
    ActionListener cancelActionListener = new ActionListener() {
      public void actionPerformed(ActionEvent actionEvent) {
        System.out.println("Cancel Button");
      }
    };
    final JDialog dialog = JColorChooser.createDialog(null, "Change Button Background", true,
        colorChooser, okActionListener, cancelActionListener);
    dialog.setVisible(true);
  }
}





JColorChooser: getSelectionModel()

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import javax.swing.BorderFactory;
import javax.swing.JColorChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.colorchooser.ColorSelectionModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class MainClass {
  public static void main(String[] a) {
    JFrame frame = new JFrame("JColorChooser Popup");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    final JLabel label = new JLabel("www.jexp.ru", JLabel.CENTER);
    label.setFont(new Font("Serif", Font.BOLD | Font.ITALIC, 48));
    frame.add(label, BorderLayout.SOUTH);
    final JColorChooser colorChooser = new JColorChooser(label.getBackground());
    colorChooser.setBorder(BorderFactory.createTitledBorder("Pick Color for jexp.ru"));
    ColorSelectionModel model = colorChooser.getSelectionModel();
    ChangeListener changeListener = new ChangeListener() {
      public void stateChanged(ChangeEvent changeEvent) {
        Color newForegroundColor = colorChooser.getColor();
        label.setForeground(newForegroundColor);
      }
    };
    model.addChangeListener(changeListener);
    frame.add(colorChooser, BorderLayout.CENTER);
    frame.pack();
    frame.setVisible(true);
  }
}





JColorChooser: setDragEnabled(boolean b)

import java.awt.BorderLayout;
import javax.swing.JColorChooser;
import javax.swing.JFrame;
public class MainClass {
  public static void main(final String args[]) {
    JFrame frame = new JFrame("Double Color Choosers");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JColorChooser left = new JColorChooser();
    left.setDragEnabled(true);
    frame.add(left, BorderLayout.WEST);
    JColorChooser right = new JColorChooser();
    right.setDragEnabled(true);
    frame.add(right, BorderLayout.EAST);
    frame.pack();
    frame.setVisible(true);
  }
}





JColorChooser: setPreviewPanel(JComponent preview)

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JColorChooser;
import javax.swing.JDialog;
import javax.swing.JLabel;
public class MainClass {
  public static void main(String[] a) {
    final JColorChooser colorChooser = new JColorChooser();
    final JLabel previewLabel = new JLabel("I Love Swing", JLabel.CENTER);
    previewLabel.setFont(new Font("Serif", Font.BOLD | Font.ITALIC, 48));
    previewLabel.setSize(previewLabel.getPreferredSize());
    previewLabel.setBorder(BorderFactory.createEmptyBorder(0, 0, 1, 0));
    colorChooser.setPreviewPanel(previewLabel);
    ActionListener okActionListener = new ActionListener() {
      public void actionPerformed(ActionEvent actionEvent) {
        System.out.println("OK Button");
        System.out.println(colorChooser.getColor());
      }
    };
    ActionListener cancelActionListener = new ActionListener() {
      public void actionPerformed(ActionEvent actionEvent) {
        System.out.println("Cancel Button");
      }
    };
    final JDialog dialog = JColorChooser.createDialog(null, "Change Button Background", true,
        colorChooser, okActionListener, cancelActionListener);
    dialog.setVisible(true);
  }
}





JColorChooser: showDialog(Component component, String title, Color initialColor)

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JFrame;
public class MainClass {
  public static void main(String args[]) {
    JFrame f = new JFrame("JColorChooser Sample");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    final JButton button = new JButton("Pick to Change Background");
    ActionListener actionListener = new ActionListener() {
      public void actionPerformed(ActionEvent actionEvent) {
        Color initialBackground = button.getBackground();
        Color background = JColorChooser.showDialog(null,
            "JColorChooser Sample", initialBackground);
        if (background != null) {
          button.setBackground(background);
        }
      }
    };
    button.addActionListener(actionListener);
    f.add(button, BorderLayout.CENTER);
    f.setSize(300, 200);
    f.setVisible(true);
  }
}