且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在 JFrame 中自动调整 JPanel 的大小

更新时间:2023-12-05 18:33:52

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.