Starting GridBagLayout from top left corner in Java Swing

后端 未结 6 660
执念已碎
执念已碎 2021-02-13 18:39

I\'m new to Java Swing and I have been struggling to start the GridBagLayout from top left corner so that c.gridx=0 c.gridy=0 will put my object on the top left corner.

6条回答
  •  猫巷女王i
    2021-02-13 19:07

    if you want the grid to go all the way to the top, simply set all your weighty = 0 until the last item, set it to any number greater than 0, it will effectively push the rest of the buttons to the top.

    Don't forget to also increment your button's gridy value.

    Otherwise it will be centered. (the same can be done using gridx and weightx value if you arent using the c.fill = GridBagConstraints.HORIZONTAL property.)

提交回复
热议问题