1

如何防止 GUI 组件在 BorderLayout 管理器中调整大小时缩小?是否可以设置最小尺寸以使组件不会缩小超出它?似乎使用 setBounds 不适用于布局管理器。

还有一个问题:如果我使用嵌套面板,是否可以为它们分配固定位置并让布局管理器处理这些面板内的组件?我试图阻止 GUI 组件移动。

4

5 回答 5

5

不幸的是,布局管理器是否支持最小尺寸、最大尺寸等取决于布局管理器的实现。这会导致很多令人困惑的行为,因为并非所有布局管理器都尊重它们,而 BorderLayout 也不尊重这些约束。

BoxLayout 将尊重这些值。JComponent.setBounds 是 LayoutManager 用来设置孩子边界的东西,所以你的布局管理器可能会覆盖你放在那里的任何东西。如果您试图限制某些东西可以变得多小,我建议在您的 JFrame 或 JDialog 上设置一个最小尺寸,以防止有人将其设置得太小,从而导致布局管理器做一些有趣的事情。

我对任何做 Swing 的人的第一个建议是抓住 TableLayout,花 15 分钟来学习它,你的大部分布局问题都可以解决。我不知道这是否适用于您正在做的事情,但不久您就会遇到需要它的布局问题。TableLayout 允许您指定哪些东西增长或缩小,如果窗口太小而无法显示,则它不会将它们调整为小于首选大小。

https://www.oracle.com/technetwork/java/tablelayout-141489.html

我不确定你到底想做什么来提供更好的建议。

于 2011-08-25T23:06:01.117 回答
2

如何防止 GUI 组件在 BorderLayout 管理器中调整大小时缩小?是否可以设置最小尺寸以使组件不会缩小超出它?

对我来说,问题应该是MaximumSize为大多数LayoutManagers设置我想要的,但Minimum & PreferredSize按我的预期工作,只是基本示例

BorderLayout铺设,期望 CENTER 区域

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

public class CustomComponent extends JFrame {

    private static final long serialVersionUID = 1L;

    public CustomComponent() {
        setTitle("Custom Component Test / BorderLayout");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public void display() {
        add(new CustomComponents(), BorderLayout.NORTH);
        add(new CustomComponents(), BorderLayout.CENTER);
        add(new CustomComponents(), BorderLayout.SOUTH);
        add(new CustomComponents(), BorderLayout.EAST);
        pack();
        // enforces the minimum size of both frame and component
        setMinimumSize(getMinimumSize());
        setPreferredSize(getPreferredSize());
        setVisible(true);
    }

    public static void main(String[] args) {
        CustomComponent main = new CustomComponent();
        main.display();
    }
}

class CustomComponents extends JLabel {

    private static final long serialVersionUID = 1L;

    @Override
    public Dimension getMinimumSize() {
        return new Dimension(200, 100);
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(300, 200);
    }

    @Override
    public void paintComponent(Graphics g) {
        int margin = 10;
        Dimension dim = getSize();
        super.paintComponent(g);
        g.setColor(Color.red);
        g.fillRect(margin, margin, dim.width - margin * 2, dim.height - margin * 2);
    }
}

GridLayout铺设

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

public class CustomComponent1 extends JFrame {

    private static final long serialVersionUID = 1L;

    public CustomComponent1() {
        setTitle("Custom Component Test / GridLayout");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public void display() {
        setLayout(new GridLayout(0, 1));
        add(new CustomComponents());
        add(new CustomComponents());
        add(new CustomComponents());
        add(new CustomComponents());
        pack();
        // enforces the minimum size of both frame and component
        setMinimumSize(getMinimumSize());
        setPreferredSize(getPreferredSize());
        setVisible(true);
    }

    public static void main(String[] args) {
        CustomComponent1 main = new CustomComponent1();
        main.display();
    }
}

class CustomComponents extends JLabel {

    private static final long serialVersionUID = 1L;

    @Override
    public Dimension getMinimumSize() {
        return new Dimension(200, 100);
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(300, 200);
    }

    @Override
    public void paintComponent(Graphics g) {
        int margin = 10;
        Dimension dim = getSize();
        super.paintComponent(g);
        g.setColor(Color.red);
        g.fillRect(margin, margin, dim.width - margin * 2, dim.height - margin * 2);
    }
}

Just one more question: If I used nested panels, is it possible to assign 
them fixed positions and let the layout manager takes care of the components 
inside these panels?

是的,对于最完整的 GUI,我看不到任何问题,问题应该是正确地将几个 JComponents 布局到 GUI 中,但为此你必须提出真正的问题

于 2011-08-25T23:48:29.373 回答
1

您可以使用setMinimumSize,setMaximumSizesetPreferredSize来控制组件的大小(尽管某些布局管理器在与布局管理器的要求冲突时必然会忽略这些)。

setBounds使用布局管理器时不应使用,因为setBounds这是布局管理器调整组件大小的机制,因此您设置的任何边界都将被布局管理器覆盖。

于 2011-08-25T23:08:26.010 回答
1

我正在寻找类似问题的解决方案,我在这里找到了非常有趣的文档(它解决了我的问题):http: //javadude.com/articles/layouts/#blHow

实际上,您只需要在使用 BorderLayout 时设置组件的preferedSize(更多信息请参见链接)

例子

JFrame jframe = new JFrame("test Borderlayout");
jframe.setLayout(new BorderLayout());

JButton button1 = new JButton("1");
JButton button2 = new JButton("2");
button2.setPreferredSize(new Dimension(200,0));

jframe.add(button1,BorderLayout.CENTER);
jframe.add(button2, BorderLayout.EAST);

jframe.setSize(300, 100);
jframe.setVisible(true);
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

在上面的示例中,即使您调整大小,button2 也不会缩小(将其宽度保持为 200 像素)。

结果

在此处输入图像描述

结果调整大小

在此处输入图像描述

于 2016-06-02T13:15:36.177 回答
0

也许你可以试试 MigLyout。它真的是很好的布局管理器。

于 2011-08-25T23:41:39.880 回答