import java.awt.FlowLayout; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import javax.swing.JFrame; import javax.swing.JTextField; import javax.swing.JPasswordField; import javax.swing.JOptionPane; import javax.swing.JLabel; /** A simple JFrame---similar to the one presented in the YouTube video "Java Programming Tutorial-53 ActionListener by YouTube user "thenewboston". This class corresponds to the tuna class from the video. (http://www.youtube.com/watch?v=qhYook53olE) @author Youtube user "thenewboston" @author Phill Conrad @see Youtube Link */ public class MyJFrame extends JFrame { private JLabel theLabel; private JTextField jtf1; private JTextField jtf2; private JTextField jtf3; public MyJFrame() { super("My Window Title"); // superclass constructor sets the title setLayout(new FlowLayout()); // default layout theLabel = new JLabel("This is my JFrame"); theLabel.setToolTipText("Click in the fields, change them, then hit Enter while in the field. See what happens!"); add(theLabel); jtf1 = new JTextField(10); add(jtf1); jtf2 = new JTextField("default text"); add(jtf2); jtf3 = new JTextField("uneditable"); jtf3.setEditable(false); add(jtf3); // Add some brains to the things on the screen // so they are waiting for something--listening, as it were MyHandler handler = new MyHandler(); jtf1.addActionListener(handler); jtf2.addActionListener(handler); jtf3.addActionListener(handler); } // MyJFrame // inner class private class MyHandler implements ActionListener { public void actionPerformed(ActionEvent event) { String string = ""; if (event.getSource()==jtf1) { // getActionCommand gets the text from the JTextField string = String.format("field 1: %s", event.getActionCommand()); } else if (event.getSource()==jtf2) { string = String.format("field 2: %s", event.getActionCommand()); } else if (event.getSource()==jtf3) { string = String.format("field 3: %s", event.getActionCommand()); } // if-else if-else if-else System.out.println(string); } // actionPerformed }// innerclass MyHandler } // outerclass MyJFrame