JTextField Example
This JTextField example shows one way of allowing the user to enter text. A JTextField allows the input and editing of a single line of text. Multi-line text can be input and edited with a JTextArea.
Normally a user interface with such text components would have a 'submit' button with an associated action listener. This is shown using a JTextArea in the next example on the left.
// JTextField Example
import java.awt.*;
import javax.swing.*;
class JTextFieldExample {
public static void main(String[] args) {
JFrame frame = new JFrame();
Container cp = frame.getContentPane();
cp.setLayout(new FlowLayout());
JLabel label = new JLabel("Web address: ", JLabel.RIGHT);
JTextField jTextField = new JTextField(20);
cp.add(label);
cp.add(jTextField);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
