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

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

问题描述

I have a JPanel subclass on which I add buttons, 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?

解决方案

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天全站免登陆