Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/390.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将JPanel放入JTabbedPane大小?_Java_Swing_Jpanel - Fatal编程技术网

Java 如何将JPanel放入JTabbedPane大小?

Java 如何将JPanel放入JTabbedPane大小?,java,swing,jpanel,Java,Swing,Jpanel,我正在尝试将一个JPanel添加到另一个中,并使其适合父级的`JPanel大小 我有一个JPanel,其中包含一个JTable和一个JButton使用此代码: JScrollPane attributeTable = new JScrollPane(this); JPanel attributesPanel = new JPanel(); JPanel textFieldPanel=new JPanel(); JPanel buttonsPanel = new JPanel(); JBut

我正在尝试将一个
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
gridbagloayout
,但我无法正确显示它们,因为我遇到了同样的问题


提前感谢您。

您的示例不完整,很难判断错误,但这可能会有帮助:

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);

请编辑您的问题,以包括一个展示您描述的问题的,例如。