java 在对象之间放置空格的最佳方法是什么?Swing JSeparator 对象可以是不可见的分隔符吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2975489/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the best way to put spaces between objects? Can a Swing JSeparator object be an invisible separator?
提问by Warren P
I'm trying to put two buttons inside a panel using Swing widgets. Inside the NetBeans IDE, my JSeparatorborderproperty is set to (No border)in the properties pane.
我正在尝试使用 Swing 小部件将两个按钮放在面板内。在 NetBeans IDE 中,我的JSeparatorborder属性(No border)在属性窗格中设置为。
Nevertheless a line appears. This is not what I would expect from a separator object. Am I doing something wrong? Coming from a background in Delphi, and C# WinForms, I expect to find some oddities in Swing. But how exactly do you make a transparent gap of a particular size, between two buttons in a panel? Do I have to play with layouts and avoid the JSeparator?
尽管如此,还是出现了一条线。这不是我对分隔符对象的期望。难道我做错了什么?来自 Delphi 和 C# WinForms 的背景,我希望在 Swing 中找到一些奇怪的东西。但是,如何在面板中的两个按钮之间制作特定大小的透明间隙?我是否必须玩布局并避免使用JSeparator?
Update:It should be trivial to do this with a layout and without any separator object. So how do you do that? I am looking into the NetBeans layout customizer and properties inspector and finding no way to do it. (Answer: Layouts with Insets, instead of separators.)
更新:使用布局并且没有任何分隔符对象来做到这一点应该是微不足道的。那你怎么做呢?我正在研究 NetBeans 布局定制器和属性检查器,但找不到办法。(答案:带有插图的布局,而不是分隔符。)
回答by Adamski
You should take a look at the static utility methods on the Boxclass. They can be used to manufacture fixed struts that act as invisible separators; e.g.
您应该查看类上的静态实用程序方法Box。它们可用于制造作为隐形隔板的固定支柱;例如
JPanel pnl = new JPanel(new FlowLayout());
pnl.add(new JButton("Hello"));
pnl.add(Box.createHorizontalStrut(10)); // Fixed width invisible separator.
pnl.add(new JButton("Goodbye");
This produces more compact code than creating / configuring a JPanelyourself with appropriate minimum, maximum and preferred dimensions.
JPanel与使用适当的最小、最大和首选维度创建/配置自己的代码相比,这会生成更紧凑的代码。
回答by jjnguy
JSeparatoris meant to be a visible separator between components.
JSeparator旨在成为组件之间的可见分隔符。
From the javadoc for JSeparator:
JSeparator provides a general purpose component for implementing divider lines - most commonly used as a divider between menu items that breaks them up into logical groupings.
JSeparator 提供了一个用于实现分隔线的通用组件——最常用作菜单项之间的分隔线,将它们分成逻辑分组。
If you want to put a component in between two components that is invisible just use an JPanelinstead. Then set the size of the panel with setPreferedSize()and setMin/MaxSize().
如果您想将一个组件放在两个不可见的组件之间,只需使用 anJPanel代替。然后使用setPreferedSize()和设置面板的大小setMin/MaxSize()。
回答by Anton
You don't need JSeparator. Most layouts allow you to set gap (space) between compoponents. And Box class can be particularly useful.
你不需要 JSeparator。大多数布局允许您设置组件之间的间隙(空间)。Box 类可能特别有用。
回答by SubJunk
Using addSeparator with a value of 1 for height makes it invisible for me, for example:
使用值为 1 的 addSeparator 使其对我不可见,例如:
MyJToolBar.addSeparator(new Dimension(20, 1));

