Java Reference
In-Depth Information
JButton
JLabel
JButton
JPanel
JFrame
Figure 3.2 The construction of the counter GUI. The intermediate JPanel is introduced to
guarantee modularity. The panel can be reused in other applications
The frame of our counter application is implemented in the class Counter-
Frame . This class is derived from SimpleFrame so that the application is termi-
nated when the frame is closed. A CounterFrame has a CounterPanel embedded
into the central position of its content pane. We list the code of the classes Coun-
terPanel , CounterFrame and the driver class CounterDriver below.
When the application is started, the frame appears. We can press the buttons
and see that their colour changes for a short period in response. This function
is automatically supplied. The application does not, however, change the value of
the counter in the label. It cannot do this at this point because we have not yet
'told' the application to do this. The control part introduced in Section 3.4 will
be responsible for that. In the listing of CounterPanel ; Lines 29 to 31 will later
be used to enable the listener. Currently they are comments, so do not have any
effect.
File: its/CounterGUI/CounterPanel.java
1.
package its.CounterGUI;
2.
3.
import javax.swing.JPanel;
import javax.swing.JButton;
4.
import javax.swing.JLabel;
5.
import java.awt.BorderLayout;
6.
import javax.swing.SwingConstants;
7.
8.
Search WWH ::




Custom Search