Can you set a permanent size for a JPanel inside of a JFrame?

前端 未结 4 1463
北海茫月
北海茫月 2021-01-21 07:43

My current problem is that I have a JFrame with a 2x2 GridLayout. And inside one of the squares, I have a JPanel that is to display a grid. I am having a field day with the java

4条回答
  •  不知归路
    2021-01-21 07:50

    GridBagLayout is what you really want to use. The GridLayout will force the same size for each component in the layout no matter what size constraints you put on them. GridBagLayout is a lot more powerful and a lot more complicated. Study up on the API page for it. Using GridBagLayout, the components won't fill the whole grid space if you don't want them to and can even stay the size that you ask it to be. To keep a component's size from changing, I would set all three available size constraints:

    water.setPreferredSize(new Dimension(20, 20));
    water.setMinimumSize(new Dimension(20, 20));
    water.setMaximumSize(new Dimension(20, 20));
    

    For your buttons, I would definitely use an inner panel as Bryan mentions. You could use either a GridLayout like he suggests or a FlowLayout if you don't want all the buttons to be the same size. Add all your buttons to that inner panel instead of the main one.

提交回复
热议问题