java - 如何在 JFrame 中正确居中 JPanel ( FIXED SIZE )?

标签 java swing gridbaglayout

大家好! 我正在尝试解决一个 - 显然 - 简单的问题,但我无法修复它。 我正在使用 Java/Swing 库开发示例应用程序; 我有一个 JFrame 和一个 JPanel。 我只想实现以下目标:

  1. JPanel 必须在 JFrame 内居中。

  2. JPanel 必须始终指定的大小
    setPreferredSize() 方法。不得将其调整为低于此尺寸。

我尝试使用 GridBagLayout:这是我能做到的唯一方式。

请参阅下面的示例:

/* file StackSample01.java */

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

public class StackSample01 {
    public static void main(String [] args) {

        JFrame frame = new JFrame();
        JPanel panel = new JPanel();
        panel.setPreferredSize(new Dimension(100, 100));
        panel.setBackground(Color.RED);  

        frame.setLayout(new GridBagLayout());
        frame.add(panel, new GridBagConstraints());
        frame.setSize(new Dimension(200, 200));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);

    }
}

Here截图:

我不会使用 GridBagLayout 来做太简单的事情。 我尝试了一个最简单的解决方案,即使用 Box,但这不起作用:

示例代码:

/* file StackSample02.java */

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

public class StackSample02 {
    public static void main(String [] args) {

        JFrame frame = new JFrame();
        JPanel panel = new JPanel();
        panel.setPreferredSize(new Dimension(100, 100));
        panel.setBackground(Color.RED); // for debug 

        panel.setAlignmentX(JComponent.CENTER_ALIGNMENT); // have no effect

        Box box = new Box(BoxLayout.Y_AXIS);

        box.add(Box.createVerticalGlue());
        box.add(panel);     
        box.add(Box.createVerticalGlue()); // causes a deformation

        frame.add(box);
        frame.setSize(new Dimension(200, 200));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);

    }
}

Here截图,

有什么想法吗?感谢所有:-)

最佳答案

BoxLayout可以很好地容纳您的 setXxxSize(),然后只需添加 panel.setMaximumSize(new Dimension(100, 100));

你的输出将是

由 setMinimumSize 删除(注意 Container 是否有更大的尺寸...)

enter image description here enter image description here enter image description here

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

public class CustomComponent12 extends JFrame {

    private static final long serialVersionUID = 1L;

    public CustomComponent12() {
        Box box = new Box(BoxLayout.Y_AXIS);
        box.setAlignmentX(JComponent.CENTER_ALIGNMENT);
        box.add(Box.createVerticalGlue());
        box.add(new CustomComponents12());
        box.add(Box.createVerticalGlue());
        add(box);
        pack();
        setTitle("Custom Component Test / BoxLayout");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setMaximumSize(getMinimumSize());
        setMinimumSize(getMinimumSize());
        setPreferredSize(getPreferredSize());
        setLocation(150, 150);
        setVisible(true);
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {

            @Override
            public void run() {
                CustomComponent12 main = new CustomComponent12();
            }
        };
        javax.swing.SwingUtilities.invokeLater(r);
    }
}

class CustomComponents12 extends JPanel {

    private static final long serialVersionUID = 1L;

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

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

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

    @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);
    }
}

关于java - 如何在 JFrame 中正确居中 JPanel ( FIXED SIZE )?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7223530/

相关文章:

java - 如何为 GridBagLayout 中的所有组件设置间隙?

Java Swing GridBagLayout : Is it possible to create rows with different number of columns?

java - 使用 GridLayout 时在按钮之间留出小空间

java - 使用entityManager将多条记录插入到包含@GenerateValue的表中

java - 使 TitledBorder 标题可编辑

java - 如何在 Java Swing 中抓取鼠标?

JavaFX 平台似乎已死(关闭/崩溃)

java - 在字符串中搜索单词

java - 如何在 Java 中解码从 Azure 获取的 SAML token ?

java - Java 中使用 obj.getXY() 和 Object o = obj.getXY() 的性能