简体   繁体   中英

How to Center Jpanel in JFrame?

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

这是图像

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

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.

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

For example

Take a look at Laying Out Components Within a Container and How to Use GridBagLayout for more details

You really should avoid using null layouts, 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

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. :-)

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM