對於此String
我該如何做?將字體和顏色設置爲包含在一個JLabel中的字符串的不同部分
Orb of Deception Range: 880 Cooldown: 7 Cost: 70/75/80/85/90 Mana
我想有藍顏色和大小14 "Orb of Deception"
,並/"Cooldown"
/"Cost"
顏色黑色尺寸12和數字顏色爲綠色和大小10
它必須包含在一個JLabel
。
這可能嗎?
對於此String
我該如何做?將字體和顏色設置爲包含在一個JLabel中的字符串的不同部分
Orb of Deception Range: 880 Cooldown: 7 Cost: 70/75/80/85/90 Mana
我想有藍顏色和大小14 "Orb of Deception"
,並/"Cooldown"
/"Cost"
顏色黑色尺寸12和數字顏色爲綠色和大小10
它必須包含在一個JLabel
。
這可能嗎?
E.G. (調整需要)
import java.awt.*;
import javax.swing.*;
class ColoredLabel {
static String text =
"<html>"
+ "<head>"
+ "<style type='text/css'>"
+ ".name {"
+ " font-size: 16px;"
+ " color: blue;"
+ "}"
+ ".value {"
+ " font-size: 12px;"
+ " color: green;"
+ "}"
+ "</style>"
+ "</head>"
+ "<body>"
+ "<h1>Orb of Deception</h1>"
+ "<table border=1>"
+ "<tr><td class='name'>Range</td><td class='value'>880</td></tr>"
+ "<tr><td class='name'>Cost</td><td class='value'>70/75/80/85/90 Mana</td></tr>"
+ "<tr><td class='name'>Cooldown</td><td class='value'>7</td></tr>"
+ "</table>"
+ "</body>"
+ "</html>";
public static void main(String[] args) {
Runnable r = new Runnable() {
@Override
public void run() {
JOptionPane.showMessageDialog(null, new JLabel(text));
}
};
// Swing GUIs should be created and updated on the EDT
// http://docs.oracle.com/javase/tutorial/uiswing/concurrency
SwingUtilities.invokeLater(r);
}
}
謝謝!你的回答對我所問的問題是完美的。還有一個問題,因爲它還沒有完全解決我的問題。基本上我的程序html解析具有相同結構的字符串<技能名稱>範圍:<範圍編號>費用:
這是一個*完全*不同的問題,你應該啓動一個[new quest1on](http://stackoverflow.com/questions/ask)!如果此問題與新問題相關,請連接至該問題。不要認爲只因爲一個人可以回答你問的一個答案,他們可以回答你可能會問的任何問題。 ;) –
使用HTML格式。 –
[JButtons中的Wrap Wrap]的可能的重複(http://stackoverflow.com/questions/5766175/word-wrap-in-jbuttons) –