I have a JOptionPane:
JOptionPane.showMessageDialog(null, text);
The text is a sting:
String text = "Hello world."
What I want to do is change the color of the text, specifically a single word, lets say 'Hello'. SO what I've tried is:
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);
I'm not familiar with attributedtext() and this wont work. It does this:
"java.text.AttributedString@479c479cworld"
Is there a step I'm missing? Is this not the right way? Any suggestions?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…