[英]How to fit a JPanel into a JTabbedPane size?
我试图将一个JPanel
添加到另一个中,以使其适合父`JPanel大小。
我有一个JPanel
,其中包含使用以下代码的JTable
和JButton
:
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
的选项卡中时,它仅显示JTable
和JButton
,但位于面板的中央。 我希望attributesPanel
的尺寸与打开的选项卡的尺寸相同。
这是我用来将JPanel添加到我的JTabbedPane中的代码:
TabbedPane tabbedPane = new TabbedPane();
tabbedPane.setComponentAt(1, attributesPanel);
我尝试使用GridLayout
,它很好地适合了JPanel
但是无法调整按钮的大小。 我尝试使用FlowLayout
和GridBagLayout
,但由于存在相同的问题,因此无法正确显示它们。
先感谢您。
您的示例是不完整的浸液,很难分辨出哪里出了问题,但这也许会有所帮助:
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.