无法阻止面板变得太小

时间:2013-07-29 21:50:48

标签: java swing resize jlabel gridbaglayout

我有一个格式为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);
    }
}

运行此代码,然后水平调整窗口大小。最终,窗口调整得足够小,使标签搞砸了。

2 个答案:

答案 0 :(得分:2)

我不知道为什么GridBagLayout会做它的功能。但如果你给每个标签加权x = 1.0,它们都会收缩。

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

答案 1 :(得分:2)

基于@ camrickr的评论,我尝试将内部面板包裹在一个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);
    }
}