Java – how to add a gap at the edge of a JButton?

I want to keep the default borders on my jbuttons, but also put a blank area around them I'm using vertical @ r_ 518_ 2419@Layout.

>I didn't talk about borders at first, and I got the single pixel lineborders I wanted, but the buttons are docked with each other. > Then I tried the button [i] setBorder(BorderFactory.createEmptyBorder(5,5,5)). Instead of adding a blank area around the button, it expands the button area It also removes lineborder. > Then I tried: button [i] setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEmptyBorder(5,5),button. getBorder()))

This brings me back to lineborder, but instead of adding spaces off the line, I expand the area of the button off the line!

I realized that I could add blank boxes to separate my buttons, but I also wanted space on both sides of them, which is why I wanted to add an empty border I'm new to swing, so maybe there's a better way to do this, I don't know:)

I'm using Jython, but the API should be the same as Java

Solution

Conceptually, the "empty borders" you want to add are not actually part of the button (for example, they should not be clickable)

This is actually a layout problem, so you should check the documentation of the layout manager you are using For example:

>Some layout managers (such as flowlayout, borderlayout, or GridLayout) have hgap and vgap attributes to specify horizontal and vertical gaps between components. > Using gridbaglayout, you can set the illustration of the GridBagConstraints object. > With BoxLayout, you can add "rigid areas", "glue" and "fillers" (see box class)

wait.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>