简体   繁体   中英

How to format a text in JLabel

I need to display a JLabel text in this format.

Hoffenheim  1 : 0  Koeln
    Bayern  2 : 1  HSV

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

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

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()));
    }
}

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.

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 MigLayout to be very good for things like this).

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

| 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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM