java 如何在 JLabel 中格式化文本

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

How to format a text in JLabel

javaswingjlabel

提问by user1533166

I need to display a JLabel text in this format.

我需要以这种格式显示 JLabel 文本。

Hoffenheim  1 : 0  Koeln
    Bayern  2 : 1  HSV

I just can't get this done. I've tried String.format()without luck. Any advice?

我就是做不到这件事。我试过String.format()没有运气。有什么建议吗?

回答by tenorsax

You can use HTML. Read How to Use HTML in Swing Componentsfor details. For example you could build a table, similar to the following:

您可以使用 HTML。阅读如何在 Swing 组件中使用 HTML了解详细信息。例如,您可以构建一个表,类似于以下内容:

import javax.swing.JLabel;
import javax.swing.JOptionPane;

public class StringDemo {

    public static void main(String arg[]){

        StringBuilder buff = new StringBuilder();
        buff.append("<html><table>");
        buff.append(String.format("<tr><td align='right'>%s</td><td>:</td><td>%s</td></tr>", "Hoffenheim  1", "0  Koeln"));
        buff.append(String.format("<tr><td align='right'>%s</td><td>:</td><td>%s</td></tr>", "Bayern  2", "1  HSV"));
        buff.append("</table></html>");

        JOptionPane.showMessageDialog(null, new JLabel(buff.toString()));
    }
}

回答by dimo414

String.format()is used to insert content into a specially formatted string, not to display the string differently. JLabels and other Swing components do allow HTML however, which may get you what you need.

String.format()用于将内容插入到特殊格式的字符串中,而不是以不同的方式显示字符串。但是,JLabels 和其他 Swing 组件确实允许使用 HTML,这可能会满足您的需求。

new JLabel("<html><pre>         Hoffenheim  1 : 0  Koeln</pre></html>");
new JLabel("<html><pre>             Bayern  2 : 1  HSV</pre></html>");

That said, it looks like what you're asking for is to center your text around those colons, not just have them a given number of spaces away from the edge. That's going to be a little trickier, but still possible. To do this, you'll want to actually break everything up into several labels and arrange them in a grid (I find MigLayoutto be very good for things like this).

也就是说,看起来您要求的是将您的文本围绕这些冒号居中,而不仅仅是让它们远离边缘给定数量的空格。这会有点棘手,但仍然有可能。为此,您实际上需要将所有内容分解为多个标签并将它们排列在网格中(我发现MigLayout非常适合此类事情)。

Create six JLabels, left, right, or center aligningthem as appropriate, and drop then into a grid like so:

创建六个 JLabel,左、右或居中对齐,视情况而定,然后放入网格中,如下所示:

| Hoffenheim  1 | : | 0  Koeln |
|     Bayern  2 | : | 1  HSV   |

Obviously you can disable borders and so-on such that it doesn't appear to the user to be anything other than a nicely formatted bit of text.

很明显,您可以禁用边框等,这样它在用户看来就不会是格式良好的文本以外的任何东西。

回答by MadProgrammer

Extending on from dimo414 answer you could actually use a HTML table

从 dimo414 答案扩展,您实际上可以使用 HTML 表格

StringBuilder sb = new StringBuilder(128);

sb.append("<html>");
sb.append("<table border='0'>");
sb.append("<tr>");
sb.append("<td align='right'>Hoffenheim</td>");
sb.append("<td align='center'>1</td>");
sb.append("<td align='center'>:</td>");
sb.append("<td align='center'>0</td>");
sb.append("<td align='left'>Koeln</td>");
sb.append("</tr>");
sb.append("<tr>");
sb.append("<td align='right'>Bayern</td>");
sb.append("<td align='center'>2</td>");
sb.append("<td align='center'>:</td>");
sb.append("<td align='center'>1</td>");
sb.append("<td align='left'>HSV</td>");
sb.append("</tr>");
sb.append("</table>");
sb.append("</html>");

label.setText(sb.toString());

While this is far more complicated, it gives you greater flexibility over the formatting.

虽然这要复杂得多,但它为您提供了更大的格式灵活性。