Class 1, Part 2 | Intro to Java 30-IT-396 |
A way to do something similar
/**
*
* @author jonesbr@email.uc.edu
*/
public class FourthEvent extends javax.swing.JFrame
{
/** Creates new
form FourthEvent */
public FourthEvent()
{
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.
// We can instantiate and add our inner class listener object here.
java.awt.event.ActionListener listener = new MyListener(this);
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?
protected 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 FourthEvent().show();
}
// Pardon my blatant
ignorance of the JavaBeans standard here.
public String
getText() {
return jTextField1.getText();
}
// Again, pardon
my blatant ignorance of the JavaBeans standard here.
public void setText(String
text) {
jLabel2.setText(text);
}
// 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
}
// Here's our inner class.
class MyListener implements java.awt.event.ActionListener
{
FourthEvent view;
MyListener(FourthEvent
fourth) {
view = fourth;
}
public void actionPerformed(java.awt.event.ActionEvent
evt) {
view.setText(view.getText());
}
}
Created by: Brandan
Jones January 4, 2002