问题描述
我有一个 JOptionPane:
I have a JOptionPane:
JOptionPane.showMessageDialog(null, text);
文字很刺眼:
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);
我不熟悉attributedtext(),这行不通.它这样做:
I'm not familiar with attributedtext() and this wont work. It does this:
"java.text.AttributedString@479c479cworld"
"java.text.AttributedString@479c479cworld"
有没有我遗漏的步骤?这不是正确的方法吗?有什么建议吗?
Is there a step I'm missing? Is this not the right way? Any suggestions?
推荐答案
应该可以用html来解决这个问题,即
It should be possible to use html to solve this, ie
String t = "<html><font color=#ffffdd>Hello</font> world!";
参见 http://docs.oracle.com/javase/tutorial/uiswing/components/html.html 了解更多信息.
See http://docs.oracle.com/javase/tutorial/uiswing/components/html.html for more info.
这篇关于在 jdialog 框中格式化文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!