一列布局不使用窗口的整个宽度

发布于 2024-12-25 05:35:16 字数 605 浏览 1 评论 0原文

我想创建一个布局:2 行,1 列。第一行应占据窗口高度的 70%,第二行应占据窗口高度的 30%。我通过使用 GridBagConstraintsweighty 属性来实现此目的。

但是,我对组件的宽度有问题,因为当我调整应用程序窗口大小时,组件保持在中心,其宽度是恒定的,并且组件的左侧和右侧有空格(即使我设置了 填充BOTH)。当我更改窗口的高度时,不会出现此问题(组件可以很好地调整大小并填充窗口的整个高度)。

以下是我的限制:

firstConstraints.gridx = 0;
firstConstraints.gridy = 0;  
firstConstraints.weighty = 0.7;
firstConstraints.fill = GridBagConstraints.BOTH;

secondConstraints.gridx = 0;
secondConstraints.gridy = 1;  
secondConstraints.weighty = 0.3;
secondConstraints.fill = GridBagConstraints.BOTH;

I would like to create a layout: 2 rows, 1 column. 1st row should occupy 70% height of the window and 2nd row 30% of the window. I achieve this by using weighty attribute of GridBagConstraints.

However I have problem with the width of my component, because when I resize application window the component remain in the center, its width is constant and I get a white spaces in the left and right of the component (even if I set fill to BOTH). This problem does not occur when I change the height of the window (components resize very well and fill full height of the window).

Below my constraints:

firstConstraints.gridx = 0;
firstConstraints.gridy = 0;  
firstConstraints.weighty = 0.7;
firstConstraints.fill = GridBagConstraints.BOTH;

secondConstraints.gridx = 0;
secondConstraints.gridy = 1;  
secondConstraints.weighty = 0.3;
secondConstraints.fill = GridBagConstraints.BOTH;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

美煞众生 2025-01-01 05:35:16

我认为您还需要:

gbc.weightx = 1.0;

请参阅 Swing 教程中关于 如何使用一个 GrigBagLayout 讨论了weightx,权重约束。

I think you also need:

gbc.weightx = 1.0;

See the secton from the Swing tutorial on How to Use a GrigBagLayout that talks about the weightx, weighty constraints.

过期以后 2025-01-01 05:35:16

简单的例子

import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;

public class BorderPanels extends JFrame {

    private static final long serialVersionUID = 1L;

    public BorderPanels() {
        getContentPane().setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        JPanel panel1 = new JPanel();
        Border eBorder = BorderFactory.createEtchedBorder();
        panel1.setBorder(BorderFactory.createTitledBorder(eBorder, "70pct"));
        gbc.gridx = gbc.gridy = 0;
        gbc.gridwidth = gbc.gridheight = 1;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.anchor = GridBagConstraints.NORTHWEST;
        gbc.weightx = gbc.weighty = 70;
        getContentPane().add(panel1, gbc);
        JPanel panel2 = new JPanel();
        panel2.setBorder(BorderFactory.createTitledBorder(eBorder, "30pct"));
        gbc.gridy = 1;
        gbc.weightx = 30;
        gbc.weighty = 30;
        gbc.insets = new Insets(2, 2, 2, 2);
        getContentPane().add(panel2, gbc);
        pack();
    }

    public static void main(String[] args) {
        new BorderPanels().setVisible(true);
    }
}

simple example

import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;

public class BorderPanels extends JFrame {

    private static final long serialVersionUID = 1L;

    public BorderPanels() {
        getContentPane().setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        JPanel panel1 = new JPanel();
        Border eBorder = BorderFactory.createEtchedBorder();
        panel1.setBorder(BorderFactory.createTitledBorder(eBorder, "70pct"));
        gbc.gridx = gbc.gridy = 0;
        gbc.gridwidth = gbc.gridheight = 1;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.anchor = GridBagConstraints.NORTHWEST;
        gbc.weightx = gbc.weighty = 70;
        getContentPane().add(panel1, gbc);
        JPanel panel2 = new JPanel();
        panel2.setBorder(BorderFactory.createTitledBorder(eBorder, "30pct"));
        gbc.gridy = 1;
        gbc.weightx = 30;
        gbc.weighty = 30;
        gbc.insets = new Insets(2, 2, 2, 2);
        getContentPane().add(panel2, gbc);
        pack();
    }

    public static void main(String[] args) {
        new BorderPanels().setVisible(true);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文