As discussed in AWT and Swing
sections, we prefer to use the lightweight Swing components
if possible since they are so much more flexible than the standard
AWT components and there is a greater selection.
On some platforms with limited resources it may be necessary to
use the less memory consuming AWT approach. Chapter
6: Supplements provides a brief overview of GUI building within
only the AWT.
Note: Do not mix Swing components
and AWT components. Use either all Swing or all AWT. Otherwise,
the display of the components will become very unstable and cause
your your computer to explode. Not really, at least not the last
part about an explosion.
So we will concentrate on Swing GUI development. In this chapter
we will discuss some basic aspects of interface design, starting
here with a demonstration of creating a subclass of JPanel.
An interface typically sub-divides the display into several such
panels, allowing for a flexible and logical arrangement of the components.
The following applet creates a subclass of JPanel
that two buttons. An instance of this JPanel
is then added to the applet's content
pane.
|
ButtonsPanelApplet.java
|
import
java.awt.*;
import javax.swing.*;
public
class
ButtonsPanelApplet extends
JApplet {
public void init()
{
Container
content_pane = getContentPane();
// Create an instance of JButton
ActionButtonsPanel buttonsPanel =
new
ActionButtonsPanel ();
// Add the button to the contentPane.
content_pane.add
(buttonsPanel);
}
}
// JPanel subclass with two buttons
.
class ActionButtonsPanel
extends JPanel
{
ActionButtonsPanel () {
// Create two buttons
JButton addBut = new
JButton("Add");
JButton multBut = new
JButton("Mult");
// Put a button in each grid
cell
add(addBut);
add(multBut);
} //
ctor
} // class ActionButtonsPanel
|
Latest update: Oct. 25, 2004
|