2017-03-16 45 views

回答

2

有沒有辦法做到這一點,不涉及2周獨立的JLabel?

您可以在標籤中使用HTML:

firstJLabel.setText("<html><font color=\"red\">Die 1: </font>" + die1 + "</html>"); 

或者你可以使用一個JTextPane,使它看起來像一個標籤。它支持屬性:

JTextPane textPane = new JTextPane(); 
textPane.setBorder(null); 
textPane.setOpaque(false); 

SimpleAttributeSet green = new SimpleAttributeSet(); 
StyleConstants.setForeground(green, Color.GREEN); 

// Add some text 

try 
{ 
    StyledDocument doc = textPane.getStyledDocument(); 
    doc.insertString(0, die1, null); 
    doc.insertString(0, "Die 1: ", green); 
} 
catch(Exception) {}