24、Java Swing JTabbedPane:选项卡组件
使用選項卡可以在有限的布局空間內(nèi)展示更多的內(nèi)容。Swing 使用 JTabbedPane 類實現(xiàn)選項卡。
JTabbedPane 類創(chuàng)建的選項卡可以通過單擊標(biāo)題或者圖標(biāo)在選項卡之間進(jìn)行切換。
JTabbedPane 類的常用構(gòu)造方法:
- JTabbedPane():創(chuàng)建一個具有默認(rèn) JTabbedPane.TOP 布局的空 TabbedPane。
- JTabbedPane(int tabPlacement):創(chuàng)建一個空的TabbedPane,使其具有以下指定選項卡布局中的一種:JTabbedPane.TOP、JTabbedPane.BOTTOM、JTabbedPane.LEFT
或 JTabbedPane.RIGHT。
創(chuàng)建了 JTabbedPane 實例之后,可使用 addTab() 方法和 insertTab() 方法將選項卡/組件添加到 TabbedPane 對象中。選項卡通過對應(yīng)于添加位置的索引來表示,其中第一個選項卡的索引為 0,最后一個選項卡的索引為選項卡數(shù)量減 1。
TabbedPane 使用 SingleSelectionModel 屬性來表示選項卡索引集和當(dāng)前所選擇的索引。如果選項卡數(shù)量大于 0,則總會有一個被選定的索引,此索引默認(rèn)被初始化為第一個選項卡;如果選項卡數(shù)量為 0,則所選擇的索引為 -1。
JTabbedPane類的常用方法:
- add(Component component,int index) ----在指定的選項卡索引位置添加一個 component,默認(rèn)的選項卡標(biāo)題為組件名稱
- addTab(String title, Component component) ----添加一個使用 title 作為標(biāo)題,且沒有圖標(biāo)的組件
- getComponentAt(int index) ----返回 index 位置的組件
- getSelectedComponent() ----返回此選項卡窗格當(dāng)前選擇的組件
- getSelectedIndex() ----返回當(dāng)前選擇的此選項卡窗格的索引
- getTabCount() ----返回此 tabbedPane 的選項卡數(shù)
- insertTab(String title,Icon icon,Component component, String tip,int index)---- 在 index 位置插入一個組件,該組件通過 title 或 icon(任意一個 都可以為 null)來表示
- isEnabledAt(int index) ----返回當(dāng)前是否啟用了 index 位置的選項卡
- remove(int index)---- 移除對應(yīng)于指定索引的選項卡和組件
- setEnabledAt(int index,boolean enabled)---- 設(shè)置是否啟用 index 位置的選項卡
- setMnemonicAt(int tablndex,int mnemonic) ----設(shè)置用于訪問指定選項卡的鍵盤助記符
- setTitleAt(int index,String title)---- 將 index 位置的標(biāo)題設(shè)置為 title,它可以為 null
- setToolTipTextAt(int index,String toolTipText) ----將 index 位置的工具提示文本設(shè)置為
toolTipText,它可以為 null
選項卡面板和卡片布局不同的是,選項卡面板可以有標(biāo)簽。下面的示例代碼創(chuàng)建了一個選項卡面板,并在選項卡面板中添加了一個 JPand 面板。
JTabbedPane tabbedPane=new JTabbedPane(); ImageIcon icon=new ImageIcon("temp.gif"); JComponent panel1=makeTextPanel("Panel#1"); //創(chuàng)建一個jPanel容器,容納其他組件 tabbedPane.addTab("Tab 1",icon,panel1,"Does nothing"); tabbedPane.setMnemonicAt(0,KeyEvent.VK_1); //設(shè)置快捷鍵該代碼段中的第三條語句向選項卡面板 tabbedPane 中添加了一個 panel1 組件(該組件是一個 JPanel 對象),該方法中的第一個參數(shù)是選項卡標(biāo)簽文本;第二個參數(shù)是 Icon 對象,它作為選項卡標(biāo)簽上的圖標(biāo);第三個參數(shù)是添加到選項卡上的組件;第四個參數(shù)是當(dāng)鼠標(biāo)指針放在選項卡標(biāo)簽上時出現(xiàn)的提示信息。
例 1
使用 JTabbedPane 類創(chuàng)建一個包含 4 個標(biāo)簽的選項卡面板,并分別為每個選項卡填充內(nèi)容,主要實現(xiàn)過程如下。
(1) 創(chuàng)建一個 TabbedPaneDemo 類并繼承 JPanel 父類。
import javax.swing.JPanel; import javax.swing.*; import java.awt.*; import java.awt.event.KeyEvent; public class TabbedPaneDemo extends JPanel {public static void main(String[] args){JFrame frame=new JFrame("我的電腦 - 屬性");frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.add(new TabbedPaneDemo(),BorderLayout.CENTER);frame.pack();frame.setVisible(true);} }(2) 在構(gòu)造方法中創(chuàng)建空白選項卡,新建標(biāo)簽,指定標(biāo)簽的文本和圖片,以及標(biāo)簽對應(yīng)的內(nèi)容面板和快捷訪問符。這些代碼如下:
public TabbedPaneDemo() {super(new GridLayout(1,1));JTabbedPane tabbedPane=new JTabbedPane();ImageIcon icon=createImageIcon("tab.jp1g");JComponent panel1=makeTextPanel("計算機(jī)名");tabbedPane.addTab("計算機(jī)名",icon, panel1,"Does nothing");tabbedPane.setMnemonicAt(0,KeyEvent.VK_1);JComponent panel2=makeTextPanel("硬件");tabbedPane.addTab("硬件",icon,panel2,"Does twice as much nothing");tabbedPane.setMnemonicAt(1,KeyEvent.VK_2);JComponent panel3=makeTextPanel("高級");tabbedPane.addTab("高級",icon,panel3,"Still does nothing");tabbedPane.setMnemonicAt(2,KeyEvent.VK_3);JComponent panel4=makeTextPanel("系統(tǒng)保護(hù)");panel4.setPreferredSize(new Dimension(410,50));tabbedPane.addTab("系統(tǒng)保護(hù)",icon,panel4,"Does nothing at all");tabbedPane.setMnemonicAt(3,KeyEvent.VK_4);add(tabbedPane); }(3) 編寫 makeTextPane() 方法的實現(xiàn),該方法主要用于創(chuàng)建一個內(nèi)容面板,并在面板上添加指定的文本內(nèi)容,具體實現(xiàn)代碼如下所示。
protected JComponent makeTextPanel(String text) {JPanel panel=new JPanel(false);JLabel filler=new JLabel(text);filler.setHorizontalAlignment(JLabel.CENTER);panel.setLayout(new GridLayout(1,1));panel.add(filler);return panel; }(4) 構(gòu)造方法中用到的 createImageIcon() 方法主要用于加載圖片,如果圖片不存在則返回 null。
protected static ImageIcon createImageIcon(String path) {java.net.URL imgURL=TabbedPaneDemo.class.getResource(path);if(imgURL!=null){return new ImageIcon(imgURL);}else{System.err.println("Couldn't find file: "+path);return null;} }(5) 運(yùn)行程序,結(jié)果如圖 1 所示。假設(shè)存在構(gòu)造方法中指定的圖片,那么在圖 1 所示的運(yùn)行效果中,每個單擊選項卡標(biāo)簽前都帶有各自的圖標(biāo)。
總結(jié)
以上是生活随笔為你收集整理的24、Java Swing JTabbedPane:选项卡组件的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 23、Java Swing JTree:
- 下一篇: 25、Java Swing文本编辑器的实