2013-12-16 39 views
0

我對java很新,並試圖在NetBeans 7.4中創建一個表單,該表單將接收一個名爲「name」的變量並將其用作jTextField的顯示名稱。我已經嘗試了很多東西,並且儘可能地接近(不起作用,但我沒有錯誤)。請您查看代碼並幫助我瞭解我錯過了什麼。 類StatusScreen延伸javax.swing.JFrame中{我怎樣才能加載一個變量作爲jTextField顯示文本

public StatusScreen() {       // Creates new form StatusScreen 
    initComponents(); 
} 

@SuppressWarnings("unchecked") 
// <editor-fold defaultstate="collapsed" desc="Generated Code">       
private void initComponents() { 

    PartName = new javax.swing.JTextField(); 

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 
    addWindowListener(new java.awt.event.WindowAdapter() { 
     public void windowOpened(java.awt.event.WindowEvent evt) { 
      formWindowOpened(evt); 
     } 
    }); 

    PartName.setEditable(false); 
    PartName.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N 
    PartName.setHorizontalAlignment(javax.swing.JTextField.CENTER); 

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); 
    getContentPane().setLayout(layout); 
    layout.setHorizontalGroup(
     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() 
      .addContainerGap() 
      .addComponent(PartName, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE) 
      .addContainerGap()) 
    ); 
    layout.setVerticalGroup(
     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGroup(layout.createSequentialGroup() 
      .addContainerGap() 
      .addComponent(PartName, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE) 
      .addContainerGap(235, Short.MAX_VALUE)) 
    ); 

    pack(); 
}// </editor-fold>       

private void formWindowOpened(java.awt.event.WindowEvent evt) {         
    setExtendedState(JFrame.MAXIMIZED_BOTH); 
}         

//public static void main(String args[]) {      //This is here for class testing. 
public void showStatusScreen(final String name){    //This is program entry point. 
    /* Set the Nimbus look and feel */ 
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> 
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. 
    * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
    */ 
    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(StatusScreen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (InstantiationException ex) { 
     java.util.logging.Logger.getLogger(StatusScreen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (IllegalAccessException ex) { 
     java.util.logging.Logger.getLogger(StatusScreen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (javax.swing.UnsupportedLookAndFeelException ex) { 
     java.util.logging.Logger.getLogger(StatusScreen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } 
    //</editor-fold> 

    java.awt.EventQueue.invokeLater(new Runnable() {   //To create and display the form 
     @Override 
     public void run() { 
      new StatusScreen().setVisible(true); 
     } 
    }); 
    java.awt.EventQueue.invokeLater(new Runnable() {   //Trying to initialize the PartName field with String name (not working) 
    @Override 
    public void run() {           //Trying to initialize the PartName field with String name 
    PartName.setText(name);         //Trying to initialize the PartName field with String name 
    }                //Trying to initialize the PartName field with String name 

}); }

// Variables declaration - do not modify      
private javax.swing.JTextField PartName; 
// End of variables declaration     

}

+1

圍棋,學習手工做到這一點。學習如何做到這一點需要花費很長時間。 – jzd

回答

0

幾件事情來解決:

  • 你並不需要兩個線程來運行應用程序,然後另一個來設置文本。在一個地方做兩個
  • PartNameStatusScreen類中的字段。因此,您需要創建一個StatusScreen的實例並使用setter方法來調用PartName.setText(name)。沒有它的實例,你不能調用另一個類的字段。

下面包含在main方法:

StatusScreen ss = new StatusScreen(); 
ss.setVisible(true); 
ss.setPartName(name); 

setPartName方法是這樣的:在揮杆時的教程

public void setPartName(String name){ 
    PartName.setText(name); 
} 
+0

哇。非常感謝你。我一直試圖從Web(和堆棧溢出)搜索所有周末的不同選項,試圖讓這個工作。 – user3108404

相關問題