在盒子的JDialog文本格式 [英] formatting text in jdialog box

查看:132
本文介绍了在盒子的JDialog文本格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JOptionPane的:

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

我不熟悉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文本格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆