Automatically size JPanel inside JFrame

前端 未结 5 1879
我寻月下人不归
我寻月下人不归 2020-12-10 04:23

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

MainPanel mainPanel = new          


        
5条回答
  •  甜味超标
    2020-12-10 05:01

    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.

提交回复
热议问题