Java separate components with lines(Java 用线条分隔组件)
问题描述
我正在学习一些关于 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.
这篇关于Java 用线条分隔组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Java 用线条分隔组件
基础教程推荐
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- 降序排序:Java Map 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01