Java separate components with lines

放肆的年华 提交于 2019-12-12 09:41:18

问题


I am learning some GUI stuff on Java and I think Im missing something here. 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!


回答1:


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.




回答2:


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)



来源:https://stackoverflow.com/questions/7748245/java-separate-components-with-lines

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!