Java 用线条分隔组件

2022-01-21 00:00:00 line components java swing

我正在学习一些关于 Java 的 GUI 东西,我想我在这里遗漏了一些东西.我有一些使用 BoxLayout 垂直列出的组件,例如一些 JButtons 一个在另一个之上.现在我想将它们分开,在它们之间画一条线.我必须使用图形库还是有一些 Swing 方法可以用线分隔组件?

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?

直奔问题:如何画一条线来分隔组件(例如 JButtons),推荐的做法是什么?

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?

谢谢!

推荐答案

JSeparator,显示 这里,通常用于这种情况.它适用于大多数布局.另外,请考虑 如何使用边框.

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

附录:给定外观的 JSeparator UI 委托感觉,通常仿照 BasicSeparatorUI,特别简单.它的 paint() 实现以前景色绘制一像素线,并以背景色绘制相邻的一像素线.这些线与组件的边界一样宽(或高),具体取决于方向.布局管理器确定间距,因此您需要查看 布局管理器的可视化指南.

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.

相关文章