核心代码:
private JDialog chooserColor;
private JColorChooser ch = new JColorChooser() ;
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
/*依赖开的父窗口,标题,有无模式,颜色选取框,点击确认后的事件,点击取消后的事件*//*参数*/
chooserColor = JColorChooser.createDialog(frame, "选取颜色", true, ch, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
textField.setForeground(ch.getColor());
}
}, null);
chooserColor.setVisible(true);
}
});
详细代码:
package Night;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.JTextField;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JDialog;
import javax.swing.SwingConstants;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class ChoosecolorDemo {
private JFrame frame;
private JTextField textField;
private JDialog chooserColor;
private JColorChooser ch = new JColorChooser() ;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ChoosecolorDemo window = new ChoosecolorDemo();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public ChoosecolorDemo() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 473, 339);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
frame.getContentPane().add(panel, BorderLayout.SOUTH);
panel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
JButton btnNewButton = new JButton("\u524D\u666F\u8272");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
/*依赖开的父窗口,标题,有无模式,颜色选取框,点击确认后的事件,点击取消后的事件*/
chooserColor = JColorChooser.createDialog(frame, "选取颜色", true, ch, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
textField.setForeground(ch.getColor());
}
}, null);
chooserColor.setVisible(true);
}
});
panel.add(btnNewButton);
JButton btnNewButton_1 = new JButton("\u80CC\u666F\u8272");
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
chooserColor = JColorChooser.createDialog(frame, "选取颜色", true, ch, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
textField.setBackground(ch.getColor());
}
}, null);
chooserColor.setVisible(true);
}
});
panel.add(btnNewButton_1);
JPanel panel_1 = new JPanel();
frame.getContentPane().add(panel_1, BorderLayout.CENTER);
panel_1.setLayout(new GridLayout(1, 0, 0, 0));
textField = new JTextField();
textField.setHorizontalAlignment(SwingConstants.CENTER);
textField.setText("\u4E2D\u56FD\u68A6");
panel_1.add(textField);
textField.setColumns(10);
}
}
效果一览: