Java Swing GridBagLayout - 在面板上放置两个元素时出现问题

发布于 2024-10-05 00:58:27 字数 1567 浏览 4 评论 0原文

首先我将向您展示它是怎样的

picture_how_it_is

应该是这样的(请参阅我的评论中下面的链接):

标签必须向上,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--

您需要了解的事情:

  1. FestplattenreinigerGraphicalUserInterfaceTabbedPane 扩展了 JTabbedPane。
  2. 我尝试指定所有约束(即使具有默认值)来练习。
  3. 如果我推荐这些锚点,布局仍然是原来的样子。

这件事有什么问题吗?

非常感谢! (抱歉,由于我是新人,我无法直接发布图片 + 只有 1 个链接 -.-)

First i'll show you how it is:

picture_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:

  1. FestplattenreinigerGraphicalUserInterfaceTabbedPane extends JTabbedPane.
  2. I tried to specify all constraints (even with their default-val) to practice.
  3. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

东风软 2024-10-12 00:58:27

你遗漏了必要的weightx和weighty,否则所有东西都会集中在中间。所以在添加 tabpane 之前添加:

gbc.weightx = 1.0;
gbc.weighty = 1.0;

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:

gbc.weightx = 1.0;
gbc.weighty = 1.0;
往昔成烟 2024-10-12 00:58:27

要执行类似的操作,精确到您的示例,我认为您可以使用 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.

因为看清所以看轻 2024-10-12 00:58:27

不要使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文