2016-07-26 66 views
0

我正在使用JTabbedPane,並且我想向選項卡添加一個關閉按鈕。如何在選項卡中添加關閉按鈕,JTabbedPane

我創建了一個JButton,帶有一個:ActionListener,當你點擊的時候,這個選項卡將被逐個打開。

而且,我創建了一個JTabbedPane中......

//Tabbed. 
JTabbedPane Tabbedr = new JTabbedPane(); 
File F = new File("HTML1.html"); 
Frame.getContentPane().add(Tabbedr); 
Tabbedr.addTab(F.getName()); 

//Button. 
JButton Close = new JButton(); 
Close.addActionListener(new ActionListener(){ 
public void actionPerformed(ActionEvent a){ 
Tabbedr.remove(1);}}); 

所以,在這裏所有的罰款,但我不知道如何添加將JButton「關閉」,我的標籤。正如在一些着名的編輯,

要測試按鈕,我把它添加到一個工具欄,但我想把這裏面的標籤。例如:你好,txt「X」。

¿有人可以幫助我嗎?感謝您閱讀和您的時間。

+0

如果這不是['ButtonTabComponent'](https://docs.oracle.com/javase/tutorial/uiswing/components/tabbedpane.html)的重複,請編輯您的問題以包含[mcve]顯示你目前的做法。 – trashgod

回答

1
public class CustomTabbedPane extends JTabbedPane { 

    public CustomTabbedPane() { 

     // Add panel to tab screen 
     JPanel tab1Panel = new JPanel(); 
     addTab(null, null, tab1Panel, ""); 

     // Add label and button to the tab name 
     JPanel tab1 = new JPanel(); 
     temp.add(new JLabel("Tab Name")); 
     temp.add(new JButton("x")); 
     setTabComponentAt(0, tab1); 
    } 
} 

當然,你想要添加一個ActionListener到JButton並配置它,以便在按下按鈕時該選項卡消失。

爲了使它看起來不錯,我建議你擺脫對JButton的邊距,

button.setMargin(new Insets(0, 0, 0, 0)) 

刪除邊框,

button.setBorder(BorderFactory.createEmptyBorder()); 

,並設置TAB1的JPanel到「空」佈局來手動放置「x」按鈕和標籤。

相關問題