java 如何在垂直排序的元素之间放置一条水平线?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2567559/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 21:49:37  来源:igfitidea点击:

How can I put a horizontal line between vertically ordered elements?

javauser-interfaceswingline

提问by Roman

I have a set of vertically ordered elements. They are displayed with the following code:

我有一组垂直排序的元素。它们与以下代码一起显示:

JPanel myPanel = new JPanel();
myPanel.setLayout(new BoxLayout(myPanel, BoxLayout.Y_AXIS));
JButton button = new JButton("My Button");
JLabel label = new JLabel("My label!!!!!!!!!!!");
myPanel.add(button);
myPanel.add(label);

I would like to put a horizontal line between my elements (something like <hr>in html). Does anybody know how it can be done?

我想在我的元素之间放置一条水平线(类似于<hr>html)。有谁知道怎么做?

回答by Ascalonian

Use a JSeparator. Check out this tutorialon it.

使用 JSeparator。查看有关它的本教程

But for a quick answer, just use the following code:

但要快速回答,只需使用以下代码:

myPanel.add(button);
myPanel.add(new JSeparator());
myPanel.add(label);

回答by Devon_C_Miller

Create a JSeparator and add it between button and label.

创建一个 JSeparator 并将其添加到按钮和标签之间。