只是爲了說明問題,我在Netbeans中使用了JFrame表單。JFrame:在構造函數中更改值後字符串變回爲空
好吧,所以基本上我試圖改變一個公共全局字符串(PictureName)使用構造函數的值,因爲我試圖從另一個類取另一個值,但是當我嘗試在我的JLabel代碼,它顯示爲空。下面是代碼
public class ShowPage extends javax.swing.JFrame {
public javax.swing.JLabel Picture;
public String PictureName;
public ShowPage() {
initComponents();
}
public ShowPage (String picName){
PictureName = picName;
}
private void initComponents() {
Picture = new javax.swing.JLabel();
getContentPane().add(Picture);
Picture.setBounds(20, 60, 300, 130);
Picture.setIcon(new javax.swing.ImageIcon(getClass().getResource("/MiniProject/imagesMain/"+PictureName)));
pack();
}
在構造函數中爲什麼PictureName甚至在更改後仍然無效任何想法(是的,它是用SOP檢查)?
這是PSVM BTW
public static void main(String args[]) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(ShowPage.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(ShowPage.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(ShowPage.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(ShowPage.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new ShowPage().setVisible(true);
}
});
你有兩個獨立的構造函數:一個沒有參數,調用'的initComponents( )'和一個採用一個參數並將'PictureName'設置爲參數中的值。 – GiantTree