【问题标题】:Can't stop panel from getting too small无法阻止面板变得太小
【发布时间】:2013-07-29 21:50:48
【问题描述】:

我有一个用GridBagLayout 格式化的面板。即使我使用setMinimumSize 设置大小,它仍然太小。当您缩小窗口(仅使用鼠标拖动)时,它会超出标签的大小,因此标签会显示为firs...。但是,只有最左边的标签会像这样缩小。

但是,从运行下面的代码可以看出,面板仍然有可能脱离窗口边缘。我对 那种 的行为没有意见,我只是不希望标签的文字被弄乱。澄清一下:我希望既不标签缩小,我希望面板滑离周围面板的边缘。

这是我能想出的最简单的例子,它以一种容易看到的方式重现了这种行为:

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

public class TestMain {
    private JFrame frame;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    TestMain window = new TestMain();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public TestMain() {
        initialize();
    }

    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel outer = new JPanel();
        outer.setLayout(new BoxLayout(outer, BoxLayout.Y_AXIS));
        outer.setBorder(new EmptyBorder(30,30,30,30));
        outer.setBackground(Color.DARK_GRAY);

        JPanel inner = new JPanel();
        inner.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();

        JLabel labelOne = new JLabel();
        labelOne.setText("first label");
        c.anchor = GridBagConstraints.LINE_END;
        inner.add(labelOne, c);

        JLabel labelTwo = new JLabel();
        labelTwo.setText("second label");
        c.anchor = GridBagConstraints.LINE_START;
        c.gridx = 2;
        inner.add(labelTwo, c);

        c.gridx = 1;
        c.weighty = 1.0;
        inner.add(Box.createHorizontalStrut(100), c);

        outer.add(inner);
        frame.add(outer);
    }
}

运行此代码,然后水平调整窗口大小。最终窗口的大小被调整得足够小,以至于标签被弄乱了。

【问题讨论】:

  • 您可以尝试设置 ipadx/ipady 约束来定义单元格的最小可能大小。

标签: java swing resize jlabel gridbaglayout


【解决方案1】:

我不知道为什么 GridBagLayout 会这样做。但是如果你给每个标签一个 weightx=1.0,它们都会缩小。

简单的解决方案是使用 FlowLayout。组件始终以其首选尺寸显示。

【讨论】:

  • 我不想缩水。
  • 我了解您想要什么,但 GridBagLayout 似乎无法以这种方式工作,因为它旨在根据可用空间动态收缩/增长组件。使用更简单的布局管理器。
  • 不幸的是,我需要一个更复杂的布局管理器,因为我的实际应用程序以更复杂的方式布置它们。我希望一个包裹的面板或其他东西可以解决它。
【解决方案2】:

基于 @camrickr 的 cmets,我尝试将内部面板包裹在 FlowLayout 面板中,否则它什么也不做,并且修复了它。

import java.awt.Color;

public class TestMain {
    private JFrame frame;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    TestMain window = new TestMain();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public TestMain() {
        initialize();
    }

    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel outer = new JPanel();
        outer.setLayout(new BoxLayout(outer, BoxLayout.Y_AXIS));
        outer.setBorder(new EmptyBorder(30,30,30,30));
        outer.setBackground(Color.DARK_GRAY);

        JPanel middleFlowLayout = new JPanel(); // Added this wrapping panel

        JPanel inner = new JPanel();
        inner.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();

        JLabel labelOne = new JLabel();
        labelOne.setText("first label");
        c.anchor = GridBagConstraints.LINE_END;
        inner.add(labelOne, c);

        JLabel labelTwo = new JLabel();
        labelTwo.setText("second label");
        c.anchor = GridBagConstraints.LINE_START;
        c.gridx = 2;
        inner.add(labelTwo, c);

        c.gridx = 1;
        c.weighty = 1.0;
        inner.add(Box.createHorizontalStrut(100), c);

        middleFlowLayout.add(inner); // Wrap the inner pannel
        outer.add(middleFlowLayout);
        frame.add(outer);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-03-01
    • 1970-01-01
    • 2015-02-15
    • 2015-06-10
    • 1970-01-01
    • 1970-01-01
    • 2017-04-27
    • 1970-01-01
    相关资源
    最近更新 更多