♠ Posted by Unknown in Adv Java at 21:03
JTabbedPane Class
A tabbed panes is a component that appears
as a group of folders in a file cabinet. Each folder has a title. When a user
selects a folder, its contents become visible. Only one of the folders may be
selected at a time. Tabbed panes are commonly used for setting configuration
options.
Tabbed
panes are encapsulated by the JTabbedPanes class, which extends Jcomponent. We
will use its default constructor. Tabs are defined via the following method:
void addTab(String str, Component comp)
The
general procedure to use a tabbed pane in an applet is outlined here:
- Create
a JTabbedPane object.
- Call
addTab() to add a tab to the pane. (The arguments to this method define
the title of the tab and the component it contains.)
- Repeat
step 2 for each tab.
- Add
the tabbed pane to the content pane of the applet.
Example:
/* <APPLET
CODE = "JTabbedExample.CLASS" WIDTH = 500 HEIGHT = 500></APPLET>
*/
import javax.swing.*;
public class JTabbedExample extends JApplet
{
public void init()
{
JTabbedPane jtp = new
JTabbedPane();
jtp.addTab("Cities",
new CitiesPanel());
jtp.addTab("Colors",
new ColorsPanel());
jtp.addTab("Flavors",new
FlavorsPanel());
getContentPane().add(jtp);
}
}
class
CitiesPanel extends JPanel
{
public CitiesPanel()
{
JButton b1 = new
JButton("New York");
add(b1);
JButton b2 = new
JButton("London");
add(b2);
JButton b3 = new
JButton("Hong Kong");
add(b3);
JButton
b4 = new JButton("Tokyo");
add(b4);
}
}
class
ColorsPanel extends JPanel
{
public ColorsPanel()
{
JCheckBox cb1 = new
JCheckBox("Red");
add(cb1);
JCheckBox cb2 = new
JCheckBox("Green");
add(cb2);
JCheckBox cb3 = new
JCheckBox("Blue");
add(cb3);
}
class
FlavorsPanel extends JPanel
{
public FlavorsPanel()
{
JComboBox jcb = new
JComboBox();
jcb.addItem("Vanilla");
jcb.addItem("Chocolate");
jcb.addItem("Strawbarry");
add(jcb);
}
}
0 comments:
Post a Comment