简体   繁体   中英

How can i enlarge a Jbutton from the center when it's hover?

How can i enlarge a Jbutton from the center when it's hover? I use the method setSize(), but the button enlarges to the right and down. I'd like to make it enlarge to all the directions. The JButton is in a JPanel with a flowLayout. Thanks

You should use a transparant border for your button, and when hovering over it, set the border-size to 0.

This is your initial code for the button border:

JButton btn = ...;
Color borderColor = new Color(0, 0, 0, 0,); //Transparant border
btn.setBorder(BorderFactory.createLineBorder(borderColor, 4));

When hover, you should set the border to this:

btn.setBorder(BorderFactory.createLineBorder(borderColor, 0));

That should be a quick workaround.

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