2013-12-16 40 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

相关问题