Java Reference
In-Depth Information
if (ae.getActionCommand ().equals ( " Add " )) {
fTextOutput.setText ( " x+y =" + (val1+val2));
} else {
fTextOutput.setText ("x*y="+ (val1*val2));
}
} // actionPerformed
} // class MultiPanelWithEvents
import java.awt.event.*;
import javax.swing.*;
/** JPanel subclass with two buttons. **/
public class ActionButtonsPanel extends JPanel
{
/** Constructor adds 2 buttons to the panel and
* adds the listener passed as an argument to the
* action listener list in each button.
**/
ActionButtonsPanel (ActionListener listener) {
// Create two buttons
JButton add - but = new JButton ("Add");
JButton mult - but = new JButton ("Mult");
add - but.addActionListener (listener);
mult - but.addActionListener (listener);
// Put a button in each grid cell
add (add - but);
add (mult - but);
} // ctor
} // class ActionButtonsPanel
The actionPerformed() method in the applet grabs the strings from the
text fields and converts them to floating-point values. It then examines the action
command string (by default this is the text on the button) to determine which
button sent the event. It then carries out the chosen operation and displays the
result in the text area.
7.2.5 Mouse events
As the following diagram shows, the MouseEvent class inherits from several
classes:
Search WWH ::




Custom Search