当按钮不应该改变时,它们都会改变相同的颜色

时间:2018-01-18 19:21:24

标签: java

我总共有9个按钮。他们每个人都需要有一个单独的颜色,一旦被选中,他们就会改变颜色。截至目前,无论选择哪个按钮,它们都遵循最终颜色的属性(在本例中为白色),但我需要它们各自具有单独的颜色。

import java.awt.Color;
    import java.awt.FlowLayout;
    import javax.swing.JToggleButton;
    import javax.swing.WindowConstants;
    import javax.swing.SwingUtilities;
    import javax.swing.UIManager;

public class NewJFrame extends javax.swing.JFrame {
    private JToggleButton jToggleButton1;
    private JToggleButton jToggleButton2;
    private JToggleButton jToggleButton3;
    private JToggleButton jToggleButton4;
    private JToggleButton jToggleButton5;
    private JToggleButton jToggleButton6;
    private JToggleButton Magenta;
    private JToggleButton jToggleButton8;
    private JToggleButton jToggleButton9;
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                NewJFrame inst = new NewJFrame();
                inst.setLocationRelativeTo(null);
                inst.setVisible(true);
            }
        });
    }

    public NewJFrame() {
        super();
        initGUI();
    }

    private void initGUI() {
        try {
            FlowLayout thisLayout = new FlowLayout();
            getContentPane().setLayout(thisLayout);
            setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
            {
                jToggleButton1 = new JToggleButton();
                getContentPane().add(jToggleButton1);
                jToggleButton1.setText("Red");
                jToggleButton1.setPreferredSize(new java.awt.Dimension(100, 100));
                jToggleButton1.setBackground(null);
                UIManager.put("ToggleButton.select", Color.RED);
                SwingUtilities.updateComponentTreeUI(jToggleButton1);
            }
            {
                jToggleButton2 = new JToggleButton();
                getContentPane().add(jToggleButton2);
                jToggleButton2.setText("Green");
                jToggleButton2.setPreferredSize(new java.awt.Dimension(100, 100));
                jToggleButton2.setBackground(null);
                UIManager.put("ToggleButton.select", Color.GREEN);
                SwingUtilities.updateComponentTreeUI(jToggleButton2);
            }               
            {
                jToggleButton3 = new JToggleButton();
                getContentPane().add(jToggleButton3);
                jToggleButton3.setText("Blue");
                jToggleButton3.setPreferredSize(new java.awt.Dimension(100, 100));
                jToggleButton3.setBackground(null);
                UIManager.put("ToggleButton.select", Color.BLUE);
                SwingUtilities.updateComponentTreeUI(jToggleButton3);
            }
            {
                jToggleButton4 = new JToggleButton();
                getContentPane().add(jToggleButton4);
                jToggleButton4.setText("Orange");
                jToggleButton4.setPreferredSize(new java.awt.Dimension(100, 100));
                jToggleButton4.setBackground(null);
                UIManager.put("ToggleButton.select", Color.ORANGE);
                SwingUtilities.updateComponentTreeUI(jToggleButton4);

            }
            {
                jToggleButton5 = new JToggleButton();
                getContentPane().add(jToggleButton5);
                jToggleButton5.setText("Black");
                jToggleButton5.setPreferredSize(new java.awt.Dimension(100, 100));
                jToggleButton5.setBackground(null);
                UIManager.put("ToggleButton.select", Color.BLACK);
                SwingUtilities.updateComponentTreeUI(jToggleButton5);

            }
            {
                jToggleButton6 = new JToggleButton();
                getContentPane().add(jToggleButton6);
                jToggleButton6.setText("Yellow");
                jToggleButton6.setPreferredSize(new java.awt.Dimension(100, 100));
                jToggleButton6.setBackground(null);
                UIManager.put("ToggleButton.select", Color.YELLOW);
                SwingUtilities.updateComponentTreeUI(jToggleButton6);

            }
            {
                Magenta = new JToggleButton();
                getContentPane().add(Magenta);
                Magenta.setText("Magenta");
                Magenta.setPreferredSize(new java.awt.Dimension(100, 100));
                Magenta.setBackground(null);
                UIManager.put("ToggleButton.select", Color.MAGENTA);
                SwingUtilities.updateComponentTreeUI(Magenta);

            }
            {
                jToggleButton8 = new JToggleButton();
                getContentPane().add(jToggleButton8);
                jToggleButton8.setText("Pink");
                jToggleButton8.setPreferredSize(new java.awt.Dimension(100, 100));
                jToggleButton8.setBackground(null);
                UIManager.put("ToggleButton.select", Color.PINK);
                SwingUtilities.updateComponentTreeUI(jToggleButton8);

            }
            {
                jToggleButton9 = new JToggleButton();
                getContentPane().add(jToggleButton9);
                jToggleButton9.setText("White");
                jToggleButton9.setPreferredSize(new java.awt.Dimension(100, 100));
                jToggleButton9.setBackground(null);
                UIManager.put("ToggleButton.select", Color.WHITE);
                SwingUtilities.updateComponentTreeUI(jToggleButton9);

            }
            pack();
            this.setSize(350, 370);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

1 个答案:

答案 0 :(得分:2)

UIManager.put("ToggleButton.select", ...);

这将为应用程序中的所有选定按钮设置默认颜色。

要设置单个按钮的颜色,您必须使用setBackground。由于您想在选择按钮时更改颜色,您必须对事件做出反应并检查按钮的状态:

jToggleButton1.setUI(new BasicToggleButtonUI());
jToggleButton1.addItemListener(ev -> {
    if (ev.getStateChange() == ItemEvent.SELECTED)
        jToggleButton1.setBackground(Color.RED);
    else
        jToggleButton1.setBackground(null);
});

当你为很多按钮添加相同的逻辑时,创建一个方法并在你的事件监听器中调用它可能是有意义的:

private void updateColorIfSelected(JToggleButton btn, ItemEvent ev, Color color) {
    if (ev.getStateChange() == ItemEvent.SELECTED)
        btn.setBackground(color);
    else
        btn.setBackground(null);
}

然后:

jToggleButton1.setUI(new BasicToggleButtonUI());
jToggleButton1.addItemListener(ev -> updateColorIfSelected(jToggleButton1, ev, Color.RED));