自动调整JFrame中的JPanel大小 [英] Automatically size JPanel inside JFrame

查看:500
本文介绍了自动调整JFrame中的JPanel大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JPanel子类,我在其上添加了buutons,标签,表格等。要在屏幕上显示我使用JFrame:

I have a JPanel subclass on which I add buutons, labels, tables, etc. To show on screen it I use JFrame:

MainPanel mainPanel = new MainPanel(); //JPanel subclass

JFrame mainFrame = new JFrame();
mainFrame.setTitle("main window title");
mainFrame.getContentPane().add(mainPanel);
mainFrame.setLocation(100, 100);
mainFrame.pack();
mainFrame.setVisible(true);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

但是当我调整窗口大小时,面板的大小不会改变。即使调整大小,如何使面板的大小与窗口的大小相同?

But when I size the window, size of panel don't change. How to make size of panel to be the same as the size of window even if it was resized?

推荐答案

你可以设置一个像BorderLayout这样的布局管理器然后更具体地定义你的面板应该去哪里:

You can set a layout manager like BorderLayout and then define more specifically, where your panel should go:

MainPanel mainPanel = new MainPanel();
JFrame mainFrame = new JFrame();
mainFrame.setLayout(new BorderLayout());
mainFrame.add(mainPanel, BorderLayout.CENTER);
mainFrame.pack();
mainFrame.setVisible(true);

这会将面板放入框架的中心区域,并在调整框架大小时自动增长。

This puts the panel into the center area of the frame and lets it grow automatically when resizing the frame.

这篇关于自动调整JFrame中的JPanel大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆