Java Swing GridBagLayout - 在面板上放置两个元素时出现问题
首先我将向您展示它是怎样的:
应该是这样的(请参阅我的评论中下面的链接):
标签必须向上,TabPane 必须在所有方向上用边距填充屏幕的其余部分。
这是使用 GridBagLayout 进行布局的代码:
// Layout --begin--
this.mainPanel.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
// Layout:headLineLabel --begin--
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.ipadx = 0;
gbc.ipady = 0;
gbc.insets = new Insets(0, 10, 0, 0);
gbc.anchor = GridBagConstraints.FIRST_LINE_START;
this.mainPanel.add(this.headLineLabel, gbc);
// Layout:headLineLabel --end--
// Layout:FestplattenreinigerGraphicalUserInterfaceTabbedPane --begin--
gbc.gridx = 0;
gbc.gridy = 1;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.fill = GridBagConstraints.BOTH;
gbc.ipadx = 0;
gbc.ipady = 0;
gbc.insets = new Insets(10, 10, 10, 10);
gbc.anchor = GridBagConstraints.CENTER;
this.mainPanel.add(new FestplattenreinigerGraphicalUserInterfaceTabbedPane(), gbc);
// Layout:FestplattenreinigerGraphicalUserInterfaceTabbedPane --end--
// Layout --end--
您需要了解的事情:
- FestplattenreinigerGraphicalUserInterfaceTabbedPane 扩展了 JTabbedPane。
- 我尝试指定所有约束(即使具有默认值)来练习。
- 如果我推荐这些锚点,布局仍然是原来的样子。
这件事有什么问题吗?
非常感谢! (抱歉,由于我是新人,我无法直接发布图片 + 只有 1 个链接 -.-)
First i'll show you how it is:
This is how it should be (see link below in my comment):
Label has to be up and TabPane has to fill the rest of the screen with margins in all directions.
This is the code for laying it out with GridBagLayout:
// Layout --begin--
this.mainPanel.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
// Layout:headLineLabel --begin--
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.ipadx = 0;
gbc.ipady = 0;
gbc.insets = new Insets(0, 10, 0, 0);
gbc.anchor = GridBagConstraints.FIRST_LINE_START;
this.mainPanel.add(this.headLineLabel, gbc);
// Layout:headLineLabel --end--
// Layout:FestplattenreinigerGraphicalUserInterfaceTabbedPane --begin--
gbc.gridx = 0;
gbc.gridy = 1;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.fill = GridBagConstraints.BOTH;
gbc.ipadx = 0;
gbc.ipady = 0;
gbc.insets = new Insets(10, 10, 10, 10);
gbc.anchor = GridBagConstraints.CENTER;
this.mainPanel.add(new FestplattenreinigerGraphicalUserInterfaceTabbedPane(), gbc);
// Layout:FestplattenreinigerGraphicalUserInterfaceTabbedPane --end--
// Layout --end--
Things you need to know:
- FestplattenreinigerGraphicalUserInterfaceTabbedPane extends JTabbedPane.
- I tried to specify all constraints (even with their default-val) to practice.
- If I commend out the achors, layout remains how it is.
What's wrong in this thing?
THX very much! (sorry i couldnt post images directly + only 1 link due to i'm new -.-)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你遗漏了必要的weightx和weighty,否则所有东西都会集中在中间。所以在添加 tabpane 之前添加:
You have left out weightx and weighty which are necessary or everything will just lump together in the middle. So before adding the tabpane add:
要执行类似的操作,精确到您的示例,我认为您可以使用 NetBeans 中的“自由设计”而不是 GridBagLayout,然后限制调整应用程序窗口大小的能力。
To do something like that,exact to your sample, I think you can use the 'Free Design' in NetBeans rather than GridBagLayout and then restrict the ability to resize the application window.
不要使用 GridBagLayout。尝试使用边框布局。标签位于北部,选项卡式窗格位于中心。
或者也许是一个垂直的 BoxLayout。
Don't use a GridBagLayout. Try a BorderLayout. The label in the NORTH and the tabbed pane in the CENTER.
Or maybe a vertical BoxLayout.