go to previous page   go to home page   go to next page

Answer:

actionPerformed()


Not Quite Correct Program

Here is the program with an actionPerformed() method added in the correct place. The program is complete, and can be compiled and run. However, there is something wrong with it. You might want to compile and run it, and see what is wrong.

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()                           
  {
    super( title );

    redButton = new JButton("Red");
    grnButton = new JButton("Green");

    // choose the layout manager
    setLayout( new FlowLayout() );
    add( redButton );                      
    add( grnButton );                      


    // register the buttonDemo frame (this)
    // as the listener for both JButtons. 
    redButton.addActionListener( this );
    grnButton.addActionListener( this ); 
    
    setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );   
  }
  
  
  public void actionPerformed( ActionEvent evt)
  {
    getContentPane().setBackground( Color.green );
    repaint();
  }

  public static void main ( String[] args )
  {
    TwoButtons demo  = new TwoButtons( "Click a Button") ;

    demo.setSize( 200, 150 );     
    demo.setVisible( true );      

  }
}

Check back to the description of what this program is supposed to do (its specifications) and determine where the current version of the program errors.


QUESTION 9:

What is wrong with the program?