如何更改按钮的颜色?
How to change button's color?
在我的程序中我有 2 个按钮:第一个改变框架背景,第二个改变按钮背景(只为它自己)。关键是程序应该为程序中的每个按钮更改按钮背景(不仅仅是为它自己)。我应该如何重写 dialog?
class ButtonBackgroundChange implements ActionListener{
private JDialog dialog;
private JColorChooser chooser;
private Color currentBackground;
public ButtonBackgroundChange(JButton button1, Component component, Color currentBackground){
this.currentBackground = currentBackground;
chooser = new JColorChooser();
dialog = JColorChooser.createDialog(component, "Background Color", false /* not modal */, chooser, event -> button1.setBackground(chooser.getColor()), null /* no Cancel button listener */);
}
@Override
public void actionPerformed(ActionEvent e) {
chooser.setColor(currentBackground);
dialog.setVisible(true);
}
}
如果将按钮存储在传递给函数的列表中,您将能够遍历它并为每个按钮设置背景颜色。
在我的程序中我有 2 个按钮:第一个改变框架背景,第二个改变按钮背景(只为它自己)。关键是程序应该为程序中的每个按钮更改按钮背景(不仅仅是为它自己)。我应该如何重写 dialog?
class ButtonBackgroundChange implements ActionListener{
private JDialog dialog;
private JColorChooser chooser;
private Color currentBackground;
public ButtonBackgroundChange(JButton button1, Component component, Color currentBackground){
this.currentBackground = currentBackground;
chooser = new JColorChooser();
dialog = JColorChooser.createDialog(component, "Background Color", false /* not modal */, chooser, event -> button1.setBackground(chooser.getColor()), null /* no Cancel button listener */);
}
@Override
public void actionPerformed(ActionEvent e) {
chooser.setColor(currentBackground);
dialog.setVisible(true);
}
}
如果将按钮存储在传递给函数的列表中,您将能够遍历它并为每个按钮设置背景颜色。