2011-03-29 20 views
3

因此,我偶然發現了JTabbedPane中標籤的位置左右(即setTabPlacement(JTabbedPane.RIGHT)),我喜歡它的外觀。我需要的是利用標籤下留下的空間。我目前有一列JButton,但是它們被推到一邊,留下了很多空白區域。如何將組件放置在正確方向的JTabbedPane中的標籤下

有關如何做到這一點的任何想法?某種自定義覆蓋或什麼?

Here's a screenshot。在代碼中,我基本上有一個水平對齊的Box,JTabbedPane覆蓋JTree,然後是後面的按鈕列。

boxOfEverything.add(tabbedPane); 
boxOfEverything.add(boxColumnButtons); 

Screenshot here

+0

請張貼圖片和代碼。 – Manoj 2011-03-29 08:47:02

回答

1

我做了這個community wiki,因爲這個答案不是我的。 @cheesecamera似乎已經在另一個forum上發佈了相同的問題,並在那裏得到了答案。我複製了答案,以便來這裏尋找答案的人可以得到答案。

這個想法是使用swing的glasspane

import java.awt.*; 
import javax.swing.*; 

public class RightTabPaneButtonPanel { 

    public static void main(String[] args) { 
    SwingUtilities.invokeLater(new Runnable() { 

     @Override 
     public void run() { 
     new RightTabPaneButtonPanel().makeUI(); 
     } 
    }); 
    } 

    public void makeUI() { 
    JTabbedPane tabbedPane = new JTabbedPane(); 
    tabbedPane.setTabPlacement(JTabbedPane.RIGHT); 
    JPanel panel = new JPanel(new GridLayout(0, 1)); 

    for (int i = 0; i < 3; i++) { 
     JPanel tab = new JPanel(); 
     tab.setName("tab" + (i + 1)); 
     tab.setPreferredSize(new Dimension(400, 400)); 
     tabbedPane.add(tab); 

     JButton button = new JButton("B" + (i + 1)); 
     button.setMargin(new Insets(0, 0, 0, 0)); 
     panel.add(button); 
    } 

    JFrame frame = new JFrame(); 
    frame.add(tabbedPane); 
    frame.pack(); 
    Rectangle tabBounds = tabbedPane.getBoundsAt(0); 

    Container glassPane = (Container) frame.getGlassPane(); 
    glassPane.setVisible(true); 
    glassPane.setLayout(new GridBagLayout()); 
    GridBagConstraints gbc = new GridBagConstraints(); 
    gbc.weightx = 1.0; 
    gbc.weighty = 1.0; 
    gbc.fill = GridBagConstraints.NONE; 
    int margin = tabbedPane.getWidth() - (tabBounds.x + tabBounds.width); 
    gbc.insets = new Insets(0, 0, 0, margin); 
    gbc.anchor = GridBagConstraints.SOUTHEAST; 

    panel.setPreferredSize(new Dimension((int) tabBounds.getWidth() - margin, 
      panel.getPreferredSize().height)); 
    glassPane.add(panel, gbc); 

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setLocationRelativeTo(null); 
    frame.setVisible(true); 
    } 
} 
相關問題