2013-12-14 17 views
1

對於此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

這可能嗎?

+2

使用HTML格式。 –

+0

[JButtons中的Wrap Wrap]的可能的重複(http://stackoverflow.com/questions/5766175/word-wrap-in-jbuttons) –

回答

2

E.G. (調整需要)

enter image description here

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); 
    } 
} 
+0

謝謝!你的回答對我所問的問題是完美的。還有一個問題,因爲它還沒有完全解決我的問題。基本上我的程序html解析具有相同結構的字符串<技能名稱>範圍:<範圍編號>費用:冷卻時間:。我將這些單獨的結構保存爲不同的字符串變量我可以在這個html代碼中加入一個字符串變量嗎? – DHH

+0

這是一個*完全*不同的問題,你應該啓動一個[new quest1on](http://stackoverflow.com/questions/ask)!如果此問題與新問題相關,請連接至該問題。不要認爲只因爲一個人可以回答你問的一個答案,他們可以回答你可能會問的任何問題。 ;) –

相關問題