import javax.swing.*; import java.awt.event.*; public class TextFieldExample { public static void main(String[] args) { JFrame frame = new JFrame("Text Field Example"); JTextField textField = new JTextField(20); textField.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.out.println("Text entered: " + textField.getText()); } }); frame.add(textField); frame.pack(); frame.setVisible(true); } }
import javax.swing.*; import java.awt.event.*; public class TextFieldExample { public static void main(String[] args) { JFrame frame = new JFrame("Text Field Example"); JTextField textField = new JTextField(20); JLabel label = new JLabel(); textField.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { label.setText("Text entered: " + textField.getText()); } }); JPanel panel = new JPanel(); panel.add(textField); panel.add(label); frame.add(panel); frame.pack(); frame.setVisible(true); } }Both examples use the javax.swing package library.