2014-02-23 50 views
-1

我一直在試圖編寫一個程序,有兩個框架,第一個是NewJFrame,第二個是NewJFrame1,在N​​ewJFrame中我把一個jComboBox(jComboBox1 )和一個jTextField(jTextField1)和一個jButton(jButton1),在第二個我只把一個jTextField(jTextField2), 我寫了一個代碼,應該(當jButton1被點擊)採取選擇的項目從jComboBox1中將其放入NewJFrame1中的jTextField 2中,現在如果在jComboBox1中選擇的項目是「None」,則打印到jTextField 2中的文本應該寫入NewJFrame中的jTextField 1中,此按鈕也應該關閉NewJFrame並打開NewJFrame1.Now,當我嘗試構建程序時,它返回了2個錯誤。這兩個錯誤是由NetBeans紅色列出的,當我看到它所提示的建議時:無法找到符號。 謝謝如果和其他方法jComboBox和2 jTextFields

public class NewJFrame extends javax.swing.JFrame { 

private Object jTextField2; 

/** 
* Creates new form NewJFrame 
*/ 
public NewJFrame() { 
    initComponents(); 
} 

/** 
* This method is called from within the constructor to initialize the form. 
* WARNING: Do NOT modify this code. The content of this method is always 
* regenerated by the Form Editor. 
*/ 
@SuppressWarnings("unchecked") 
// <editor-fold defaultstate="collapsed" desc="Generated Code">       
private void initComponents() { 

    jButton1 = new javax.swing.JButton(); 
    jComboBox1 = new javax.swing.JComboBox(); 
    jTextField1 = new javax.swing.JTextField(); 

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 

    jButton1.setText("jButton1"); 
    jButton1.addActionListener(new java.awt.event.ActionListener() { 
     public void actionPerformed(java.awt.event.ActionEvent evt) { 
      jButton1ActionPerformed(evt); 
     } 
    }); 

    jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" })); 
    jComboBox1.addActionListener(new java.awt.event.ActionListener() { 
     public void actionPerformed(java.awt.event.ActionEvent evt) { 
      jComboBox1ActionPerformed(evt); 
     } 
    }); 

    jTextField1.setText("jTextField1"); 
    jTextField1.addActionListener(new java.awt.event.ActionListener() { 
     public void actionPerformed(java.awt.event.ActionEvent evt) { 
      jTextField1ActionPerformed(evt); 
     } 
    }); 

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); 
    getContentPane().setLayout(layout); 
    layout.setHorizontalGroup(
     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGroup(layout.createSequentialGroup() 
      .addGap(159, 159, 159) 
      .addComponent(jButton1) 
      .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) 
     .addGroup(layout.createSequentialGroup() 
      .addGap(54, 54, 54) 
      .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
      .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 151, Short.MAX_VALUE) 
      .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
      .addGap(80, 80, 80)) 
    ); 
    layout.setVerticalGroup(
     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() 
      .addGap(95, 95, 95) 
      .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
       .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) 
      .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 99, Short.MAX_VALUE) 
      .addComponent(jButton1) 
      .addGap(63, 63, 63)) 
    ); 

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

private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {           


}           

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {           

    NewJFrame1 a = new NewJFrame1(); 
    this.setVisible(false); 
    a.setVisible(true); 



    String Str; 
    String Abc; 

    Str = jComboBox1.getSelectedItem().toString(); 
    Abc = jTextField1.getText().toString(); 


if (jComboBox1.getSelectedItem().equals("None")){ 
jTextField2.setText(Abc);/*****here is the error,it's the setText*****/ 
} 
else 
{ 
jTextField2.setText(Str);/*****and here is the second error,it's the setText also*****/ 
} 





}           

private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {            
    // TODO add your handling code here: 
}           

/** 
* @param args the command line arguments 
*/ 
public static void main(String args[]) { 
    /* 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(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (InstantiationException ex) { 
     java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (IllegalAccessException ex) { 
     java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (javax.swing.UnsupportedLookAndFeelException ex) { 
     java.util.logging.Logger.getLogger(NewJFrame.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 NewJFrame().setVisible(true); 
     } 
    }); 
} 

// Variables declaration - do not modify      
private javax.swing.JButton jButton1; 
private javax.swing.JComboBox jComboBox1; 
private javax.swing.JTextField jTextField1; 
// End of variables declaration     

}

這是輸出:

ant -f "C:\\Users\\user\\Documents\\MYJAVAPROGRAMS\\if combobox trial" -Dnb.internal.action.name=rebuild clean jar 
init: 
deps-clean: 
Updating property file: C:\Users\user\Documents\MYJAVAPROGRAMS\if combobox trial\build \built-clean.properties 
Deleting directory C:\Users\user\Documents\MYJAVAPROGRAMS\if combobox trial\build 
clean: 
init: 
deps-jar: 
Created dir: C:\Users\user\Documents\MYJAVAPROGRAMS\if combobox trial\build 
Updating property file: C:\Users\user\Documents\MYJAVAPROGRAMS\if combobox trial\build \built-jar.properties 
Created dir: C:\Users\user\Documents\MYJAVAPROGRAMS\if combobox trial\build\classes 
Created dir: C:\Users\user\Documents\MYJAVAPROGRAMS\if combobox trial\build\empty 
Created dir: C:\Users\user\Documents\MYJAVAPROGRAMS\if combobox trial\build\generated- sources\ap-source-output 
Compiling 3 source files to C:\Users\user\Documents\MYJAVAPROGRAMS\if combobox  trial\build\classes 
C:\Users\user\Documents\MYJAVAPROGRAMS\if combobox trial\src\NewJFrame.java:108:  error: cannot find symbol 
     jTextField2.setText(Abc); 
    symbol: method setText(String) 
    location: variable jTextField2 of type Object 
C:\Users\user\Documents\MYJAVAPROGRAMS\if combobox trial\src\NewJFrame.java:113:  error: cannot find symbol 
jTextField2.setText(Str);  
     symbol: method setText(String) 
     location: variable jTextField2 of type Object 
2 errors 
C:\Users\user\Documents\MYJAVAPROGRAMS\if combobox trial\nbproject\build-impl.xml:945:  The following error occurred while executing this line: 
C:\Users\user\Documents\MYJAVAPROGRAMS\if combobox trial\nbproject\build-impl.xml:285:  Compile failed; see the compiler error output for details. 
BUILD FAILED (total time: 0 seconds) 
+0

然後會發生什麼?你的投入是什麼,你的預期產出是什麼,你的實際產出是多少? –

+0

嗯,我已經將這段代碼分配給一個j Button,那個按鈕應該打開一個新的Frame並關閉前一個,並且應該將文本打印到我放入新框架的文本框中。 但是,當我嘗試構建該應用程序時,它返回2錯誤這兩個錯誤是由紅字排列netbeans這是setText(兩者)。 – Anthony

+0

當我檢查它的建議時說:「找不到符號」 – Anthony

回答

0

我想這getSelectedItem不返回一個字符串。將其更改爲

if (jComboBox1.getSelectedItem().toString().equals("None")){

+0

它仍然顯示相同的錯誤 – Anthony

+0

例外情況會有所幫助。他們幫助識別錯誤。對於使用eclipse的人來說,這種日誌文件很難閱讀。 – JavaForAndroid

+0

如果jTextField2在同一個幀中,此代碼有效, 您認爲如果我在同一個包中創建2個幀,它會起作用嗎? – Anthony