BorderLayout center command won't center

后端 未结 3 1807
时光说笑
时光说笑 2021-01-21 18:08

I am trying to place two JButtons next to each other in the center of a JFrame, that won\'t re-size the buttons when the JFrame is re-size

3条回答
  •  攒了一身酷
    2021-01-21 18:19

    Set GridBagLayout to panel1.

     panel1.setLayout(new GridBagLayout());
    

    EDIT:

    @trashgod : I have to figure out how the default constraints do it.

    Because of the field GridBagLayout.defaultConstraints :

    This field holds a gridbag constraints instance containing the default values, so if a component does not have gridbag constraints associated with it, then the component will be assigned a copy of the defaultConstraints.

    In normal practice, one must have to create GridBagConstraints object and set fields to specify the constraints on each object.

    To quote the tutorial:

    The preferred approach to set constraints on a component is to use the Container.add variant, passing it a GridBagConstraints object

提交回复
热议问题