2013-02-10 104 views
0

我试图将JPanel添加到另一个中,并使其适合父`JPanel大小。如何将JPanel放入JTabbedPane大小?

我有一个JPanel其中包含使用该码的JTableJButton

JScrollPane attributeTable = new JScrollPane(this); 

JPanel attributesPanel = new JPanel(); 

JPanel textFieldPanel=new JPanel(); 
JPanel buttonsPanel = new JPanel(); 

JButton newAttributeButton = new JButton("New Attribute"); 

attributesPanel.add(textFieldPanel, BorderLayout.CENTER); 
attributesPanel.add(buttonsPanel, BorderLayout.SOUTH); 

newAttributeButton.addActionListener(AttributesTableController.getInstance()); 

GroupLayout layout = new GroupLayout(textFieldPanel); 
textFieldPanel.setLayout(layout); 

// Turn on automatically adding gaps between components 
layout.setAutoCreateGaps(true); 

// Turn on automatically creating gaps between components that touch 
// the edge of the container and the container. 
layout.setAutoCreateContainerGaps(true); 

// Create a sequential group for the horizontal axis. 

GroupLayout.SequentialGroup hGroup = layout.createSequentialGroup(); 

// The sequential group in turn contains two parallel groups. 
// One parallel group contains the labels, the other the text fields. 
// Putting the labels in a parallel group along the horizontal axis 
// positions them at the same x location. 
// 
// Variable indentation is used to reinforce the level of grouping. 
hGroup.addGroup(layout.createParallelGroup(). 
     addComponent(attributeTable).addComponent(newAttributeButton)); 
layout.setHorizontalGroup(hGroup); 

// Create a sequential group for the vertical axis. 
GroupLayout.SequentialGroup vGroup = layout.createSequentialGroup(); 
vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).addComponent(attributeTable)); 
vGroup.addGroup(layout.createParallelGroup(Alignment.CENTER). 
     addComponent(newAttributeButton)); 
layout.setVerticalGroup(vGroup); 

当我有此面板(名为attributesPanel)成JTabbedPane的一个选项卡,它只是显示JTableJButton ,但在面板的中心。我希望attributesPanel的尺寸与打开标签的尺寸相同。

这是我使用到的JPanel添加到我的JTabbedPane的代码:

TabbedPane tabbedPane = new TabbedPane(); 
tabbedPane.setComponentAt(1, attributesPanel); 

我使用GridLayout尝试,它安装在JPanel很好,但我没能调整按钮。我尝试了FlowLayoutGridBagLayout,但我无法正确显示它们,因为我遇到了同样的问题。

预先感谢您。

+0

请编辑您的问题包括[SSCCE(http://sscce.org/)展示您描述的问题,例如(http://stackoverflow.com/a/11949899/230513)。 – trashgod 2013-02-10 16:40:41

回答

1

你举的例子是不完整的SOP这是很难说什么是错的,但也许这将帮助:

JTable table = new JTable (12, 5); 
JButton button = new JButton ("Button"); 

JPanel panel = new JPanel(); 
panel.setLayout (new BorderLayout()); 
panel.add (table, BorderLayout.CENTER); 
panel.add (button, BorderLayout.SOUTH); 

JTabbedPane tabbedPane = new JTabbedPane(); 
tabbedPane.addTab ("Tab", panel); 

JFrame frame = new JFrame(); 
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); 
frame.getContentPane().setLayout (new BorderLayout()); 
frame.getContentPane().add (tabbedPane, BorderLayout.CENTER); 
frame.pack(); 
frame.setVisible (true); 
+0

它运作良好,谢谢 – Chewbye 2013-02-11 14:34:49