Java Reference
In-Depth Information
what action to perform. Here we use the Applet showStatus() method to
display a string on the browser status line. Figure 7.1 shows the program running
in the applet viewer program.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/** Demonstrate event handling with two buttons. **/
public class PlainButtonApplet extends JApplet
implements ActionListener
{
int fPushACount = 0;
int fPushBCount = 0;
/** Build the interface with two buttons. **/
public void init () {
Container content - pane = getContentPane ();
content - pane.setLayout (new FlowLayout ());
// Create an instance of JButton
JButton button - a = new JButton ( " A " );
// Create an instance of JButton
JButton button - b = new JButton ("B");
// Add this applet to each button's ActionListener
// list
button - a.addActionListener (this);
button - b.addActionListener (this);
JPanel panel = new JPanel ();
// Add the buttons to the content pane.
content - pane.add (button - a);
content - pane.add (button - b);
} // init
/** Count each button click and post total on status
* line. **/
public void actionPerformed (ActionEvent event) {
String cmd = event.getActionCommand ();
 
Search WWH ::




Custom Search