Automatically size JPanel inside JFrame

前端 未结 5 1886
我寻月下人不归
我寻月下人不归 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:10

    From my experience, I used GridLayout.

        thePanel.setLayout(new GridLayout(a,b,c,d));
    

    a = row number, b = column number, c = horizontal gap, d = vertical gap.


    For example, if I want to create panel with:

    • unlimited row (set a = 0)
    • 1 column (set b = 1)
    • vertical gap= 3 (set d = 3)

    The code is below:

        thePanel.setLayout(new GridLayout(0,1,0,3));
    

    This method is useful when you want to add JScrollPane to your JPanel. Size of the JPanel inside JScrollPane will automatically changes when you add some components on it, so the JScrollPane will automatically reset the scroll bar.

提交回复
热议问题