actionPerformed()
Here is the program with an actionPerformed()
method added in
the correct place.
You can copy this program to a file, compile, and run it.
However,
there is something wrong with it.
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class TwoButtons extends JFrame implements ActionListener { JButton redButton ; JButton grnButton ; // constructor for TwoButtons public TwoButtons() { redButton = new JButton("Red"); grnButton = new JButton("Green"); getContentPane().setLayout( new FlowLayout() ); getContentPane().add( redButton ); getContentPane().add( grnButton ); // register the buttonDemo frame // (the frame this constructor is making) // as the listener for both Buttons. redButton.addActionListener( this ); grnButton.addActionListener( this ); setDefaultCloseOperation( WindowConstants.EXIT_ON_CLOSE ); } public void actionPerformed( ActionEvent evt) { getContentPane().setBackground( Color.blue ); repaint(); } public static void main ( String[] args ) { TwoButtons demo = new TwoButtons() ; demo.setSize( 200, 150 ); demo.setVisible( true ); } }
Check back to the description of what this program is supposed to do (its specifications) and determinine where the current version of the program errors.
What is wrong with the program?