如何正确地将JPanel

如何正确地将JPanel

本文介绍了如何正确地将JPanel(固定大小)居中放在JFrame中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Hi all!I'm trying to solve an -apparently- simple problem, but I cannot fix it.I'm working on a sample application with Java/Swing libraries;I have a JFrame and a JPanel.I just want to achieve the following objectives:

  1. JPanel 必须位于JFrame内部.

  1. JPanel MUST be centered inside the JFrame.

JPanel 必须具有始终的大小,该大小由
指定setPreferredSize()方法.不得在此尺寸以下调整大小.

JPanel MUST have ALWAYS the size that is specified with
setPreferredSize() method. It MUST NOT be resized under this size.

我尝试使用GridBagLayout:这是我唯一可以做到的方式.

I tried by using a GridBagLayout: it's the ONLY way I can do it.

请参见以下示例:

/* 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);

    }
}

此处屏幕截图:

我不会使用GridBagLayout做太简单的事情.我通过使用Box尝试了最简单的解决方案,但这不起作用:

I would not use a GridBagLayout to do a thing too simple.I tried a simplest solution, by using a Box, but this does not work:

示例代码:

/* 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);

    }
}

此处屏幕截图

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

Any ideas? Thanks to all :-)

推荐答案

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

BoxLayout can pretty to hold your setXxxSize(), then just add panel.setMaximumSize(new Dimension(100, 100));

,您的输出将是

已由setMinimumSize删除(如果Container的大小大于...,请注意

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

这篇关于如何正确地将JPanel(固定大小)居中放在JFrame中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 02:53