/* * File: ButtonTest.java * * Create some buttons with simple actions * * Copyright: Northeast Parallel Architectures Center * */ import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.net.*; public class ButtonTest extends JApplet implements ActionListener { // there are three buttons labelled red, white and blue // this class is the event listener for the buttons private JButton button1, button2, button3; private Font f = new Font( "Serif", Font.BOLD, 24 ); private ImageIcon icon1, icon2; public void init() { try { icon1=new ImageIcon(new URL("http://www.npac.syr.edu/projects/webtech/java/examples1.2/buttontest/radio.gif")); icon2=new ImageIcon(new URL("http://www.npac.syr.edu/projects/webtech/java/examples1.2/buttontest/radioPressed.gif")); } catch (Exception e){} getContentPane().setLayout(new FlowLayout()); getContentPane().setBackground( Color.white ); getContentPane().setFont (f); button1 = new JButton( "Red", icon1 ); button1.addActionListener( this ); getContentPane().add( button1 ); button2 = new JButton( "White", icon1 ); button2.addActionListener( this ); getContentPane().add( button2 ); button3 = new JButton( "Blue", icon1 ); button3.addActionListener( this ); getContentPane().add( button3 ); } public void actionPerformed( ActionEvent event ) { // to respond to a button click, change the background color of the applet String buttonLabel = event.getActionCommand(); if ( buttonLabel.equals( "Red" ) ) { getContentPane().setBackground( Color.red ); changeIcon(button1); } else if ( buttonLabel.equals( "White" ) ) { getContentPane().setBackground( Color.white ); changeIcon(button2); } else if ( buttonLabel.equals( "Blue" ) ) { getContentPane().setBackground( Color.blue ); changeIcon(button3); } colorButtons(); } private void colorButtons() { button1.setBackground( Color.lightGray ); button2.setBackground( Color.lightGray ); button3.setBackground( Color.lightGray ); } public void changeIcon(JButton b1) { button1.setIcon(icon1); button2.setIcon(icon1); button3.setIcon(icon1); b1.setIcon(icon2); } }//end of class