You will learn how to create various GUI components. First, you will need to instantiate a container for GUI elements and then put all necessary GUI components into this container. To add a swing component to your applet use the add() method of class Container. Use the method setLayout() to set up the layout of the GUI components.
To create a label use one of the following constructors:
Example:
| GUI | Java code |
|---|---|
|
panel.setLayout(new GridLayout(3,1));
JLabel l = new JLabel("alignment left", SwingConstants.LEFT);
l.setFont(new Font("Helvetica", Font.BOLD, 14));
panel.add(l);
panel.add(new JLabel("alignment center", SwingConstants.CENTER));
panel.add(new JLabel("alignment right", SwingConstants.RIGHT));
|
The class JLabel has the following methods: getText(), setText(String), getAlignment(), setAlignment(int).
To create a button use one of the following constructors:
Example:
| GUI | Java code |
|---|---|
|
panel.setLayout(new FlowLayout());
panel.add(new JButton("Rewind"));
panel.add(new JButton("Play"));
panel.add(new JButton("Fast Forward"));
panel.add(new JButton("Stop"));
|
The class JButton has methods getText(), setText(String), setEditable(boolean), and addActionListener(ActionListener). The last method is used to add the functionality to the button.
Check boxes can be used in two ways:
Non-exclusive check boxes can be created by using the Checkbox class:
Example:
| GUI | Java code |
|---|---|
|
panel.setLayout(new FlowLayout(FlowLayout.LEFT));
panel.add(new JCheckBox("Shoes"));
panel.add(new JCheckBox("Socks"));
panel.add(new JCheckBox("Pants"));
panel.add(new JCheckBox("Underwear", true));
panel.add(new JCheckBox("Shirt"));
|
Supported methods of the class JCheckBox include: isSelected() (returns a boolean value depending on whether the check box is selected or not), setSelected(boolean), setLocation(), and setItemListener().
To create a series of radio buttons (JRadioButton), you will need to do the following:
Example:
| GUI | Java code |
|---|---|
|
panel.setLayout(new FlowLayout(FlowLayout.LEFT));
JRadioButton b1 = new JRadioButton("Red", false);
JRadioButton b2 = new JRadioButton("Blue", false);
JRadioButton b3 = new JRadioButton("Yellow", false);
JRadioButton b4 = new JRadioButton("Green", true);
JRadioButton b5 = new JRadioButton("Orange", false);
ButtonGroup bgr = new ButtonGroup();
bgr.add(b1);
bgr.add(b2);
bgr.add(b3);
bgr.add(b4);
bgr.add(b5);
panel.add(b1);
panel.add(b2);
panel.add(b3);
panel.add(b4);
panel.add(b5);
|
Radio buttons support (among others) the same methods as check boxes.
Text fields provide an area where you can enter an edit a single line of text. To create a text field use one of the following constructors:
Example:
| GUI | Java code |
|---|---|
|
panel.setLayout(new GridLayout(3,2));
panel.add(new JLabel("Your name:"));
panel.add(new JTextField("your name here", 30));
panel.add(new JLabel("Phone number:"));
panel.add(new JTextField(30));
panel.add(new JLabel("Password:"));
JPasswordField tf = new JPasswordField(30);
tf.setEchoChar('*');
panel.add(tf);
|
Methods of class JTextField include: setText(String), getText() (returns the text contained in the text field as a String), setEditable(boolean), and addActionListener.