简体   繁体   中英

How to change button's color?

In my program I've got 2 buttons: the first one changes frame background, the second one changes button background(only for itself). The point is that program should change button background for every button in the program(not only for itself). How am I supposed to to rewrite 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);
    }
}

如果您将按钮存储在传递给函数的列表中,您将能够遍历它并为每个按钮设置背景颜色。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM