尝试使用GridBaglayout来管理Java中的Jpanels

发布于 2025-01-24 11:44:33 字数 1615 浏览 2 评论 0原文

我目前正在使用Guis迈出第一步,现在正试图根据我的艺术品在此处使用两个Jprame使用两个Jpanel容器:

​作品。

我当前的代码看起来像这样:

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

public class MyFrame extends JFrame {

     private JPanel contentPane;
         
    public JPanel getContentPane() {
        return contentPane;
    }

    public void setContentPane(JPanel contentPane) {
        this.contentPane = contentPane;
    }

    public MyFrame (){

        contentPane = new JPanel();
        contentPane.setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();

        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.gridwidth = 2;
        gbc.gridheight = 3;
        JPanel blue = new JPanel();
        blue.setBackground(Color.BLUE);

        contentPane.add(blue, gbc);


        gbc.gridx = 2;
        gbc.gridy = 0;
        gbc.gridwidth = 1;
        gbc.gridheight = 3;
        JPanel gruen = new JPanel();
        gruen.setBackground(Color.green);
        contentPane.add(gruen, gbc);

        this.setPreferredSize(new Dimension(630, 650));
        this.pack();
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setVisible(true);
        this.setContentPane(contentPane);
    }

    public static void main(String[] args) {
        new MyFrame();
    }
}

给我这个空窗口:

I am currently making my first steps with GUIs and am right now just trying to make a JFrame with two JPanel containers according to my work of art here:

https://i.sstatic.net/5wq1f.png

I chose the GridBagLayout since I want it to be resizeable later but keep the given proportions, yet I'm having a hard time understanding how all of this works.

My current code looks like this:

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

public class MyFrame extends JFrame {

     private JPanel contentPane;
         
    public JPanel getContentPane() {
        return contentPane;
    }

    public void setContentPane(JPanel contentPane) {
        this.contentPane = contentPane;
    }

    public MyFrame (){

        contentPane = new JPanel();
        contentPane.setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();

        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.gridwidth = 2;
        gbc.gridheight = 3;
        JPanel blue = new JPanel();
        blue.setBackground(Color.BLUE);

        contentPane.add(blue, gbc);


        gbc.gridx = 2;
        gbc.gridy = 0;
        gbc.gridwidth = 1;
        gbc.gridheight = 3;
        JPanel gruen = new JPanel();
        gruen.setBackground(Color.green);
        contentPane.add(gruen, gbc);

        this.setPreferredSize(new Dimension(630, 650));
        this.pack();
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setVisible(true);
        this.setContentPane(contentPane);
    }

    public static void main(String[] args) {
        new MyFrame();
    }
}

Which gives me this empty window:

https://i.sstatic.net/jmIo5.png

If someone could tell me what I'm doing wrong that would be great.

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

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

发布评论

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

评论(1

喜你已久 2025-01-31 11:44:33

1。首先,您需要删除以下内容:

public JPanel getContentPane() {
    return contentPane;
}

public void setContentPane(JPanel contentPane) {
    this.contentPane = contentPane;
}

,因为您基本上覆盖了jframe的基本逻辑,因此您可以(并且可能)破坏了某些内容。

2。第二件事是将组件放在网格单元上,并不会自动在整个区域延伸。如果要引用 documentation ,您会看到:

本质上,GridBaglayout将组件放在矩形(单元)中,然后使用组件的首选尺寸来确定细胞的大小。

3。可以使用填充约束的属性来实现自动拉伸。但是您仍然需要以某种方式声明所需的尺寸。

4。尺寸规格可以使用stogexweefty约束的属性。

权重用于确定如何在列之间和行之间分布空间(weighty);这对于指定调整大小的行为很重要。

因此,在您的简单情况下,您基本上有两列,其中一个具有0.7的X重量,另一个具有0.3

以下代码演示了完整的解决方案:

class MyFrame extends JFrame {

    private JPanel contentPane;

    public MyFrame (){
        contentPane = new JPanel();
        contentPane.setLayout(new GridBagLayout());

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        gbc.weightx = 0.2/0.3;
        gbc.weighty = 1;
        gbc.fill = GridBagConstraints.BOTH;
        JPanel blue = new JPanel();
        blue.setBackground(Color.BLUE);
        contentPane.add(blue, gbc);

        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        gbc.weightx = 0.1/0.3;
        gbc.weighty = 1;
        JPanel green = new JPanel();
        green.setBackground(Color.green);
        contentPane.add(green, gbc);

        setContentPane(contentPane);
        setPreferredSize(new Dimension(630, 650));
        pack();
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setVisible(true);
    }

    public static void main(String[] args) {
        new MyFrame();
    }
}

”在此处输入图像说明”

1. First of all, you need to remove this:

public JPanel getContentPane() {
    return contentPane;
}

public void setContentPane(JPanel contentPane) {
    this.contentPane = contentPane;
}

because you basically override the base logic of JFrame, hence you may (and likely will) break something.

2. The second thing is that putting component to the grid cells with constraints does not automatically stretch them across the area. If to refer to the documentation, you will see that:

Essentially, GridBagLayout places components in rectangles (cells) in a grid, and then uses the components' preferred sizes to determine how big the cells should be.

3. Automatic stretching can be achieved with fill property of constraints. But you still need to declare the required sizes somehow.

4. The sizes specification can be set up with weightx and weighty properties of constraints.

Weights are used to determine how to distribute space among columns (weightx) and among rows (weighty); this is important for specifying resizing behavior.

So in your simple case you basically have two columns where one have 0.7 of x weight and another have 0.3.

The below code demonstrates the complete solution:

class MyFrame extends JFrame {

    private JPanel contentPane;

    public MyFrame (){
        contentPane = new JPanel();
        contentPane.setLayout(new GridBagLayout());

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        gbc.weightx = 0.2/0.3;
        gbc.weighty = 1;
        gbc.fill = GridBagConstraints.BOTH;
        JPanel blue = new JPanel();
        blue.setBackground(Color.BLUE);
        contentPane.add(blue, gbc);

        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        gbc.weightx = 0.1/0.3;
        gbc.weighty = 1;
        JPanel green = new JPanel();
        green.setBackground(Color.green);
        contentPane.add(green, gbc);

        setContentPane(contentPane);
        setPreferredSize(new Dimension(630, 650));
        pack();
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setVisible(true);
    }

    public static void main(String[] args) {
        new MyFrame();
    }
}

enter image description here

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