📄 tabbedpane.html
字号:
If you want similar functionalitywithout the tab interface,you can use a<a class="TutorialLink" target="_top" href="../layout/card.html">card layout</a> instead of a tabbed pane.<p><h4>To Create Tabbed Panes</h4>To create a tabbed pane,instantiate <code>JTabbedPane</code>,create the components you wish it to display,and then add the componentsto the tabbed paneusing the<code>addTab</code>method.<p>The following picture introduces an application that hasa tabbed panewith four tabs:<p><center><IMG SRC="../../figures/uiswing/components/TabbedPaneDemo.png" WIDTH="423" HEIGHT="117" ALIGN="BOTTOM" ALT="A screenshot of TabbedPaneDemo"></center></p><blockquote><hr><strong>Try this:</strong> <ol><li> <a href="http://java.sun.com/docs/books/tutorialJWS/uiswing/components/examples/TabbedPaneDemo.jnlp">Run TabbedPaneDemo</a> (it requires release 6) using<a class="TutorialLink" target="_top" href="../../information/javawebstart.html">Java<sup><font size=-2>TM</font></sup> Web Start</a>. Or, to compile and run the example yourself, consult the <a href="examples/index.html#TabbedPaneDemo">example index</a>.<li> Put the cursor over a tab. <br> The tool tip associated with the tab appears. As a convenience, you can specify tool tip text when you add a component to the tabbed pane.<li> Select a tab by clicking it. <br> The tabbed pane displays the component corresponding to the tab.<li> Select a tab by entering its mnemonic. <br> For example, in the Java look and feel you can select the tab labeled "Tab 3" by typing Alt-3.<li> Navigate between scrollable tabs. <br> This example provides scrollable tabs. Resize the dialog box by moving its left or right boundary so that tabs do not fit within the dialog. Scroll arrows appear next to the tabs. <br> Click the arrow to view one of the hidden tabs. <br> Note that clicking the arrow only reveals hidden tabs. It does not select a new tab.</ol><hr></blockquote><p>As the <code>TabbedPaneDemo</code> example shows,a tab can have a tool tip and a mnemonic,and it can display both text and an image.<h4>Tab Placement</h4>The default tab placement is set to the <code>TOP</code> location,as shown above.You can change the tab placement to <code>LEFT</code>, <code>RIGHT</code>,<code>TOP</code> or <code>BOTTOM</code>by using the <code>setTabPlacement</code> method.<p><h4>Code for Tabbed Panes</h4>The following code from<a class="SourceLink" target="_blank" href="examples/TabbedPaneDemo.java"><code>TabbedPaneDemo.java</code></a>creates the tabbed pane in the previous example.Note that no event-handling code is necessary.The <code>JTabbedPane</code> objecttakes care of mouse and keyboard events for you.<blockquote><pre>JTabbedPane tabbedPane = new JTabbedPane();ImageIcon icon = createImageIcon("images/middle.gif");JComponent panel1 = makeTextPanel("Panel #1");<b>tabbedPane.addTab("Tab 1", icon, panel1, "Does nothing");</b>tabbedPane.setMnemonicAt(0, KeyEvent.VK_1);JComponent panel2 = makeTextPanel("Panel #2");<b>tabbedPane.addTab("Tab 2", icon, panel2, "Does twice as much nothing");</b>tabbedPane.setMnemonicAt(1, KeyEvent.VK_2);JComponent panel3 = makeTextPanel("Panel #3");<b>tabbedPane.addTab("Tab 3", icon, panel3, "Still does nothing");</b>tabbedPane.setMnemonicAt(2, KeyEvent.VK_3);JComponent panel4 = makeTextPanel( "Panel #4 (has a preferred size of 410 x 50).");panel4.setPreferredSize(new Dimension(410, 50));<b>tabbedPane.addTab("Tab 4", icon, panel4, "Does nothing at all");</b>tabbedPane.setMnemonicAt(3, KeyEvent.VK_4);</pre></blockquote>As the previous code shows, the <code>addTab</code> method handles the bulk of the work in setting up a tabin a tabbed pane.The <code>addTab</code> method has several forms,but they all use both a string titleand the component to be displayed by the tab.Optionally, you can specify an icon and <a href="tooltip.html">tool tip</a> string.The text or icon (or both) can be null.Another way to create a tab is to use the<code>insertTab</code> method,which lets you specify the index of the tab you're adding.Note that the <code>addTab</code> method does not allowindex specification in this step.<p><h4>To Switch to Specific Tabs</h4>There are three ways to switch to specific tabs using GUI.<OL><LI><strong>Using a mouse.</strong>To switch to a specific tab,the user clicks it with the mouse.</LI><LI><strong>Using keyboard arrows.</strong>When the <code>JTabbedPane</code> object has the focus,the keyboard arrows can be used to switch from tab to tab.</LI><LI><strong>Using key mnemonics.</strong>The <code>setMnemonicAt</code> methodallows the user to switch to a specific tabusing the keyboard.For example, <code>setMnemonicAt(3, KeyEvent.VK_4)</code>makes '4' the mnemonicfor the fourth tab(which is at index 3,since the indices start with 0);pressing Alt-4 makes the fourth tab's component appear.Often, a mnemonicuses a character in the tab's titlethat is thenautomatically underlined.</LI></OL>To switch to a specific tab programmatically,use the<a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JTabbedPane.html#setSelectedIndex(int)"><code>setSelectedIndex</code></a> or the<a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JTabbedPane.html#setSelectedComponent(java.awt.Component)"><code>setSelectedComponent</code></a>methods.<p><h4>Preferred Size in Tabs</h4>When building components toadd to a tabbed pane,keep in mind that no matter whichchild of a tabbed pane is visible,each child gets the same amount of spacein which to display itself.The preferred size of the tabbed pane is just big enough to display its tallest child at its preferred height,and its widest child at its preferred width.Similarly,the minimum size of the tabbed pane depends onthe biggest minimum width and heightof all its children.<p>In the <code>TabbedPaneDemo</code> example,the fourth panel has a preferred width and heightthat are larger than those of the other panels.Thus, the preferred size of the tabbed paneis just big enough to display the fourth panelat its preferred size.Every panel gets exactly the same amount of space —410 pixels wide and 50 high,assuming the tabbed pane is at its preferred size.If you don't understand how preferred size is used,please refer to <a class="TutorialLink" target="_top" href="../layout/howLayoutWorks.html">How Layout Management Works</a>.</blockquote><h3>Tabs With Custom Components</h3><blockquote>The <code>TabComponentsDemo</code> example introduces a tabbed panewhose tabs contain real components.The use of custom components brings new featuressuch as buttons, combo boxes, labels and other componentsto tabs, and allows more complex user interaction.<p>Here is a tabbed panewith close buttons on its tabs.<p><center><IMG SRC="../../figures/uiswing/components/TabComponentsDemo.png" WIDTH="400" HEIGHT="200" ALIGN="BOTTOM" ALT="A screenshot of TabComponentsDemo"></center></p><blockquote><hr><strong>Try this:</strong> <ol><li> <a href="http://java.sun.com/docs/books/tutorialJWS/uiswing/components/examples/TabComponentsDemo.jnlp">Run TabComponentsDemo</a> (it requires release 6) using<a class="TutorialLink" target="_top" href="../../information/javawebstart.html">Java<sup><font size=-2>TM</font></sup> Web Start</a>. Or, to compile and run the example yourself, consult the <a href="examples/index.html#TabComponentsDemo">example index</a>.<br><li> Put the cursor over a tab. <li> Select a tab by clicking it (make sure not to hit the little cross). <li> Put the cursor over one of the widgets with a little cross. <br> The cross turns magenta and gets enclosed in a square. A tool tip associated with the close button appears. <br> Click the cross with the left mouse button to close the tab. <li> Restore the tabs that have been removed by choosing the Reset JTabbedPane item from the Options menu. <li> Note that tabs with custom components are displayed on top of original tabbed pane tabs. <br> To view the tabs underneath, open the Options menu and clear the Use TabComponents checkbox. <li> Display the tabs with components by selecting the Use TabComponents checkbox again. <li> Close all tabs. Now the tabbed pane is empty. </ol><hr></blockquote><h4>To Remove Tabs</h4>The following code from<a class="SourceLink" target="_blank" href="examples/ButtonTabComponent.java"><code>ButtonTabComponent.java</code></a>removes a tab from the tabbed pane. Note that event-handling code is necessary.Since each tab contains a real <code>JButton</code> object, you mustattach an <code>ActionListener</code> to the close button.As the user clicks the button,the <code>actionPerformed</code> methoddetermines the index of the tab it belongs toand removes the corresponding tab.<blockquote><pre>public void actionPerformed(ActionEvent e) { <b>int i = pane.indexOfTabComponent(ButtonTabComponent.this);</b> if (i != -1) { pane.remove(i); }}</pre></blockquote><h4>To Give Titles to Customized Tabs</h4>The code below, taken from<a class="SourceLink" target="_blank" href="examples/ButtonTabComponent.java"><code>ButtonTabComponent.java</code></a>, shows how a customized tab component getsa title from an original tabbed pane tab.<blockquote><pre>JLabel label = new JLabel(title) { public String getText() { int i = pane.indexOfTabComponent(ButtonTabComponent.this); if (i != -1) { return pane.getTitleAt(i); } return null; }};</pre></blockquote></blockquote><h3><a name="api">The Tabbed Pane API</a></h3><blockquote>The following tables list the commonly used<code>JTabbedPane</code> constructors and methods.The API for using tabbed panes falls into the following categories:<ul><li><a href="#creating">Creating and Setting Up a Tabbed Pane</a><li><a href="#tabapi">Inserting, Removing, Finding, and Selecting Tabs</a><li><a href="#appearanceapi">Changing Tab Appearance</a><li><a href="#tabscomponents">Setting Up Custom Components on Tabs</></ul><p><table border=1><caption><a name="creating"><b>Creating and Setting Up a Tabbed Pane</b></a></caption><tr><th align=left>Method or Constructor</th><th align=left>Purpose</th></tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JTabbedPane.html#JTabbedPane()">JTabbedPane()</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JTabbedPane.html#JTabbedPane(int)">JTabbedPane(int)</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JTabbedPane.html#JTabbedPane(int, int)">JTabbedPane(int, int)</a> </td> <td>Create a tabbed pane. The first optional argument specifies where the tabs should appear. By default, the tabs appear at the top of the tabbed pane. You can specify these positions (defined in the <code>SwingConstants</code> interface, which <code>JTabbedPane</code> implements): <code>TOP</code>, <code>BOTTOM</code>, <code>LEFT</code>, <code>RIGHT</code>. The second optional argument specifies the tab layout policy. You can specify one of these policies (defined in <code>JTabbedPane</code>):<a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JTabbedPane.html#WRAP_TAB_LAYOUT"><code>WRAP_TAB_LAYOUT</code></a> or<a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JTabbedPane.html#SCROLL_TAB_LAYOUT"><code>SCROLL_TAB_LAYOUT</code></a>. </td>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -