简体   繁体   中英

Adding Second Border to JButton

Here's the issue: When I try to add a border to a JButton via setBorder(), the normal background styling of the button:

正确

disappears, to be replaced by what is essentially a clickable JLabel:

不正确的

Basically, what I'd like to do is add a colored border around the current default border. If there's another process other than using setBorder() that would work, I would be interested in hearing about it.

Also, I should add that I cannot subclass or override methods of the graphics elements, as this needs to be inserted as a standalone tool in a far larger code repository.

Edit: Specifying question better

You should create a compound border. You can do this:

JButton myButton = new JButton("BUTTON TEXT");
myButton.setBorder(BorderFactory.createCompoundBorder(myButton.getBorder(), BorderFactory.createLineBorder(Color.RED));

This will preserve the look/feel of the button and will add a red border.

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