Hi guys
Below code mean to change the background colour by clicking the redButton or blueButton, but the code doesn't work, (only can show the frame and two buttons), does any anyone know why?
Thanks heaps
package brainydraw; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class PictureButton extends JFrame { private JButton redButton; private JButton blueButton; public PictureButton() { setSize(200, 200); setLocation(200, 200); redButton = new JButton("RED"); blueButton = new JButton("BLUE"); Container content = getContentPane(); content.setLayout(new FlowLayout()); content.add(redButton); content.add(blueButton); //register the current panel as listener for the buttons ActionListener rBListener = new RedAndBlueButtonListener(); redButton.addActionListener(rBListener); blueButton.addActionListener(rBListener); } public class RedAndBlueButtonListener implements ActionListener { public void actionPerformed(ActionEvent ae) { Color color = getBackground(); // color will be set Object source = ae.getSource(); if (source == redButton) color = Color.red; else if (source == blueButton) color = Color.blue; setBackground(color); repaint(); } } public static void main(String[] args) { JFrame f = new PictureButton(); f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent we) { System.exit(0); } }); f.setVisible(true); } }