2014-01-30 29 views
0

我想知道是否有方法用新文本覆蓋放入JLabel的文本。是否有覆蓋JLabel內容的方法

我目前剛剛創建新標籤的代碼並未取代以前製作的標籤。

關於我的代碼,我想根據哪個按鈕被按下,從每個字符串數組中更改JLabel中保存的文本。

已上傳我的動作偵聽器的代碼。

@Override 
public void actionPerformed(ActionEvent event) { 
    ImageIcon gws = new ImageIcon("src\\myGui\\sharke.jpg"); 
    ImageIcon gib = new ImageIcon("src\\myGui\\gibbone.jpg"); 
    ImageIcon croc = new ImageIcon("src\\myGui\\croce.jpg"); 
    ImageIcon cat = new ImageIcon("src\\myGui\\cheetahe.jpg"); 
    String gw[] = {"Shark", "fast", "water", "sea", "a", "b"}; 
    String gbn[] = {"Gibbon", "climb", "tree", "grass", "a", "b"}; 
    String crc[] = {"Croc", "scale", "teeth", "river", "a", "b"}; 
    String cht[] = {"Cheetah", "run", "fur", "sand", "a", "b"}; 
    for (int i = 0; i < field1.length; i++) { 
     field1[i] = new JTextField(i); 
     panel6.add(field1[i]); 
    } 

    if (event.getSource() == quit) { 
     System.exit(0); 
    } else if (event.getSource() == reset) { 
     pic1.setIcon(null); 
     pic1.setBorder(null); 
    } else if (event.getActionCommand().equalsIgnoreCase("shark")) { 
     pic1.setIcon(gws); 
     pic1.setBorder(BorderFactory.createEtchedBorder(Color.black, Color.blue)); 
     for (int i = 0; i < field1.length; i++) { 
      field1[i] = new JTextField(gw[i]); 
      panel6.add(field1[i]); 
     } 
    } else if (event.getActionCommand().equalsIgnoreCase("gibbon")) { 
     pic1.setIcon(gib); 
     pic1.setBorder(BorderFactory.createEtchedBorder(Color.black, Color.orange)); 

    } else if (event.getActionCommand().equalsIgnoreCase("crocodile")) { 
     pic1.setIcon(croc); 
     pic1.setBorder(BorderFactory.createEtchedBorder(Color.black, Color.green)); 
    } else if (event.getActionCommand().equalsIgnoreCase("cheetah")) { 
     pic1.setIcon(cat); 
     pic1.setBorder(BorderFactory.createEtchedBorder(Color.black, Color.yellow)); 
    } 
} 

回答

2

JLabel.setText()是你在找什麼

+0

感謝您的答覆。這似乎並不適用於字符串數組,但是,只使用單個字符串,這正是我試圖用JLables填充的。你有什麼其他的建議? – eggman