Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
329 views
in Technique[技术] by (71.8m points)

java - formatting text in jdialog box

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It should be possible to use html to solve this, ie

String t = "<html><font color=#ffffdd>Hello</font> world!";

See http://docs.oracle.com/javase/tutorial/uiswing/components/html.html for more info.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...