This article link: http://blog.csdn.net/xietansheng/article/details/74366541
Java Swing Graphical Interface Development (TOC) 1. Overview
Official JavaDocsApi:javax.swing.JTabbedPane
JTabbedPane, tab panel. It allows the user to toggle the display between a set of components by clicking on the tab of a given caption or icon.
Common construction Methods :
/**
* Supervisor Note:
* tabplacement: tab title position, value is Jtabbedpane.top, Jtabbedpane.bottom,
* Jtabbedpane.left or Jtabbedpane.right, the default is top
*
* tablayoutpolicy: When the tab position cannot be placed in all tabs, the policy of placing the tab,
* value is jtabbedpane.wrap_tab_layout or jtabbedpane.scroll_tab_layout
*/
JTabbedPane ()
JTabbedPane ( int tabplacement)
jtabbedpane (int tabplacement, int tablayoutpolicy)
To Create a tab :
/**
* Description:
* Title : Tab title
* Icon : tab of the icons
* tip: When the mouse moves to the tab, the text of the hover hint
* Component: The content component that is displayed when this tab is selected/
void AddTab (string title, component component)
void AddTab (string title, Icon icon, Component Component)
void AddTab (string title, Icon icon, Component Component, String tip)
/**
* Inserts a tab at the specified index
/void InsertTab (string title, Icon icon, Component Component, string tip, int index)
To Remove a tab :
Removes the option at the specified location (two methods are exactly the same)
void Removetabat (int index)
void Remove (int index)
//Remove tab
Void for the specified content control Remove (Component Component)
//Remove all tab
void RemoveAll ()
tab Related actions :
Sets the currently selected tab
void setSelectedIndex (int index)
//Gets the currently selected tab index
int getselectedindex ()
// Gets the content component for the currently selected tab
Component getselectedcomponent ()
//Set the title of the tab at the index position
void settitleat (int index, String title
///Set the Index Position tab's icon
void seticonat (int index, icon icon)
//Set the tab at index position available
void Sete Nabledat (int index, Boolean enabled)
//sets the content component of the index position to component
void Setcomponentat (int index, component C omponent)
//Get the number of tabs
int gettabcount ()
/**
* Custom Tab header position components, where titlecomponent will be placed in the tab header position.
* Application Scenario: For example, add a Close button to the tab title position and click the button to remove the tab.
*
/void Settabcomponentat (int index, Component titlecomponent)
Status Listener :
Add a tab Select state Change listener
void Addchangelistener (ChangeListener l)
2. code example
Package Com.xiets.demo;
Import javax.swing.*;
Import javax.swing.event.ChangeEvent;
Import Javax.swing.event.ChangeListener;
Import java.awt.*;
public class Main {public static void main (string[] args) {JFrame JF = new JFrame ("Test Window");
Jf.setsize (300, 300);
Jf.setdefaultcloseoperation (Windowconstants.exit_on_close);
Jf.setlocationrelativeto (NULL);
Create the TAB panel final JTabbedPane tabbedpane = new JTabbedPane ();
Create a 1th Tab (tab contains only headings) Tabbedpane.addtab ("Tab01", Createtextpanel ("tab 01"));
Create a 2nd Tab (tab contains title and icon) Tabbedpane.addtab ("Tab02", New ImageIcon ("Bb.jpg"), Createtextpanel ("tab 02"); Create a 3rd Tab (tab contains title, Icon, and Tip tip) Tabbedpane.addtab ("Tab03", New ImageIcon ("Bb.jpg"), Createtextpanel ("tab 0
3 ")," This is a tab. "); Add tab Select State Change Listener tabbedpane.addchangelistener (new ChangeListener () {@Override public vo ID statechanged (ChangeevenT e) {System.out.println ("Currently selected tab:" + Tabbedpane.getselectedindex ());
}
});
Set the default selected tab Tabbedpane.setselectedindex (1);
Jf.setcontentpane (TabbedPane);
Jf.setvisible (TRUE);
/** * Create a panel with a tab center displaying a label that indicates what a tab needs to display/private static jcomponent Createtextpanel (String text) {
Create a panel, using a grid layout of 1 rows and 1 columns (in order to give the label a wide-high automatic brace) JPanel panel = new JPanel (new GridLayout (1, 1));
Create label JLabel label = new JLabel (text);
Label.setfont (new Font (null, Font.plain, 50));
Label.sethorizontalalignment (Swingconstants.center);
Add tags to panel panel.add (label);
return panel; }
}
Results show:
PS: The screenshot of the test is a screenshot that runs in the MAC OS X environment, which can be significantly different in the Windows environment.