简体   繁体   中英

How to stop resizing of JPanel in JFrame

I have reminder class extending JPanel, with boxlayout and

setPreferredSize(new Dimension(500,500))

In the mainclass I have JFrame

rem = new Reminder();

            frame.setSize(900,900);
            rem.setMaximumSize(new Dimension(500,500));
            frame.getContentPane().removeAll();
            frame.setLayout(new BorderLayout());
            frame.add(rem,BorderLayout.CENTRE);
            frame.validate();
            frame.repaint();

And I want the reminder panel in the centre of the jframe without changing its size(500,500) and frame should not change it size(900,900). When I run the above code, rem panel is totally expanded. How to make the rem panel in centre?

Easy way: use AbsoluteLayout and position the "rem" exactly where you want with a graphical editor. Then set invariable size for frame:

 frame.setResizable(false);

Do not use a BorderLayout . Why do you use a BorderLayout if it is exactly what you don't want to have?

Edit: BoxLayout is one choice, if you really need to position everything absolutely yourself, you can also decide to use no layout manager, eg http://docs.oracle.com/javase/tutorial/uiswing/layout/none.html

Also if you wanna use a Borderlayout I'm pretty sure it's a typo in your code:

frame.add(rem,BorderLayout.CENTRE);

should be:

frame.add(rem,BorderLayout.CENTER);

Could it be this small thing that's bothering you?

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