Académique Documents
Professionnel Documents
Culture Documents
Events
1. Basic structure
2. Panels
3. Text fields
4. Layout managers
Grid layout
Border layout
5. Events
6. Event listener
7. Example: increment
8. Two buttons
Two listeners
9. Boxes and struts
10. The main method
1
1. Basic structure
To build a basic window system, you
• create a window
• create a panel with components
• add the panel to the window
• pack the window around the panel
• make the window visible on the screen
2
2. Panels
You create a panel, add components to the panel, and add
the panel to the window. A panel has a layout to control where
the components are placed. In the simplest layout, components
appear in the order they are added, top left to bottom right.
Here is a GUI to add 1 to the input value:
A label (class JLabel) shows text. The colour of the text is set
by setForeground, and the colour of the background is set
by setBackground.
3
3. Text fields
A text field provides textual input and output. It can be set
to a fixed length, or given a string to display. The user can
type a value into the field.
String s = field.getText();
int i = Integer.parseInt(s);
double d = Double.parseDouble(s);
If the string is not a valid number the system throws a non-
fatal exception, so you may need to wrap the code in a try-
catch block.
4
4. Layout managers
A layout manager controls the way that components are
placed in a panel. There are three layout managers:
setLayout(new FlowLayout());
setLayout(new FlowLayout(FlowLayout.LEFT));
5
Grid layout
A grid layout divides the window's content pane into cells
of the same size. The constructor takes two arguments, the
number of rows and columns. Components are added in row
and then column order.
class Panel extends JPanel
{ public Panel()
{ setup();
build(); }
6
BorderLayout
A BorderLayout with different sized panels is often
enough. A BorderLayout has five areas; north and south only
expand vertically, east and west only expand horizontally.
North
Center
West East
South
public class Panel extends JPanel
{ ...
setLayout(new BorderLayout());
add(label("left", 120), BorderLayout.WEST);
add(label("right", 40), BorderLayout.EAST);
7
5. Events
You can now set up a GUI system so it is visible on the
screen, but to make it react to the user is a whole new story.
Your code has to react to events.
8
6. Event listeners
The last step is to add the action listener to the button, so
the button knows where to send the event:
button.addActionListener(listener);
The listener is usually a private class, coded inside the panel
class. A private class can access it's owner's attributes and
methods, and only the owner can create an object of that class.
A private class is placed at the end of the containing class.
public class Panel extends JPanel
{ private JTextField field = new JTField(10);
9
7. Example: increment
GUI systems have a very different control structure from
non-GUI systems. Before, the root class drove the system by
creating objects and calling methods on those objects. Here,
the listener - the code in actionPerformed - drives the
system; it waits for events and then calls methods to react to
those events.
import javax.swing.*;
10
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public Panel()
{ setup();
build(); }
11
8. Two buttons
A system with more than one button has to know which
button was clicked. The source of a button event is found by
matching
• the button object
if (e.getSource() == button)
• the button label
if (e.getActionCommand().equals("...")
This panel has two buttons. Both buttons have the same
listener, and the listener sorts out which button was pressed.
private void build()
{ ...
Listener listener = new Listener();
add(button("+1", listener));
add(button("-1", listener)); }
12
Two listeners
Each button can have its own listener. This makes the
build and listener code very simple, so it is a very common
GUI coding pattern.
13
9. Boxes and struts
A box gives you fine control over where components
appear in a panel. You create the box, add components and
spaces (called struts) to the box, and add the box to the panel.
Close a window
To close a window, click on the cross at top right. To exit
the whole system when you close a window, set the close
operation on the window to exit on close; the method and
constant are supplied by class JFrame.
setDefaultCloseOperation(EXIT_ON_CLOSE);
14
10. The main method
The standard pattern is to use a static main method to
create the system. All the method does is call the constructor;
you do not need to store an object of the root class.
In BlueJ, you call the static main method from the class pop-
up window and no root object is created. The window appears
with its panels, and the system waits for user actions.
15