vendredi 29 mai 2015

java swing - how to change window background

I was trying to make a button that switches the window background from default to red. I haven't found any preset colors to match the default so i tried to get it from panel.getBackground when i created it. I have an error at line 11, i don't know how to check the current background color.

JPanel panel = new JPanel();
    panel.setBounds(0, 0, 434, 262);
    frame.getContentPane().add(panel);
    panel.setLayout(null);
    panel.setVisible(true);
    Color c=panel.getBackground();

    JButton btnRed = new JButton("Red");
    btnRed.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if(panel.getBackground(c));{
                panel.setBackground(Color.RED);
            }
            else{
                panel.setBackground(c);
            }
        }
    });

Aucun commentaire:

Enregistrer un commentaire