Java Reference
In-Depth Information
Display 17.17 A Text Field (part 1 of 2)
1
import javax.swing.JFrame;
2
import javax.swing.JTextField;
3
import javax.swing.JPanel;
4
import javax.swing.JLabel;
5
import javax.swing.JButton;
6
import java.awt.GridLayout;
7
import java.awt.BorderLayout;
8
import java.awt.FlowLayout;
9
import java.awt.Color;
10
import java.awt.event.ActionListener;
11
import java.awt.event.ActionEvent;
public class TextFieldDemo extends JFrame
12
implements ActionListener
13
14
{
15
public static final int WIDTH = 400;
16
public static final int HEIGHT = 200;
17
public static final int NUMBER_OF_CHAR = 30;
18
private JTextField name;
19
public static void main(String[] args)
20
{
21
TextFieldDemo gui = new TextFieldDemo();
22
gui.setVisible( true );
23
}
24
public TextFieldDemo()
25
{
26
super ("Text Field Demo");
27
setSize(WIDTH, HEIGHT);
28
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
29
setLayout( new GridLayout(2, 1));
30
JPanel namePanel = new JPanel();
31
namePanel.setLayout( new BorderLayout());
32
namePanel.setBackground(Color.WHITE);
33
name = new JTextField(NUMBER_OF_CHAR);
34
namePanel.add(name, BorderLayout.SOUTH);
35
JLabel nameLabel = new JLabel("Enter your name here:");
36
namePanel.add(nameLabel, BorderLayout.CENTER);
37
add(namePanel);
(continued)
Search WWH ::




Custom Search