gpt4 book ai didi

java GridBagLayout 不工作?

转载 作者:行者123 更新时间:2023-11-30 06:28:20 25 4
gpt4 key购买 nike

我正在尝试使用 gridbagayout 来对齐 jtabbedpane 和滚动 Pane 。我尝试将它们垂直对齐。然而,结果却是水平的。有人知道为什么会这样吗?谢谢。

这是实际结果: enter image description here

这是我的代码:

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

关于java GridBagLayout 不工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46648497/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com