下面列出了怎么用javax.swing.JColorChooser的API类实例代码及写法,或者点击链接到github查看源代码。
public void actionPerformed(ActionEvent event) {
String command = event.getActionCommand();
if (CMD_DIALOG.equals(command)) {
JDialog dialog = new JDialog(this.frame, "Dialog"); // NON-NLS: dialog title
dialog.setLocation(200, 0);
show(dialog, CMD_CHOOSER);
}
else if (CMD_CHOOSER.equals(command)) {
Object source = event.getSource();
Component component = (source instanceof Component)
? (Component) source
: null;
JColorChooser.showDialog(component, "ColorChooser", Color.BLUE); // NON-NLS: title
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
JColorChooser colorChooser = new JColorChooser();
colorChooser.setDragEnabled(true);
panel.setBorder(BorderFactory.createTitledBorder("JColorChoosers"));
panel.add(colorChooser);
frame.setContentPane(panel);
frame.pack();
frame.setVisible(true);
}
});
}
private static JButton createColorButton(Color color) {
JButton colorButton = new JButton();
colorButton.setPreferredSize(new Dimension(30, 20));
colorButton.setBorder(BorderFactory.createLineBorder(Color.darkGray));
colorButton.setBackground(color);
colorButton.setForeground(color);
colorButton.setUI(new MetalButtonUI());
//colorButton.setActionCommand("" + i);
colorButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//int i = Integer.parseInt(e.getActionCommand());
Color newColor = JColorChooser.showDialog(Gui.frame, "Choose Color", ((JButton) e.getSource()).getBackground());
if (newColor != null) {
((JButton) e.getSource()).setBackground(newColor);
((JButton) e.getSource()).setForeground(newColor);
}
}
});
return colorButton;
}
/**
*
*/
public void actionPerformed(ActionEvent e)
{
if (e.getSource() instanceof mxGraphComponent)
{
mxGraphComponent graphComponent = (mxGraphComponent) e
.getSource();
Color newColor = JColorChooser.showDialog(graphComponent,
mxResources.get("background"), null);
if (newColor != null)
{
graphComponent.getViewport().setOpaque(true);
graphComponent.getViewport().setBackground(newColor);
}
// Forces a repaint of the outline
graphComponent.getGraph().repaint();
}
}
public void actionPerformed(ActionEvent event) {
String command = event.getActionCommand();
if (CMD_DIALOG.equals(command)) {
JDialog dialog = new JDialog(this.frame, "Dialog"); // NON-NLS: dialog title
dialog.setLocation(200, 0);
show(dialog, CMD_CHOOSER);
}
else if (CMD_CHOOSER.equals(command)) {
Object source = event.getSource();
Component component = (source instanceof Component)
? (Component) source
: null;
JColorChooser.showDialog(component, "ColorChooser", Color.BLUE); // NON-NLS: title
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
JColorChooser colorChooser = new JColorChooser();
colorChooser.setDragEnabled(true);
panel.setBorder(BorderFactory.createTitledBorder("JColorChoosers"));
panel.add(colorChooser);
frame.setContentPane(panel);
frame.pack();
frame.setVisible(true);
}
});
}
public static void main(String[] args) throws Exception {
JColorChooser chooser = new JColorChooser();
AbstractColorChooserPanel[] panels = chooser.getChooserPanels();
chooser.setChooserPanels(new AbstractColorChooserPanel[] { panels[1] });
JDialog dialog = show(chooser);
pause(DELAY);
dialog.dispose();
pause(DELAY);
Test4177735 test = new Test4177735();
SwingUtilities.invokeAndWait(test);
if (test.count != 0) {
throw new Error("JColorChooser leaves " + test.count + " threads running");
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
JColorChooser colorChooser = new JColorChooser();
colorChooser.setDragEnabled(true);
panel.setBorder(BorderFactory.createTitledBorder("JColorChoosers"));
panel.add(colorChooser);
frame.setContentPane(panel);
frame.pack();
frame.setVisible(true);
}
});
}
private static void test(Color[] colors) {
JLabel label = new JLabel("Preview Panel"); // NON-NLS: simple label
JColorChooser chooser = new JColorChooser();
chooser.setPreviewPanel(label);
float[] hsb = new float[3];
for (int i = 0; i < colors.length; i++) {
Color color = colors[i];
// Make sure sure that there wasn't a regression
// in java.awt.Color and the conversion methods
Color.RGBtoHSB(color.getRed(), color.getGreen(), color.getBlue(), hsb);
if (!color.equals(Color.getHSBColor(hsb[0], hsb[1], hsb[2]))) {
throw new Error("color conversion is failed");
}
// 4193384 regression test
if (!color.equals(new JColorChooser(color).getColor())) {
throw new Error("constructor sets incorrect initial color");
}
// 4200976 regression test
chooser.setColor(color);
if (!color.equals(label.getForeground())) {
throw new Error("a custom preview panel doesn't handle colors");
}
}
}
private static void test(Color[] colors) {
JLabel label = new JLabel("Preview Panel"); // NON-NLS: simple label
JColorChooser chooser = new JColorChooser();
chooser.setPreviewPanel(label);
float[] hsb = new float[3];
for (int i = 0; i < colors.length; i++) {
Color color = colors[i];
// Make sure sure that there wasn't a regression
// in java.awt.Color and the conversion methods
Color.RGBtoHSB(color.getRed(), color.getGreen(), color.getBlue(), hsb);
if (!color.equals(Color.getHSBColor(hsb[0], hsb[1], hsb[2]))) {
throw new Error("color conversion is failed");
}
// 4193384 regression test
if (!color.equals(new JColorChooser(color).getColor())) {
throw new Error("constructor sets incorrect initial color");
}
// 4200976 regression test
chooser.setColor(color);
if (!color.equals(label.getForeground())) {
throw new Error("a custom preview panel doesn't handle colors");
}
}
}
public ColorEditor() {
// Set up the editor (from the table's point of view),
// which is a button.
// This button brings up the color chooser dialog,
// which is the editor from the user's point of view.
button = new JButton();
button.setActionCommand(EDIT);
button.addActionListener(this);
button.setBorderPainted(false);
// Set up the dialog that the button brings up.
colorChooser = new JColorChooser();
dialog = JColorChooser.createDialog(button, "Pick a Color", true, // modal
colorChooser, this, // OK button handler
null); // no CANCEL button handler
}
public void actionPerformed(ActionEvent event) {
String command = event.getActionCommand();
if (CMD_DIALOG.equals(command)) {
JDialog dialog = new JDialog(this.frame, "Dialog"); // NON-NLS: dialog title
dialog.setLocation(200, 0);
show(dialog, CMD_CHOOSER);
}
else if (CMD_CHOOSER.equals(command)) {
Object source = event.getSource();
Component component = (source instanceof Component)
? (Component) source
: null;
JColorChooser.showDialog(component, "ColorChooser", Color.BLUE); // NON-NLS: title
}
}
public void actionPerformed(ActionEvent event) {
String command = event.getActionCommand();
if (CMD_DIALOG.equals(command)) {
JDialog dialog = new JDialog(this.frame, "Dialog"); // NON-NLS: dialog title
dialog.setLocation(200, 0);
show(dialog, CMD_CHOOSER);
}
else if (CMD_CHOOSER.equals(command)) {
Object source = event.getSource();
Component component = (source instanceof Component)
? (Component) source
: null;
JColorChooser.showDialog(component, "ColorChooser", Color.BLUE); // NON-NLS: title
}
}
/**
* Allow the user the opportunity to select a Paint object. For now, we
* just use the standard color chooser - all colors are Paint objects, but
* not all Paint objects are colors (later we can implement a more general
* Paint chooser).
*/
public void attemptPaintSelection() {
Paint p = this.titlePaint.getPaint();
Color defaultColor = (p instanceof Color ? (Color) p : Color.blue);
Color c = JColorChooser.showDialog(
this, localizationResources.getString("Title_Color"), defaultColor
);
if (c != null) {
this.titlePaint.setPaint(c);
}
}
static JDialog show(JColorChooser chooser) {
JDialog dialog = JColorChooser.createDialog(null, null, false, chooser, null, null);
dialog.setVisible(true);
// block till displayed
Point point = null;
while (point == null) {
try {
point = dialog.getLocationOnScreen();
}
catch (IllegalStateException exception) {
pause(DELAY);
}
}
return dialog;
}
/**
* Handle a grid paint selection.
*/
protected void attemptGridPaintSelection() {
Color c;
c = JColorChooser.showDialog(this, localizationResources.getString(
"Grid_Color"), Color.blue);
if (c != null) {
this.gridPaintSample.setPaint(c);
}
}
/**
* Allow the user to change the background paint.
*/
private void attemptBackgroundPaintSelection() {
Color c;
c = JColorChooser.showDialog(this, localizationResources.getString(
"Background_Color"), Color.blue);
if (c != null) {
this.backgroundPaintSample.setPaint(c);
}
}
public static void main(String[] args) {
JColorChooser chooser = new JColorChooser(COLOR);
JDialog dialog = Test4177735.show(chooser);
PropertyChangeListener listener = new Test4234761();
chooser.addPropertyChangeListener("color", listener); // NON-NLS: property name
JTabbedPane tabbedPane = (JTabbedPane) chooser.getComponent(0);
tabbedPane.setSelectedIndex(1); // HSB tab index
if (!chooser.getColor().equals(COLOR)) {
listener.propertyChange(null);
}
dialog.dispose();
}
protected void selectColor() {
ResourceManager rm = ResourceManager.all(FilePropertyEditor.class);
String title = rm.getString("ColorPropertyEditor.title");
Color selectedColor = JColorChooser.showDialog(editor, title, color);
if (selectedColor != null) {
Color oldColor = color;
Color newColor = selectedColor;
label.setValue(newColor);
color = newColor;
firePropertyChange(oldColor, newColor);
}
}
public static void main(String[] args) {
JColorChooser chooser = new JColorChooser();
chooser.setColor(new Color(new Random().nextInt()));
Color before = chooser.getColor();
Color after = copy(chooser).getColor();
if (!after.equals(before)) {
throw new Error("color is changed after serialization");
}
}
public static void main(String[] args) {
JColorChooser chooser = new JColorChooser(COLOR);
JDialog dialog = Test4177735.show(chooser);
PropertyChangeListener listener = new Test4234761();
chooser.addPropertyChangeListener("color", listener); // NON-NLS: property name
JTabbedPane tabbedPane = (JTabbedPane) chooser.getComponent(0);
tabbedPane.setSelectedIndex(1); // HSB tab index
if (!chooser.getColor().equals(COLOR)) {
listener.propertyChange(null);
}
dialog.dispose();
}
protected Color showMoreColorsDialog() {
JColorChooser colorChooser = new JColorChooser(getSelectedColor());
AbstractColorChooserPanel[] oldChooserPanels = colorChooser.getChooserPanels();
AbstractColorChooserPanel[] newChooserPanels = new AbstractColorChooserPanel[oldChooserPanels.length + 1];
System.arraycopy(oldChooserPanels, 0, newChooserPanels, 1, oldChooserPanels.length);
newChooserPanels[0] = new MyAbstractColorChooserPanel();
colorChooser.setChooserPanels(newChooserPanels);
ColorTracker colorTracker = new ColorTracker(colorChooser);
JDialog dialog = JColorChooser.createDialog(this, "Select Colour", true, colorChooser, colorTracker, null);
dialog.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
dialog.setVisible(true);
return colorTracker.getColor();
}
/**
* Allow the user to change the background paint.
*/
private void attemptBackgroundPaintSelection() {
Color c;
c = JColorChooser.showDialog(this, localizationResources.getString(
"Background_Color"), Color.blue);
if (c != null) {
this.backgroundPaintSample.setPaint(c);
}
}
@Override
public void init() {
JButton button = new JButton("Swap models");
button.addActionListener(this);
this.chooser = new JColorChooser(Color.RED);
this.chooser.setSelectionModel(WHITE);
add(BorderLayout.NORTH, button);
add(BorderLayout.CENTER, this.chooser);
}
public synchronized void run() {
if (this.chooser == null) {
this.chooser = new JColorChooser();
JFrame frame = new JFrame(getClass().getName());
frame.add(this.chooser);
frame.setVisible(true);
}
else if (this.updated) {
if (isShowing(this.chooser.getPreviewPanel())) {
exit("custom preview panel is showing");
}
exit(null);
}
else {
Component component = this.chooser.getPreviewPanel();
if (component == null) {
component = getPreview(this.chooser);
}
if (!isShowing(component)) {
exit("default preview panel is not showing");
}
this.updated = true;
this.chooser.setPreviewPanel(new JPanel());
}
LookAndFeelInfo[] infos = UIManager.getInstalledLookAndFeels();
LookAndFeelInfo info = infos[++this.index % infos.length];
try {
UIManager.setLookAndFeel(info.getClassName());
}
catch (Exception exception) {
exit("could not change L&F");
}
SwingUtilities.updateComponentTreeUI(this.chooser);
SwingUtilities.invokeLater(this);
}
/**
* Allow the user the opportunity to select a Paint object. For now, we
* just use the standard color chooser - all colors are Paint objects, but
* not all Paint objects are colors (later we can implement a more general
* Paint chooser).
*/
public void attemptPaintSelection() {
Paint p = this.titlePaint.getPaint();
Color defaultColor = (p instanceof Color ? (Color) p : Color.blue);
Color c = JColorChooser.showDialog(
this, localizationResources.getString("Title_Color"), defaultColor
);
if (c != null) {
this.titlePaint.setPaint(c);
}
}
private void jLabel_ColorMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel_ColorMouseClicked
// TODO add your handling code here:
Color color = JColorChooser.showDialog(this, null, _color);
if (color != null) {
_color = color;
updateLabelSet();
updateLabelsFontColor();
_mapView.paintLayers();
}
}
/**
* Allow the user the opportunity to select a Paint object. For now, we
* just use the standard color chooser - all colors are Paint objects, but
* not all Paint objects are colors (later we can implement a more general
* Paint chooser).
*/
public void attemptPaintSelection() {
Paint p = this.titlePaint.getPaint();
Color defaultColor = (p instanceof Color ? (Color) p : Color.blue);
Color c = JColorChooser.showDialog(
this, localizationResources.getString("Title_Color"), defaultColor
);
if (c != null) {
this.titlePaint.setPaint(c);
}
}
public synchronized void run() {
if (this.chooser == null) {
this.chooser = new JColorChooser();
JFrame frame = new JFrame(getClass().getName());
frame.add(this.chooser);
frame.setVisible(true);
}
else if (this.updated) {
if (isShowing(this.chooser.getPreviewPanel())) {
exit("custom preview panel is showing");
}
exit(null);
}
else {
Component component = this.chooser.getPreviewPanel();
if (component == null) {
component = getPreview(this.chooser);
}
if (!isShowing(component)) {
exit("default preview panel is not showing");
}
this.updated = true;
this.chooser.setPreviewPanel(new JPanel());
}
LookAndFeelInfo[] infos = UIManager.getInstalledLookAndFeels();
LookAndFeelInfo info = infos[++this.index % infos.length];
try {
UIManager.setLookAndFeel(info.getClassName());
}
catch (Exception exception) {
exit("could not change L&F");
}
SwingUtilities.updateComponentTreeUI(this.chooser);
SwingUtilities.invokeLater(this);
}
/**
* Allows the user the opportunity to select a new background paint. Uses
* JColorChooser, so we are only allowing a subset of all Paint objects to
* be selected (fix later).
*/
private void attemptModifyBackgroundPaint() {
Color c;
c = JColorChooser.showDialog(this, localizationResources.getString(
"Background_Color"), Color.blue);
if (c != null) {
this.background.setPaint(c);
}
}