java Swing implements TAB functionality (JTabbedPane) instance code

  • 2020-05-19 04:47:27
  • OfStack

Swing implements TAB functionality (JTabbedPane)

Create the JTabbedPane object first, and the constructor can use JTabbedPane(int tabPlacement). tabPlacement is a field that JTabbedPane inherits from the interface javax.swing.SwingConstants. It could be BUTTOM,TOP, etc. The following code is shown:


JFrame jframe = new JFrame("TEST"); 
<a href="http://lib.csdn.net/base/docker" class='replace_word' title="Docker The knowledge base " target='_blank' style='color:#df3434; font-weight:bold;'>Container</a> c = jframe.getContentPane(); 
//  Create options pane ,  The Settings TAB is placed at the top  
JTabbedPane tabPane = new JTabbedPane(JTabbedPane.TOP); 
// Add to the window  
c.add(tabPane); 

The addTab function is called to add information to the TAB. addTab also has several, among which void addTab(String title, Component component) is simply used so that the TAB can have a title and an information component is added to the TAB. 1 general component is 1 Panel, put 1 Panel already designed into this TAB. The following code is shown:


//  new Panel , this Panel It should contain the information you want to display  
TabPanel tabPanel1 = new TabPanel (); 
//  add Panel Go to this TAB TAB1 In the  
tabPane.addTab("TAB1", tabPanel1); 
//  You can add multiple tabs based on this pattern  
...... 

Once you've added the code, you can choose which TAB to display by default using setSelectedIndex(int index). I select the first TAB and use the following code:


//  Select the first 1 Options page for the currently selected options page  
tabPane.setSelectedIndex(0); 

Thank you for reading, I hope to help you, thank you for your support of this site!


Related articles: