【发布时间】:2016-03-03 02:57:37
【问题描述】:
我有一个带有 jcolorchooser 一些文本字段和一个按钮的简单程序。 当我按下按钮时,jcolorchooser 出现,然后我选择一种颜色。 现在假设我想采用我选择的背景颜色并将其应用于我的按钮,如下所示:
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("", "[][][][][]", "[][][][][][]"));
colorButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
JColorChooser.showDialog(null, "title", null);
bgColor=colorPicker.getBackground();
colorButton.setBackground(bgColor);
}
});
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();
}
}
问题是我的 bgcolor 不会应用于我的 colorButton。有什么想法吗?
【问题讨论】:
标签: java swing jcolorchooser