java GridBagLayout 不工作?
java GridBagLayout not working?
我正在尝试使用 gridbagayout 来对齐 jtabbedpane 和 scrollpane。我试图将它们垂直对齐。然而,结果是横向的。有人为什么会这样吗?谢谢。
这是实际结果:
这是我的代码:
JTabbedPane tabs= new JTabbedPane();
tabs.setOpaque(false);
JScrollPane textScrollPane= new JScrollPane();
textArea= new JTextArea();
textArea.setEditable(false);
DefaultCaret caret = (DefaultCaret)textArea.getCaret();
caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
textScrollPane.setViewportView(textArea);
GridBagConstraints c = new GridBagConstraints();
{
c.fill = GridBagConstraints.BOTH;
c.gridx = 0;
c.gridy = 0;
c.weightx=1;
c.gridheight=4;
panel.add(tabs,c);
c.fill = GridBagConstraints.BOTH;
c.gridx=0;
c.gridy=1;
c.weightx=1;
c.gridheight=1;
c.ipadx=20;
c.ipady=10;
panel.add(textScrollPane,c);
/////restore to default
c.gridheight=1;
c.ipadx=0;
c.ipady=0;
}
However, the result was horizontal. Does anyone why this happened?
可能是因为 JPanel 的默认布局管理器是 FlowLayout
而您在创建面板时没有更改布局管理器。 FlowLayout
水平显示组件。
创建面板时还需要:
JPanel panel = new JPanel( new GridBagLayout() );
我正在尝试使用 gridbagayout 来对齐 jtabbedpane 和 scrollpane。我试图将它们垂直对齐。然而,结果是横向的。有人为什么会这样吗?谢谢。
这是实际结果:
这是我的代码:
JTabbedPane tabs= new JTabbedPane();
tabs.setOpaque(false);
JScrollPane textScrollPane= new JScrollPane();
textArea= new JTextArea();
textArea.setEditable(false);
DefaultCaret caret = (DefaultCaret)textArea.getCaret();
caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
textScrollPane.setViewportView(textArea);
GridBagConstraints c = new GridBagConstraints();
{
c.fill = GridBagConstraints.BOTH;
c.gridx = 0;
c.gridy = 0;
c.weightx=1;
c.gridheight=4;
panel.add(tabs,c);
c.fill = GridBagConstraints.BOTH;
c.gridx=0;
c.gridy=1;
c.weightx=1;
c.gridheight=1;
c.ipadx=20;
c.ipady=10;
panel.add(textScrollPane,c);
/////restore to default
c.gridheight=1;
c.ipadx=0;
c.ipady=0;
}
However, the result was horizontal. Does anyone why this happened?
可能是因为 JPanel 的默认布局管理器是 FlowLayout
而您在创建面板时没有更改布局管理器。 FlowLayout
水平显示组件。
创建面板时还需要:
JPanel panel = new JPanel( new GridBagLayout() );