Java Swing JColorChooser

2018-01-09 19:23 更新

Java Swing教程 - Java Swing JColorChooser


方法...

以下代码使用JColorChooser选择一种颜色,并在标准输出上打印一条消息:

import java.awt.Color;
/*from ww w .  j  a  v a2s.co m*/
import javax.swing.JColorChooser;

public class Main {
  public static void main(String[] args) {
    // Display a color chooser dialog
    Color color = JColorChooser.showDialog(null, "Select  a  color", null);

    // Check if user selected a color
    if (color == null) {
      System.out.println("we cancelled or  closed the   color chooser");
    } else {
      System.out.println("we selected  color:  " + color);
    }

  }
}


例子

下面的代码显示了如何显示颜色选择器对话框并使用所选颜色设置按钮背景。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/*from w  ww . jav  a 2s .co  m*/
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JFrame;

public class Main {
  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);
  }
}   


实施例2

下面的代码显示了如何听彩色选择变化事件。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
/*from w  ww . ja va2  s  . co  m*/
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 Main {

  public static void main(String args[]) {
    JFrame frame = new JFrame("JColorChooser Popup");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    final JLabel label = new JLabel("www.www.w3cschool.cn", JLabel.CENTER);
    label.setFont(new Font("Serif", Font.BOLD | Font.ITALIC, 48));

    frame.add(label, BorderLayout.SOUTH);

    final JColorChooser colorChooser = new JColorChooser(label.getBackground());

    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对话框。

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
/*w  w  w .j  a  v  a  2  s .  co  m*/
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.colorchooser.AbstractColorChooserPanel;

public class Main {
  public static void main(String[] argv) {
    JColorChooser chooser = new JColorChooser();
    chooser.addChooserPanel(new MyChooserPanel());
  }
}

class MyChooserPanel extends AbstractColorChooserPanel {
  public void buildChooser() {
    setLayout(new GridLayout(0, 3));
    makeAddButton("Red", Color.red);
    makeAddButton("Green", Color.green);
    makeAddButton("Blue", Color.blue);
  }

  public void updateChooser() {
  }

  public String getDisplayName() {
    return "MyChooserPanel";
  }

  public Icon getSmallDisplayIcon() {
    return null;
  }
  public Icon getLargeDisplayIcon() {
    return null;
  }
  private void makeAddButton(String name, Color color) {
    JButton button = new JButton(name);
    button.setBackground(color);
    button.setAction(setColorAction);
    add(button);
  }

  Action setColorAction = new AbstractAction() {
    public void actionPerformed(ActionEvent evt) {
      JButton button = (JButton) evt.getSource();

      getColorSelectionModel().setSelectedColor(button.getBackground());
    }
  };
}
以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号