2013-07-10 406 views
1

任何人可以告訴我如何在獲取jTextFields的值後更改a1,a2,a3,a4,a5的字體,以便如果我在JOptionPane上顯示它的字體賦值每個變量都有顏色如何使用變量更改文本字段的字體

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {           
    // TODO add your handling code here: 
    String a1 = jTextField1.getText(); 
    String a2 = jTextField2.getText(); 
    String a3 = jTextField3.getText(); 
    String a4 = jTextField4.getText(); 
    String a5 = jTextField5.getText(); 

    String m1 = "You will never forget " + a1 + "\n\n"; 
    String m2 = "You can consider " + a2 + " as your true friend \n\n"; 
    String m3 = "You really Love " + a3 + "\n\n"; 
    String m4 = a4 + " is your twin soul \n\n" ; 
    String m5 = "you will remember " + a5 + " for the rest of your life (due to past –good or bad- experiences, lessons etc) \n\n"; 
    String m6 = m1 + m2 + m3 + m4 + m5; 
    JOptionPane.showMessageDialog(null,m6); 
} 

回答

1

您應該創建標籤,設置字體,然後用它來創建messageDialog。 嘗試是這樣的:

String a1 = jTextField1.getText(); 
String a2 = jTextField2.getText(); 
String a3 = jTextField3.getText(); 
String a4 = jTextField4.getText(); 
String a5 = jTextField5.getText(); 

String m1 = "You will never forget " + a1 + "\n\n"; 
String m2 = "You can consider " + a2 + " as your true friend \n\n"; 
String m3 = "You really Love " + a3 + "\n\n"; 
String m4 = a4 + " is your twin soul \n\n" ; 
String m5 = "you will remember " + a5 + " for the rest of your life (due to past –good or bad- experiences, lessons etc) \n\n"; 
String m6 = m1 + m2 + m3 + m4 + m5; 

JLabel label = new JLabel(m6); 
label.setFont(new Font("serif", Font.BOLD, 14)); 
JOptionPane.showMessageDialog(null,label); 
+0

是否有我應該打電話的任何導入?因爲沒有一個JLabel當我鍵入它 – Miki

+0

是的,「javax.swing.JLabel中的」如果你正在使用Eclipse,有一個很好的快捷鍵「Ctrl + Shift + O」,添加你需要的所有進口: –

+0

還要考慮'的deriveFont ()'。 – trashgod

2

請參閱How to Use HTML in Swing Components。例如:

String m1 = "<html>You will never forget <b>" + a1 + "</b></html>"; 

現在a1將出現在大膽

+0

一般來說它的工作原理,但似乎不是[JTextField中(http://stackoverflow.com/questions/12231750/jtextfield-together-with-html-tags)。 – Eugene

+0

@Eugene他不想在'JTextField'中顯示。 – Maroun

相關問題