Class 1, Part 2 | Intro to Java 30-IT-396 |
Another way to do the same thing...
/**
*
* @author jonesbr@email.uc.edu
*/
public class SecondEvent extends javax.swing.JFrame
{
/** Creates new form
SecondEvent */
public SecondEvent()
{
initComponents();
}
/** 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()
{
// This panel holds our components.
jPanel1 = new javax.swing.JPanel();
// add components to our panel
jLabel1 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
jLabel2 = new javax.swing.JLabel();
// A FlowLayout puts all of the components next to each other, according
to their
// preferred size.
getContentPane().setLayout(new java.awt.FlowLayout());
// Here we're adding an anonymous inner class.
// Note that we define an object right in this method call, and that object
// does not have a name. Hence, the anonymous inner class.
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
exitForm(evt);
}
});
// Set up the parameters of our components.
jPanel1.setLayout(new java.awt.GridLayout(1, 0));
jLabel1.setText("Enter some text.");
jLabel1.setName("lblEnter");
jPanel1.add(jLabel1);
jTextField1.setName("txtEnter");
jPanel1.add(jTextField1);
jButton1.setText("Click Me");
jButton1.setName("btnClickMe");
// Here we're adding an anonymous inner class.
// Note that we define an object right in this method call, and that object
// does not have a name. Hence, the anonymous inner class.
// This class will call the ButtonClick method when the button is clicked.
java.awt.event.ActionListener listener = new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent evt) {
ButtonClick(evt);
}
};
jButton1.addActionListener(listener);
jPanel1.add(jButton1);
jLabel2.setName("lblResult");
jPanel1.add(jLabel2);
getContentPane().add(jPanel1);
pack();
}
// Here's the method
that the inner class calls. Interesting, huh?
private void ButtonClick(java.awt.event.ActionEvent
evt) {
// Add your handling code here:
String strEnteredText = jTextField1.getText();
jLabel2.setText(strEnteredText);
}
/** 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 SecondEvent().show();
}
// Variables declaration
- do not modify
private javax.swing.JTextField
jTextField1;
private javax.swing.JButton
jButton1;
private javax.swing.JLabel
jLabel2;
private javax.swing.JLabel
jLabel1;
private javax.swing.JPanel
jPanel1;
// End of variables
declaration
}
Created by: Brandan
Jones January 4, 2002