// File: Colours2.java
// T Balls : March 1998
// Capture events from Buttons
// and change background colour
// Process window close event
import java.awt.*;
import java.awt.event.*;
public class Colours2 extends Frame
{ public static void main( String[] args )
{ new Colours2();
}
public Colours2()
{ setTitle( "Colours2" );
setSize ( 200, 200 );
setLayout( new FlowLayout() );
Button red = new Button( "Red" );
red.setBackground( Color.white );
red.setForeground( Color.black );
Button blue = new Button( "Blue" );
blue.setBackground( Color.white );
blue.setForeground( Color.black );
add( red );
add( blue );
red.addActionListener( new ActionListener() {
public void actionPerformed( ActionEvent e )
{ setBackground( Color.red );
repaint();
}
});
blue.addActionListener( new ActionListener() {
public void actionPerformed( ActionEvent e )
{ setBackground( Color.blue );
repaint();
}
});
addWindowListener( new WindowAdapter() {
public void windowClosing( WindowEvent e )
{ System.exit(0);
}
});
setVisible( true );
}
}
|