/* * File: TextFieldExample2.java * ---------------------------- * This program shows an example of a text field */ import acm.program.*; import java.awt.event.*; import javax.swing.*; /** This class displays a greeting whenever a name is entered */ public class TextFieldExample2 extends ConsoleProgram { public void init() { setFont("Times New Roman-24"); add(new JLabel("Name"), SOUTH); nameField = new JTextField(50); nameField.setActionCommand("Go"); // give command name to text field add(nameField, SOUTH); nameField.addActionListener(this); add(new JButton("Go"), SOUTH); addActionListeners(); } public void actionPerformed(ActionEvent e) { String cmd = e.getActionCommand(); if (cmd.equals("Go")) { println("Hello, " + nameField.getText()); } } /* Private instance variables */ private JTextField nameField; }