Java 如何让 JLabels 从下一行开始
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1534889/
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
How to make JLabels start on the next line
提问by Jessy
JPanel pMeasure = new JPanel();
....
JLabel economy = new JLabel("Economy");
JLabel regularity = new JLabel("Regularity");
pMeasure.add(economy);
pMeasure.add(regularity);
...
When I run the code above I get this output:
当我运行上面的代码时,我得到了这个输出:
Economy Regularity
How can I get this output, where each JLabel starts on a new line? Thanks
我怎样才能得到这个输出,每个 JLabel 从一个新行开始?谢谢
Economy
Regularity
回答by John Kugelman
You'll want to play around with layout managersto control the positioning and sizing of the controls in your JPanel
. Layout managers are responsible for placing controls, determining where they go, how big they are, how much space is between them, what happens when you resize the window, etc.
您将需要使用布局管理器来控制JPanel
. 布局管理器负责放置控件、确定它们的位置、它们有多大、它们之间有多少空间、调整窗口大小时会发生什么等。
There are oodles of different layout managers each of which allows you to layout controls in different ways. The default layout manager is FlowLayout
, which as you've seen simply places components next to each other left to right. That's the simplest. Some other common layout managers are:
有许多不同的布局管理器,每一个都允许您以不同的方式布局控件。默认的布局管理器是FlowLayout
,正如您所见,它只是将组件从左到右彼此相邻放置。那是最简单的。其他一些常见的布局管理器是:
GridLayout
- arranges components in a rectangular grid with equal-size rows and columnsBorderLayout
- has one main component in the center and up to four surrounding components above, below, to the left, and to the right.GridBagLayout
- the Big Bertha of all the built-in layout managers, it is the most flexible but also the most complicated to use.
GridLayout
- 在具有相同大小的行和列的矩形网格中排列组件BorderLayout
- 中心有一个主要组件,上方、下方、左侧和右侧最多有四个周围组件。GridBagLayout
- 所有内置布局管理器中的Big Bertha,它是最灵活但使用起来最复杂的。
You could, for example, use a BoxLayoutto layout the labels.
例如,您可以使用BoxLayout来布局标签。
BoxLayout
either stacks its components on top of each other or places them in a row — your choice. You might think of it as a version ofFlowLayout
, but with greater functionality. Here is a picture of an application that demonstrates usingBoxLayout
to display a centered column of components:
BoxLayout
要么将其组件堆叠在一起,要么将它们排成一排 - 您的选择。您可能会将其视为 的一个版本FlowLayout
,但具有更多功能。这是一个应用程序的图片,它演示了如何使用BoxLayout
以显示居中的组件列:
An example of code using BoxLayout
would be:
使用代码的一个例子BoxLayout
是:
JPanel pMeasure = new JPanel();
....
JLabel economy = new JLabel("Economy");
JLabel regularity = new JLabel("Regularity");
pMeasure.setLayout(new BoxLayout(pMeasure, BoxLayout.Y_AXIS));
pMeasure.add(economy);
pMeasure.add(regularity);
...
回答by waqasahmed
A quick way is to use html within the JLabel.
For instance include the <br/>
tag.
一种快速的方法是在 JLabel 中使用 html。例如包括<br/>
标签。
Otherwise, implement a BoxLayout.
否则,实现一个 BoxLayout。
回答by Emir
Here is what you need to use:
这是您需要使用的:
JLabel economy = new JLabel("<html>Economy<br>Regularity</html>");
回答by princepiero
I read this piece of code:
我读了这段代码:
pMeasure.setLayout(new BoxLayout(pMeasure, BoxLayout.VERTICAL));
It seems BoxLayout doesn't have VERTICAL. Upon searching, this will work using the following code:
BoxLayout 似乎没有 VERTICAL。搜索后,这将使用以下代码工作:
pMeasure.setLayout(new BoxLayout(pMeasure, BoxLayout.Y_AXIS));
回答by user3170404
Make a separate JPanel for each line, and set the dimensions to fit each word:
为每一行制作一个单独的 JPanel,并设置尺寸以适合每个单词:
JLabel wordlabel = new JLabel("Word");
JPanel word1 = new JPanel();
word1.setPreferredSize(new Dimension(#,#);
This should work for each word. You can then add each of those JPanels to your main JPanel. This also allows you to add other components next to each word.
这应该适用于每个单词。然后,您可以将这些 JPanel 中的每一个添加到您的主 JPanel。这也允许您在每个单词旁边添加其他组件。