jframe和jpanel尺寸
我有一个Jframe和一些JPANEL,根据用户的去处(登录,主页等)显示,是否有办法将所有JPanels设置为相同的大小而无需手动指定?
public mainApp() {
main = new JFrame("Application");
main.setSize(640, 480);
main.setVisible(true);
}
private JPanel loginScreen() {
login = new JPanel(null);
login.setSize(640, 480);
login.setVisible(true);
}
例如,我有5种不同的jpanels,并且必须指定每个大小,是否有一种方法可以设置与Jframe相同的默认大小?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您在这里有不同的有效选项。知道您想从一个视图更改为另一种视图并保持相同的尺寸,听起来最好的选择是使用纸牌套件,使您可以在不同视图之间进行更改,而不必担心重新粉刷和重新降低内容(此教程来自当我学习使用这种布局时,Oracle对我有很大帮助:。
-但是, jpanel到该边界的中心。
You have different valid options here. Knowing that you want to change from one view to another and keep the same size, it sounds like the best option would be to use a CardLayout allowing you to change between the different views without having to worry about repainting and revalidating stuff (this tutorial from Oracle helped me a lot back when I was learning to use this layout: Oracle - How to use CardLayout.
However, as usual with Swing/AWT this is not the only valid option. For example, you could also use the BorderLayout that is applied by default to the ContentPane from the JFrame and add the desired JPanel to the Center of that BorderLayout. However, you would have to manage the view-changing process in this case.