简体   繁体   中英

jButton resizing upon changing text label

I'm reasonably new to java GUIs and have been using netbeans to help me out.

I've set up a jButton such that when clicked its label changes. My issue is that the size of the button refuses to remain fixed despite setting a maximum and minimum size as well as the setting the preferredSize method. Do I need to change my layout? Should I go through and place each button on a panel or is there a simpler way?

I feel like this should be an easy problem to fix yet I've been at it for over an hour now. I'd appreciate any ideas. Thanks

  1. If you are new to Swing don't use a GUI builder as you will run into all sorts of issues like this one.
  2. It sounds like your Layout is preventing resizing. Make sure you are using the correct Layout Manager for your designed look. Double check any constraints that you have set for the layout. You could experiment with a different layout manager like FlowLayout to check to make sure your setPreferredSize () calls are working correctly etc.

There are a number of ways to handle this:

  1. A clean and easy way would be to create image icons for the different buttons , making them the same size. This lets you completely control what they will look like.

  2. A quick-and-dirty way to do this is the add spaces until the buttons are approximately the same size. This won't be perfect because the fonts that appear on JButons are typically not fixed-width.

  3. The 'proper' Swing way would be to use a custom Layout. For instance, if you use a GridBagLayout to arrange your components, and set the 'weightx' and 'weighty' for the JButton to 1.0, then it will take up as much space as possible, which will keep it the same size.

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