首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在JFrame中正确地居中JPanel (固定大小)?

如何在JFrame中正确地居中JPanel (固定大小)?
EN

Stack Overflow用户
提问于 2011-08-29 04:00:29
回答 6查看 62.7K关注 0票数 17

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

  1. JPanel必须在JFrame内部居中。
  2. JPanel必须始终具有使用指定的大小

setPreferredSize()方法。不能在此大小下调整大小。

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

请看下面的示例:

代码语言:javascript
复制
/* 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,但这不起作用:

示例代码:

代码语言:javascript
复制
/* 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一个截图,

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

EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2011-08-29 07:27:02

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

你的输出将会是

容器已被setMinimumSize删除(注意容器是否具有更大的大小... )

代码语言:javascript
复制
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);
    }
}
票数 16
EN

Stack Overflow用户

发布于 2011-09-03 20:31:00

首先,感谢所有人。

我再次回答我自己的问题,向大家展示我所做的选择。请参阅下面的示例代码;正如您所看到的,我只包含了实现目标所必需的最小步骤。

代码语言:javascript
复制
/* file StackResponse.java */

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

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

        JPanel panel = new JPanel();
        Dimension expectedDimension = new Dimension(100, 100);

        panel.setPreferredSize(expectedDimension);
        panel.setMaximumSize(expectedDimension);
        panel.setMinimumSize(expectedDimension);

        panel.setBackground(Color.RED); // for debug only

        Box box = new Box(BoxLayout.Y_AXIS);

        box.add(Box.createVerticalGlue());
        box.add(panel);     
        box.add(Box.createVerticalGlue());

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

        frame.setMinimumSize(frame.getMinimumSize());   // cannot be resized-

        frame.setVisible(true);

    }
}

Here你可以看到一个截图。

问题解决了。再次感谢所有人。

票数 6
EN

Stack Overflow用户

发布于 2014-09-05 14:28:14

使用GridBagLayout创建一个名为" FixedPanel“的面板,将首选大小设置为frame size,然后将您的frame添加到FixedPanel中。

代码语言:javascript
复制
Frame = new JFrame("CenterFrame");         
Frame.setLocation(0, 0);
Frame.setSize(new Dimension(400,400));//dim

JPanel FixedPanel = new JPanel(new GridBagLayout());
FixedPanel.setPreferredSize(Frame.getSize());

JPanel myPanel = new JPanel();
myPanel.setPreferredSize(new Dimension(100,100));
myPanel.setBackground(Color.BLACK);

FixedPanel.add(myPanel);
Frame.add(FixedPanel);
Frame.setVisible(true);  
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7223530

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档