import java.awt.*; import java.awt.event.*; import javax.swing.*; //A simple Applet to test the Swing classes //Requires Java 2 runtime and Netscape 6 or higher or IE public class swingy extends JApplet implements ActionListener { public void init() { //declare a container for graphics objects Container contentPane=getContentPane(); contentPane.setBackground(Color.cyan); contentPane.setLayout(new BorderLayout()); //declare a button panel JPanel buttonPanel=new JPanel(); buttonPanel.setBackground(Color.yellow); buttonPanel.setLayout(new FlowLayout()); //declare 2 buttons and activate them //and put them in the panel JButton redButton=new JButton("Red"); redButton.setBackground(Color.red); redButton.addActionListener(this); buttonPanel.add(redButton); JButton greenButton=new JButton("Green"); greenButton.setBackground(Color.green); greenButton.addActionListener(this); buttonPanel.add(greenButton); //put the panel at the top of the screen contentPane.add(buttonPanel, BorderLayout.NORTH); } //if user clicks red, make the screen red //if user clicks green, make the screen green public void actionPerformed(ActionEvent e) { //handle user button clicks Container contentPanel=getContentPane(); if(e.getActionCommand().equals("Red")) contentPanel.setBackground(Color.red); else if(e.getActionCommand().equals("Green")) contentPanel.setBackground(Color.green); } }