///////////////////////// Simple Text Box Example //////////////////// /* This is an example of a label, text field, and button. Text written in the text field will display in a label directly after the submit button. This example uses simple flow layout and no panels. */ import java.awt.*; import java.awt.event.*; import javax.swing.*; public class SimpleTextBoxExample extends JFrame implements ActionListener{ JLabel lbl = new JLabel("Label Text Goes Here:"); JTextField txt = new JTextField(10); JButton submit = new JButton("Submit"); JLabel result = new JLabel(); // Initalize components here, including panels ///////////////// Constructor method ///////////////////////////////// public SimpleTextBoxExample(){ super("Simple Listener Example"); //Displays the name of the frame setSize(300,200); //x,y demensions of the frame in pixels // Closes the frame when the X at the top right corner is clicked setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Layout manager FlowLayout flo = new FlowLayout(); setLayout(flo); add(lbl);// Add the label add(txt);// Add the text field add(submit); // Add the submit button add(result); // Allows the program to listen for the Submit button submit.addActionListener(this); setVisible(true); //Displays the frame } ////////////////// Action Listener Method ///////////////////////// /* Listener to take in any actions performed by user * such as button clicks, Mouse Clicks, and Enter Key press, to name * a few. */ public void actionPerformed(ActionEvent event){ String strResult = txt.getText(); result.setText(strResult); } public static void main(String[] arguments) { //Crates an instance of the frame SimpleTextBoxExample myFrameSetup = new SimpleTextBoxExample(); } }