【发布时间】:2016-03-03 05:49:24
【问题描述】:
我有一个简单的应用程序,它由一个 JButton 和一个 JColorChooser 组成。 当我按下按钮 2 JColorChoosers 出现。一个允许我选择背景颜色,第二个允许我选择前景色。 我将每种颜色保存在一个单独的变量中。这是我的代码:
public class Slide extends JFrame{
Color bgColor;
JButton colorButton=new JButton();
JColorChooser colorPicker=new JColorChooser();
public Slide(){
JPanel panel=new JPanel();
panel.setLayout(new MigLayout("", "[][][][][]", "[][][][][][]"));
this.setContentPane(panel);
colorButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
//1st color chooser for background color
bgColor=JColorChooser.showDialog(null, "Background color", null);
//2nd colorchooser appears for foreground
Color fgColor=JColorChooser.showDialog(null, "Foreground color", null);
colorButton.setBackground(bgColor);
colorButton.setForeground(fgColor);
}
});
colorButton.setText("Pick a color");
panel.add(colorButton, "cell 0 5");
this.setSize(400, 400);
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String args[]){
new Slide();
}
}
现在我的问题是:是否可以最小化 JColorChooser 的部分。我的意思是我只想显示一次 JColorChooser 并从用户那里获取前景色和背景色
【问题讨论】:
-
如果你想显示一次——那么只调用一次。为什么不简单地测试颜色是否已设置,如果是,不调用颜色选择器?
-
你能给我一个基于该代码的例子吗?
-
您可以将两个
JColorChoosers添加到JTabbedPane并在JOptionPane中显示
标签: java swing jcolorchooser