简体   繁体   English

如何在JFrame中停止调整JPanel的大小

[英]How to stop resizing of JPanel in JFrame

I have reminder class extending JPanel, with boxlayout and 我有提醒类扩展了JPanel,具有boxlayout和

setPreferredSize(new Dimension(500,500))

In the mainclass I have JFrame 在主类中,我有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). 而且我希望提醒面板位于jframe的中心而不更改其大小(500,500),而框架不应更改其大小(900,900)。 When I run the above code, rem panel is totally expanded. 当我运行上面的代码时,rem面板完全展开。 How to make the rem panel in centre? 如何使rem面板居中?

Easy way: use AbsoluteLayout and position the "rem" exactly where you want with a graphical editor. 简单方法:使用AbsoluteLayout并通过图形编辑器将“ rem”精确定位在所需位置。 Then set invariable size for frame: 然后为框架设置不变的大小:

 frame.setResizable(false);

Do not use a BorderLayout . 不要使用BorderLayout Why do you use a BorderLayout if it is exactly what you don't want to have? 如果您确实不想使用BorderLayout,为什么还要使用BorderLayout?

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 编辑: BoxLayout是一种选择,如果您确实确实需要绝对放置所有内容,则还可以决定不使用任何布局管理器,例如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: 另外,如果您想使用Borderlayout,我很确定这是您的代码中的错字:

frame.add(rem,BorderLayout.CENTRE);

should be: 应该:

frame.add(rem,BorderLayout.CENTER);

Could it be this small thing that's bothering you? 可能是这么小的事情困扰着您吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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