java 如何在 JFrame 中居中 Jpanel?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28135306/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 13:03:05  来源:igfitidea点击:

How to Center Jpanel in JFrame?

javaswingjframejpanel

提问by Adnan Ali

There is a class which extends JFrame. then Further JFramehave JPanelnamed contentPane and this Jpanel contains 2 more jpanel. as Tree shown in picture.

有一个类扩展JFrame. 再往JFrameJPanel命名的contentPane和此JPanel包含2周以上的JPanel。如图所示的树。

Here is the Image

这是图像

I Want to Center that contentPanein JFrameso that on changing size of JFrameJPanel(contentPane) will remain in center. I have tried with different Layoutsbut did not come up with right one. is there any way ? Full page picture is here

我想该中心的contentPaneJFrame,这样的改变的大小JFrameJPanel(contentPane中)将继续留在中心。我尝试了不同的Layouts但没有想出正确的。有什么办法吗?整页图片在这里

Code is this.

代码是这样的。

public class Purchases extends JFrame {

    private JPanel contentPane;

public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Purchases frame = new Purchases();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }); 
    }


        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 513, 438);


        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);
        setLocationRelativeTo(null);



        JPanel panel = new JPanel();
        panel.setBounds(10, 10, 477, 193);
        contentPane.add(panel);
        panel.setLayout(null);

        JPanel panel_1 = new JPanel();
        panel_1.setBounds(10, 214, 477, 174);
        contentPane.add(panel_1);
        panel_1.setLayout(null);

}

This code is Eclipse automatically generated. I did not find where the contentPane is added in JFrame.

这段代码是Eclipse自动生成的。我没有找到在 JFrame 中添加 contentPane 的位置。

回答by MadProgrammer

Set the frame's layout to GridBagLayout. Wrap your existing content in another JPanel, add this panel to the frame

将框架的布局设置为GridBagLayout。将您现有的内容包装在另一个中JPanel,将此面板添加到框架中

For example

对于例如

Take a look at Laying Out Components Within a Containerand How to Use GridBagLayoutfor more details

查看在容器内布置组件以及如何使用 GridBagLayout了解更多详细信息

You really should avoid using nulllayouts, pixel perfect layouts are an illusion within modern ui design. There are too many factors which affect the individual size of components, none of which you can control. Swing was designed to work with layout managers at the core, discarding these will lead to no end of issues and problems that you will spend more and more time trying to rectify

你真的应该避免使用null布局,像素完美的布局是现代 ui 设计中的一种错觉。影响单个组件大小的因素太多,您无法控制这些因素。Swing 旨在与核心布局管理器一起工作,丢弃这些将导致无休止的问题和问题,您将花费越来越多的时间来尝试纠正

回答by Aman

I have made a sample for you. Modify it as per your need. Just want to show you what you want, is working.

我为你做了一个样本。根据您的需要修改它。只是想向您展示您想要的东西,正在工作。

    TestClass()
    {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 513, 438);

        contentPane = new JPanel();
        contentPane.setLayout(new BorderLayout());

        JButton b1 = new JButton("hello");
        JPanel panel = new JPanel(new BorderLayout());
        //panel.setBounds(10, 10, 477, 193);
        panel.add(b1, BorderLayout.CENTER);
        contentPane.add(panel, BorderLayout.EAST);
        //panel.setLayout(null);

        JButton b2 = new JButton("why");
        JPanel panel_1 = new JPanel(new BorderLayout());
        //panel_1.setBounds(10, 214, 477, 174);
        panel_1.add(b2, BorderLayout.CENTER);
        contentPane.add(panel_1, BorderLayout.WEST);
        //panel_1.setLayout(null);

        this.getContentPane().add(contentPane);
    }

Hope this will help. :-)

希望这会有所帮助。:-)

回答by RonM

I think you should set the maximum size of the element & set it to align center:

我认为您应该设置元素的最大大小并将其设置为居中对齐:

setAlignmentX(JComponent.CENTER_ALIGNMENT);
setMaximumSize(new Dimension(100, 100));

As explained here: How can I properly center a JPanel ( FIXED SIZE ) inside a JFrame?

如此处所述: 如何在 JFrame 内正确地将 JPanel ( FIXED SIZE ) 居中?