如何在 Java Swing 的 JLabel 中设置行间距/高度?

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

How to set line spacing/height in a JLabel in Java Swing?

javahtmlswingjlabelfonts

提问by Stephane Grenier

I have the following JLabel code:

我有以下 JLabel 代码:

JLabel someJLabel = new JLabel("<html>first line<br>second line</html>");
someJLabel.setFont(new Font("Arial", Font.PLAIN, 16));

What I'd like to do is be able to control the line height/spacing between the two lines.

我想要做的是能够控制两条线之间的线高/间距。

PS: I've also looked at using paragraphs instead of breaklines, but it's the same thing. And I don't know if you can do that within an html tag without using css (you can't use css within html code in a JLabel in Java Swing).

PS:我也研究过使用段落而不是隔断线,但这是同一回事。而且我不知道您是否可以在不使用 css 的情况下在 html 标记中执行此操作(您不能在 Java Swing 的 JLabel 中的 html 代码中使用 css)。

采纳答案by Dave

This should work, but it's not. color: greenworks though.

这应该有效,但事实并非如此。 color: green虽然有效。

content.add(new JLabel("<html><p style=\"line-height: 150%;\">hi<br>world</p></html>"));

content.add(new JLabel("<html><p style=\"line-height: 150%;\">hi<br>world</p></html>"));

I guess line-height doesn't work. That's how you'd do it if you were to use CSS, so maybe you can't do it that way. Here's a nice toolI found which you can use to test if your HTML will work quickly.

我猜 line-height 不起作用。如果您要使用 CSS,您就会这样做,所以也许您不能那样做。 这是我发现的一个很好的工具,您可以使用它来测试您的 HTML 是否可以快速运行。

回答by camickr

Check out the setStyleSheet(...) method of the HTMLEditorKit. I've never used it before but I believe it provides some basic support.

查看 HTMLEditorKit 的 setStyleSheet(...) 方法。我以前从未使用过它,但我相信它提供了一些基本的支持。

Otherwise you can use a JTextPane to control the line spacing. I think you would use:

否则,您可以使用 JTextPane 来控制行距。我想你会使用:

StyleConstants.setLineSpacing(...);

You can then change the foreground/background etc to make the text pane look like a label.

然后,您可以更改前景/背景等,使文本窗格看起来像一个标签。

回答by David J. Liszewski

Hmm .. CSS in JLabel seems to work for me, if one sticks to supported properties. Try padding(or margin) and font-size:

嗯.. 如果坚持支持的属性,JLabel 中的 CSS 似乎对我有用。尝试padding(或margin)和font-size

someJLabel = new JLabel("<html><body><p style=\"padding:10; font-size:30\">First line</p><p style=\"padding:10; font-size:20\">Second line</p></body></html>");

回答by Dmitry

Because of Java supports <p>tag and CSS marginattribute you can use next solution:

由于 Java 支持<p>标签和 CSS margin属性,您可以使用下一个解决方案:

new JLabel("<html>first line<p style='margin-top:-5'>second line");

P.S. Now it is not required to close html tags.

PS 现在不需要关闭 html 标签。

回答by badpanda

You could try using two labels, and setting the distance between the two, as well as the whitespace, using a LayoutManager. I like GridBoxLayout, myself.

您可以尝试使用两个标签,并使用 LayoutManager 设置两者之间的距离以及空白。我自己喜欢 GridBoxLayout。

Edit: GridBagLayout. Whoops!

编辑:GridBagLayout。哎呀!

回答by iamrakesh

Does setting empty border helps, like

设置空边框有帮助吗,比如

label.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

method syntax BorderFactory.createEmptyBorder(int top, int left, int bottom, int right)

方法语法BorderFactory.createEmptyBorder(int top, int left, int bottom, int right)