【发布时间】:2015-08-20 12:40:46
【问题描述】:
我必须编写一个程序,其中 JColorChooser 被打开两次,这将允许用户选择背景和前景色。但是,如果用户选择的颜色过于相似,程序应该会抛出错误消息并再次打开 JColorChoosers...
我知道我必须使用 VetoableChangeListener 但我不知道如何在我的代码中实现它...
在我到目前为止的代码下方。
非常感谢您的帮助
package test;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class jcolortest{
private JFrame mainFrame;
private JLabel headerLabel;
private JLabel statusLabel;
private JPanel controlPanel;
public jcolortest(){
prepareGUI();
}
public static void main(String[] args){
jcolortest swingControlDemo = new jcolortest();
swingControlDemo.showColorChooserDemo();
}
private void prepareGUI(){
mainFrame = new JFrame("JColorChooser");
mainFrame.setSize(400,200);
mainFrame.setLayout(new GridLayout(3, 1));
mainFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent){
System.exit(0);
}
});
headerLabel = new JLabel("", JLabel.CENTER);
statusLabel = new JLabel("",JLabel.CENTER);
statusLabel.setSize(350,100);
controlPanel = new JPanel();
controlPanel.setLayout(new FlowLayout());
mainFrame.add(headerLabel);
mainFrame.add(controlPanel);
mainFrame.add(statusLabel);
mainFrame.setVisible(true);
}
private void showColorChooserDemo(){
headerLabel.setText("Test foreground text");
headerLabel.setForeground(Color.RED);
Color backgroundColor = JColorChooser.showDialog(mainFrame,
"Background Color", Color.white);
if(backgroundColor != null){
controlPanel.setBackground(backgroundColor);
mainFrame.getContentPane().setBackground(backgroundColor);
}
Color foregroundColor = JColorChooser.showDialog(mainFrame,
"Text Color", Color.white);
if(foregroundColor != Color.RED){
controlPanel.setForeground(foregroundColor);
headerLabel.setForeground(foregroundColor);
}
mainFrame.setVisible(true);
}
}
【问题讨论】:
标签: java swing jcolorchooser