Topic: 控制JLabel,JButton上文字的格式,换行显示。

  Print this page

1.控制JLabel,JButton上文字的格式,换行显示。 Copy to clipboard
Posted by: scottding
Posted on: 2002-12-02 13:25

上次和朋友说到要在Label上或者Button上或者messagedialog换行显示文本,或者格式化文本,当时没有什么很好的解决办法,最近看到一些资料,但是还没有找全,和大家共同讨论一下,在Java中的显示的文本是支持html标签的,我们可以控制string为html文本,如果这样的话就可以格式化显示我们所要求输出的文字,比如,我们可以给出一个String str="<html><P align=center>test1</P><font size=24><i><u>tatatatatattest2</u></i></font></html>"大家可以把这个string set到你的Label或者Button或者dialog上,你可以看到什么效果??呵呵,你还可以控制这些代码以得到你想要的一些效果,如果哪位朋友有更多的资料希望能够共同讨论。

2.Works for the tooltip as well [Re: scottding] Copy to clipboard
Posted by: snowbug
Posted on: 2002-12-02 22:59

This trick works for the popup tooltips as well.

3.Re:控制JLabel,JButton上文字的格式,换行显示。 [Re: scottding] Copy to clipboard
Posted by: floater
Posted on: 2002-12-09 22:40

However, the image tag seems not working, even though some books claim it should work.

Anyone tried before?

4.Re:控制JLabel,JButton上文字的格式,换行显示。 [Re: floater] Copy to clipboard
Posted by: snowbug
Posted on: 2002-12-09 23:08

floater wrote:
However, the image tag seems not working, even though some books claim it should work.

Anyone tried before?


I don't think the img tag can work because image requires different painting mechanism in Swing. However, I think if the requirements gets too complex for this trick to work, you can always write you own class to do the job Smile

5.Re:控制JLabel,JButton上文字的格式,换行显示。 [Re: scottding] Copy to clipboard
Posted by: floater
Posted on: 2002-12-10 01:01

In John Zukowski's book "Definitive Guide to Swing for Java 2", 2nd edition, he said it will work, but then he turns to a customized rendering class. That's why I am wondering ...

I tried before, it gave me a broken image icon(like in browser). I thought maybe I put image files in wrong place, so I put it in everywhere along the path from the root. But still the same...

6.It worked! [Re: scottding] Copy to clipboard
Posted by: snowbug
Posted on: 2002-12-10 02:06

It actually works!!

Here is the code:

import javax.swing.*;
import java.awt.*;

public class Test extends JFrame{

public Test(){
getContentPane().add(new JButton("<html><font color=red>Red</font><img src=\"http://www.cjsdn.com/images/faces/face_aplomadofalcon.gif\"</img></html>"), BorderLayout.CENTER);
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}

public static void main(String[] args) {
new Test();
}
}


I think you have to use HTTP reference for the image you want to display.

7.Re:控制JLabel,JButton上文字的格式,换行显示。 [Re: scottding] Copy to clipboard
Posted by: floater
Posted on: 2002-12-10 05:06

hehe, that's interesting.

I didn't try this before, launching a web server.

Ideally, we should make it work without a webserver. It would be a little bit demanding for a swing application to have a web server up and running, Smile. But it's something.

8.Re:控制JLabel,JButton上文字的格式,换行显示。 [Re: scottding] Copy to clipboard
Posted by: scottding
Posted on: 2002-12-10 09:33

呵呵,这个东西真是太神奇了,不知哪位可以提供一点详细的资料?


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923