Class 1, Part 2 | Intro to Java 30-IT-396 |
Handling Multiple Events
/**
*
* @author default
*/
public class MultipleComponents extends javax.swing.JFrame {
/** Creates new form FirstEvent */
public MultipleComponents() {
initComponents();
// We have two buttons. Let's make two listener objects, and instantiate
// each with unique objects. This will allow us to re-use the class,
but
// still tailor it for each button. Alas, object oriented programming.
btnClickMe1.addActionListener(new MyListener(txtEnter1, lblDisplay1));
btnClickMe2.addActionListener(new MyListener(txtEnter2, lblDisplay2));
}
/** This method is called from within the constructor
to
* initialize the form.
* WARNING: Do NOT modify this code. The
content of this method is
* always regenerated by the Form Editor.
*/
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
lblEnter1 = new javax.swing.JLabel();
txtEnter1 = new javax.swing.JTextField();
btnClickMe1 = new javax.swing.JButton();
lblDisplay1 = new javax.swing.JLabel();
jPanel11 = new javax.swing.JPanel();
lblEnter2 = new javax.swing.JLabel();
txtEnter2 = new javax.swing.JTextField();
btnClickMe2 = new javax.swing.JButton();
lblDisplay2 = new javax.swing.JLabel();
getContentPane().setLayout(new java.awt.FlowLayout());
addWindowListener(new
java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
exitForm(evt);
}
});
jPanel1.setLayout(new java.awt.GridLayout(1, 0));
lblEnter1.setText("Enter
some text.");
lblEnter1.setName("lblEnter");
jPanel1.add(lblEnter1);
txtEnter1.setName("txtEnter");
jPanel1.add(txtEnter1);
btnClickMe1.setText("Click
Me");
btnClickMe1.setName("btnClickMe");
jPanel1.add(btnClickMe1);
lblDisplay1.setName("lblResult");
jPanel1.add(lblDisplay1);
getContentPane().add(jPanel1);
jPanel11.setLayout(new java.awt.GridLayout());
lblEnter2.setText("Enter
some text.");
lblEnter2.setName("lblEnter2");
jPanel11.add(lblEnter2);
txtEnter2.setName("txtEnter2");
jPanel11.add(txtEnter2);
btnClickMe2.setText("Click
Me");
btnClickMe2.setName("btnClickMe2");
jPanel11.add(btnClickMe2);
lblDisplay2.setName("lblResult");
jPanel11.add(lblDisplay2);
getContentPane().add(jPanel11);
pack();
}
/** Exit the Application */
private void exitForm(java.awt.event.WindowEvent
evt) {
System.exit(0);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
new MultipleComponents().show();
}
// Variables declaration - do not modify
private javax.swing.JLabel lblDisplay1;
private javax.swing.JPanel jPanel11;
private javax.swing.JButton btnClickMe2;
private javax.swing.JButton btnClickMe1;
private javax.swing.JLabel lblEnter2;
private javax.swing.JLabel lblEnter1;
private javax.swing.JTextField txtEnter2;
private javax.swing.JTextField txtEnter1;
private javax.swing.JPanel jPanel1;
private javax.swing.JLabel lblDisplay2;
// End of variables declaration
// A listener class that
we re-use for both parts of the form.
class MyListener implements
java.awt.event.ActionListener {
javax.swing.JTextField source;
javax.swing.JLabel destination;
MyListener(javax.swing.JTextField source, javax.swing.JLabel destination)
{
this.source = source;
this.destination = destination;
}
public void actionPerformed(java.awt.event.ActionEvent evt) {
destination.setText(source.getText());
}
}
}
Created by: Brandan
Jones January 4, 2002