1

我想在 jlabel 中添加一些多行文本。我设法正确放置了一些 html 标签,所以我有我的多行 jlabel。现在我面临着第二个我无法克服的挑战。我需要在本文中添加一些值。例如,假设我有来自用户的输入。其中之一是他的名字,在出现在 jlabel 上的这段文字中,我想和他打个招呼等等!我该怎么做?我试图用 html 连接几个字符串,但也连接了一些变量,但没有任何效果!任何的想法?

4

1 回答 1

2

我对html一无所知,真的,不仅仅是例子

在此处输入图像描述 在此处输入图像描述

在此处输入图像描述 在此处输入图像描述

从代码

import java.awt.*;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class HTMLTextColour extends JFrame {

    private static final long serialVersionUID = 1L;
    private JButton button;
    private JLabel label;
    private String when = "  asked yesterday  ";
    private String who = "  Vagelism  ";
    private String reputation = "  110 *7  ";
    private String accept = "  86% accept rate  ";

    public HTMLTextColour() {
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setLayout(new FlowLayout());
        button = new JButton("Sample text");
        button.setText("<html><font color=" + (button.isEnabled() ? "blue" : "red") + ">"
                + button.getText() + "</font></html>");
        button.getModel().addChangeListener(new ChangeListener() {

            @Override
            public void stateChanged(ChangeEvent e) {
                ButtonModel model = (ButtonModel) e.getSource();
                if (model.isRollover()) {
                    label.setText(label.getText().replace("<html><strike>",
                            "<html>").replace("</strike></html>", "</html>"));
                } else {
                    label.setText(label.getText().replace("<html>",
                            "<html><strike>").replace("</html>", "</strike></html>"));
                }
                if (model.isPressed()) {
                    when = "   QQQQQQQQQQQQ QQQQQQQQQQQQ:   ";
                    who = "   - wwwwwwwwwww - wwwwwwwwwww   ";
                    reputation = "  - eeeeeeeeeee - eeeeeeeeeee  ";
                    accept = "  - xxxxxxxxxxx - xxxxxxxxxxx  ";
                    label.setText("<html> " + when + " <br>" + " " + who + " <br>"
                            + " " + reputation + " <br>" + " " + accept + " </html> ");
                    pack();
                }
            }
        });
        add(button);
        label = new JLabel("<html> " + when + " <br>"
                + " " + who + " <br>"
                + " " + reputation + " <br>"
                + " " + accept + " </html> ");
        add(label);
        pack();
        setVisible(true);
    }

    public static void main(String argsv[]) {
        EventQueue.invokeLater(new Runnable() {

            public void run() {
                HTMLTextColour hTMLTextColour = new HTMLTextColour();
            }
        });
    }
}
于 2012-05-23T21:17:59.583 回答