// Adapter class saves the work of having to implement all of the empty methods in an Interface
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
class FrameListener extends WindowAdapter
{
public void windowClosing( WindowEvent e )
{ System.exit( 0 );
}
}
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Listener implements ActionListener
{
GUI x;
Listener( GUI a )
{
x = a;
}
public void actionPerformed( ActionEvent e )
{
if( e.getActionCommand().equals( "buttonA") )
{
x.buttonA.setText("press me!");
x.setTitle( "buttonA " );
}
System.out.println( e.getActionCommand() );
} //end actionPerformed()
} //end class
import java.awt.Container;
import javax.swing.JFrame;
import javax.swing.JButton;
public class GUI extends JFrame
{
private static final long serialVersionUID = 1L;
JButton buttonA = new JButton( "buttonA" );
Listener listen = new Listener( this );
FrameListener frameListener = new FrameListener();
GUI()
{
Container c = getContentPane();
c.setLayout( null ); //requires absolute layout definition
c.add( buttonA );
buttonA.setBounds( 10 , 10 , 120 , 70 );
buttonA.addActionListener( listen );
this.addWindowListener( frameListener );
setSize( 250 , 250 );
setTitle( "Button Press" );
setVisible( true );
setResizable( false );
}
} //end class
// MAIN
public class GUIMain
{
@SuppressWarnings("unused")
public static void main( String[] args )
{
GUI x = new GUI();
}
} //end class