我想在jlabel中添加一些多行文字。我设法正确地做了一些html标签,所以我有我的多行jlabel。现在我面临着第二次挑战,我无法过来。我需要在本文中加入一些值。例如,假设我有来自用户的输入。其中一个是他的名字,在本文中出现在jlabel上,我想跟他打个招呼,等等! 我该怎么做? 我试图用html连接几个字符串,但也有一些变量,但没有任何作用! 有什么想法吗?
答案 0 :(得分: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();
}
});
}
}