【问题标题】:Panels not resizing correctly with GridBagLayout in Java使用 Java 中的 GridBagLayout 无法正确调整面板大小
【发布时间】:2012-09-27 05:57:40
【问题描述】:

所以我在我的 GUI 中使用了 GridBagLayout,但我遇到了一些问题,它无法正确调整大小。在有人提出替代布局之前,我很肯定我想使用 GridBagLayout;我将有大量的组件,我想正确地布置它们,当前的代码/图像只显示我现在所拥有的。

无论如何,问题是它没有正确调整自己的大小。我希望它在调整大小时保持当前的纵横比,但是它没有。某些面板在调整大小时具有某种优先级,因此当我调整大小时,它会破坏纵横比。本质上,我有一些面板比另一个大,但在调整大小后,它变得比另一个小,如下所示:

http://i.imgur.com/LWhHm.png

现在,我真正想要的是让他们保持已经提出的比例。如您所见,我希望 GUI 的聊天部分比左侧面板中的任何一个都小,就像原来一样。

下面是我现在用来生成这个的代码:

jpPack = new JPanel();
    jpCards = new JPanel();
    jpInfo = new JPanel();
    jpChat = new JPanel();


    jpPack.setBackground(Color.red);
    jpCards.setBackground(Color.blue);
    jpInfo.setBackground(Color.green);

    //create the Constraints and set some that will apply to each panel
    GridBagConstraints c = new GridBagConstraints();
    c.fill = GridBagConstraints.BOTH;

    //set the number of columns/rows to use
    c.gridwidth = 1;
    c.gridheight = 1;
    //set the x and y position
    c.gridx = 0;
    c.gridy = 0;
    //set the weight properties
    c.weightx = 1.0;
    c.weighty = 1.0;

    getContentPane().add(jpCards, c);

    //set the number of columns/rows to use
    c.gridwidth = 1;
    c.gridheight = 1;
    //set the x and y position
    c.gridx = 0;
    c.gridy = 1;
    //set the weight properties
    c.weightx = 1.0;
    c.weighty = 1.0;

    getContentPane().add(jpPack, c);

    //set the number of columns/rows to use
    c.gridwidth = 1;
    c.gridheight = 1;
    //set the x and y position
    c.gridx = 1;
    c.gridy = 0;
    //set the weight properties
    c.weightx = 0.2;
    c.weighty = 0.2;

    getContentPane().add(jpInfo, c);

    //set the number of columns/rows to use
    c.gridwidth = 1;
    c.gridheight = 1;
    //set the x and y position
    c.gridx = 1;
    c.gridy = 1;
    //set the weight properties
    c.weightx = 0.2;
    c.weighty = 0.2;

    getContentPane().add(jpChat, c);
    jpChat.setLayout(new GridLayout());
    jpChat.add(client.gui.getContentPane(), c);

    setVisible(true);

在此期间,我不妨问一下我的其他问题。我想为整个 GUI 和聊天面板设置最小尺寸限制。首先,我希望它禁止用户将其调整为小于某个 x 和某个 y。对于后者,我希望它保持我所说的我想要的纵横比,然后在我达到两个右面板的最小值后只调整左面板的大小。也就是说,它将调整每个面板的大小直到某个大小,然后继续调整整个 GUI 的大小,但只调整左侧面板的大小,而将右侧面板保留为最小大小。

感谢您帮助我。这真的让我很烦恼,我花了几个小时试图做到这一点,弄乱了 GridBagLayoutConstraints 而没有得到任何结果。这是我能做到的最好的。

【问题讨论】:

  • 您是否尝试过使用多个GridBagConstraints 对象,而不是重复使用同一个实例?
  • @gobernador GridBagLayout 的 java 教程重用了约束,所以除非从以前的使用中错误地设置,否则一切都应该没问题。
  • 你俩都对。但是当出现问题时,有时重置约束可以查明问题
  • 保持纵横比 hmm .. 如果用户调整框架大小以致 框架的 高度/宽度不保持长宽比?
  • @kleopatra - 这个想法是它会尽力保持所述纵横比。我意识到某些高度/宽度组合会使它无法做到这一点,但没有理由它不能只做最接近它的,或者尽可能多的帧的纵横比,然后将额外的空间分配给特定面板或列/行。

标签: java swing user-interface jpanel gridbaglayout


【解决方案1】:

我尝试对您的问题进行了一些研究,但原因很简单,这可能会引起您的担忧。您在 ChatPanel 中使用的 JTextField,在初始化时,您必须为其指定列,这可能会导致这种效果。由于我制作上述代码的方式,这个东西在任何意义上都没有给我任何奇怪的行为。尽管关于您的第二个问题,@kleopatra 给出了一些见解。这是我使用的代码:

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

public class ClientGrid
{
    private void displayGUI()
    {
        JFrame frame = new JFrame("Client Grid");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

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

        JPanel bluePanel = new JPanel();
        bluePanel.setOpaque(true);
        bluePanel.setBackground(Color.BLUE);

        JPanel greenPanel = new JPanel();
        greenPanel.setOpaque(true);
        greenPanel.setBackground(Color.GREEN);

        JPanel redPanel = new JPanel();
        redPanel.setOpaque(true);
        redPanel.setBackground(Color.RED);

        JPanel chatPanel = new JPanel();
        JTextField chatField = new JTextField();
        chatPanel.setLayout(new BorderLayout(5, 5));
        chatPanel.add(chatField, BorderLayout.PAGE_START);

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.anchor = GridBagConstraints.FIRST_LINE_START;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.weightx = 0.7;
        gbc.weighty = 0.3;

        contentPane.add(bluePanel, gbc);

        gbc.gridx = 1;
        gbc.weightx = 0.3;

        contentPane.add(greenPanel, gbc);

        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.weightx = 0.7;
        gbc.weighty = 0.7;

        contentPane.add(redPanel, gbc);

        gbc.gridx = 1;
        gbc.weightx = 0.3;

        contentPane.add(chatPanel, gbc);

        frame.setContentPane(contentPane);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                new ClientGrid().displayGUI();
            }
        });
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-15
    • 1970-01-01
    • 2011-03-24
    • 2017-03-22
    • 2014-02-20
    • 1970-01-01
    • 1970-01-01
    • 2012-06-03
    相关资源
    最近更新 更多