简体   繁体   中英

centering a button in 2 grids using GridBagLayout (Java Swing)

I am curious if it possible to center a JButton in two X grids of a GridBagLayout? I've done some searching through the API but haven't found much. I have also drawn up a small picture to clarify what I am trying to do.

Thank you!

Example Image:

https://i.stack.imgur.com/wTzMQ.png

I've done some searching through the API but haven't found much.

Read the section from the Swing tutorial on How to Use GridBagLayout .

You would need to concentrate on the Specifying Constraints section:

  1. the gridwidth constraint will allow the button to span multiple columns
  2. the anchor constraint will allow the button to be centered within the two columns.
  3. the fill constraint will need to be turned off.

The above assumes that you actually have other components on the panel in each of the columns. You can't just randomly say a single component takes up two columns.

So start with the demo code in the tutorial an modify it to have the button on the bottom centered.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM