Java Reference
In-Depth Information
add (
"
one
"
, makeButton (
"
one
"
));
add (
"
two
"
, makeButton (
"
two
"
));
add ("
three
"
, makeButton (
"
three
"
));
} // ctor
/** Create a JButton here and add this object
*toits action listeners list.
**/
JButton makeButton (String name) {
JButton b = new JButton (name);
b.addActionListener (this);
return b;
}
/** Flip to the next card when a button pushed. **/
public void actionPerformed (ActionEvent e) {
fCards.next (this);
}
}
// class CardPanel
Clicking on one of the buttons leads to the invocation of the
next()
method in
the
CardLayout
. This will display the next card in the stack, which circles back
to the beginning when the last card is reached. The cards are stacked according
to the order they are added.
The
JTabbedPane
is a component rather than a layout manager but it pro-
vides an alternative to
CardLayout
for overlaying a set of components. It pro-
vides a set of tabbed pages in which each page can hold a component. Selecting
the tab for a particular page will bring that page to the top. Using a container such
as
JPanel
can, of course, hold many sub-components for a page. Figure 7.11(b)
shows the interface created by the following program that uses a
JTabbedPane
subclass:
public class
TabbedApplet
extends JApplet
{
public void init()
{
Container contentPane = getContentPane ();
// Create an instance of the JTabbedPane subclass
Tabs tabs = new Tabs (this);
// Add the Tabs object to the applet's panel.
contentPane.add (tabs);
} // init
} // class TabbedApplet
Search WWH ::
Custom Search