Frage

Every time I create a JButton with an image constructed to it, it sets the JButton's size to a size slightly larger than the Image's size. I tried to use button.setBounds(x,y,width,height), and then repainted the button after this, but it did not do anything. I want to set the JButton to the exact size of the image, not slightly larger than it, and I also want to move the JButton down to the bottom of the screen. Both of these are attainable through setBounds but it does not do anything on a pre-constructed JButton with an image. What's your suggestion? Thank you for your time.

War es hilfreich?

Lösung

Try:

setMargin(new Insets(0,0,0,0));

Andere Tipps

How do I set the size of a JButton that already has a background image?

Don't do that. Instead just call setBorderPainted(false) & setContentAreaFilled(false) as shown in this example.

If the image itself is the 'wrong size' I can think of 3 alternatives. Let me know if that is the case.

Try calling (the container for the JButton).Invalidate() after setting the bounds

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top