import javax.swing.*; // all of the J components import java.awt.event.*; // ActionListener, ActionEvent // this program creates a simple GUI of an instruction, an input field and an output field // when the user enters text into the input field and presses the key, the ActionEvent // occurs invoking actionPerformed which then takes the input and moves it to the output JLabel public class TextFieldExample { public static void main(String[] args) { JFrame f=new JFrame("text field example"); f.setSize(500,100); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); TPanel tp=new TPanel(); f.add(tp); f.setVisible(true); } public static class TPanel extends JPanel implements ActionListener { private JLabel lab1, out; // lab1 is the instruction ("Enter some text"), out is the output JLabel private JTextField in; // in is the input JTextField where the user will enter text public TPanel() { lab1=new JLabel("Enter some text"); // instantiate GUI components in=new JTextField("",10); out=new JLabel(" "); add(lab1); // use a simple flow layout to position these three items on this JPanel in the same row add(in); add(out); in.addActionListener(this); // the in JTextField will respond when the key is pressed } public void actionPerformed(ActionEvent e) // upon from the in JTextField, actionPerformed is invoked { out.setText(in.getText()); // get the text entered into in and copy it to the JLabel out in.setText(""); // reset in to be empty } } }