开发者

Java separate components with lines

开发者 https://www.devze.com 2023-04-12 11:06 出处:网络
I am learning some GUI stuff on Java and I think Im missing something here. 开发者_JAVA百科I have some components vertically listed using BoxLayout, such as some JButtons one above other. Now I want t

I am learning some GUI stuff on Java and I think Im missing something here. 开发者_JAVA百科I have some components vertically listed using BoxLayout, such as some JButtons one above other. Now I want to separate them drawing a line between them. Do I have to use the Graphics library or is there some Swing way to separate the components with a line?

Going straight to the question: How to draw a line to separate components (such as JButtons) and which is the recommended way of doing it?

Thanks!


JSeparator, shown here, is commonly used in this context. It works well with most layouts. Also, consider How to Use Borders.

Addendum: The JSeparator UI delegate for a given Look & Feel, often modeled on BasicSeparatorUI, is particularly simple. It's paint() implementation draws a one pixel line in the foreground color and an adjacent one pixel line in the background color. The lines are as wide (or high) as the component's bounds, depending on orientation. The layout manager determines the spacing, so you'll want to review A Visual Guide to Layout Managers.


JSeparator returns weird sizing hints, the most problematic bit for a max respecting LayoutManager like BoxLayout is its unbounded max, see also a recent discussion (which was about vertical separators, same for horizontal, though)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号