2

我有一个用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);
    }
}

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

4

2 回答 2

2

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

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

于 2013-07-29T22:09:56.160 回答
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);
    }
}
于 2013-07-29T22:18:38.037 回答