Formatieren von Text in einem Dialogfeld
Ich habe eine JOptionPane:
JOptionPane.showMessageDialog(null, text);
Der Text ist ein Stich:
String text = "Hello world."
Was ich tun möchte, ist die Farbe des Textes zu ändern, insbesondere ein einzelnes Wort, sagen wir 'Hallo'. Also, was ich versucht habe, ist:
String t1 = "Hello";
String t2 = "world."
Font serifFont = new Font("Serif", Font.BOLD, 12);
AttributedString as = new AttributedString(t1);
as.addAttribute(TextAttribute.FONT, serifFont);
as.addAttribute(TextAttribute.FOREGROUND, Color.red);
JOptionPane.showMessageDialog(null, as+t2);
Ich kenne attributedtext () nicht und das funktioniert nicht. Es macht das:
"java.text.AttributedString@479c479cworld"
Gibt es einen Schritt, den ich vermisse? Ist das nicht der richtige Weg? Irgendwelche Vorschläge?