Java用线条分隔组件
我正在学习一些关于 Java 的 GUI 东西,我想我在这里遗漏了一些东西。 我有一些使用 BoxLayout 垂直列出的组件,例如一些 JButtons 一个在另一个之上。现在我想将它们分开,在它们之间画一条线。我是否必须使用图形库或者是否有某种 Swing 方法来用线分隔组件?
直奔问题:如何画一条线来分隔组件(例如 JButton)以及推荐的做法是什么?
谢谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
JSeparator
,如图所示这里,通常在这种情况下使用。它适用于大多数布局。另外,请考虑如何使用边框。附录:给定外观的
JSeparator
UI 委托感觉,经常仿照BasicSeparatorUI
,特别简单。它的paint()
实现以前景色绘制一条单像素线,并以背景色绘制相邻的一像素线。这些线与组件的边界一样宽(或高),具体取决于方向。布局管理器决定间距,因此您需要查看 布局管理器的视觉指南。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 onBasicSeparatorUI
, is particularly simple. It'spaint()
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 返回奇怪的大小调整提示,对于像 BoxLayout 这样的 LayoutManager 的最大值来说,最有问题的一点是它的无界最大值,另请参阅 最近的讨论(关于垂直分隔符,但水平分隔符相同)
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)