2015-04-27 76 views
0
//combo box actionperformed method. 

private void CmbActionPerformed(java.awt.event.ActionEvent evt) {          
//created aray of objects 

    JTextField t[]=new JTextField[8]; 

    String num=null,s1; 
    int num1=0; 
    num=Cmb.getSelectedItem().toString(); 
    num1=Integer.parseInt(num); 
    //applied a logic to create same no. of textfields that selected in combo box. 

    while(num1!=0){ 
     for(int i=0;i<num1;i++) 
     { 
      t[i]=new JTextField(10); 
      jPanel2.add(t[i]); 
      b1.setText("the objects has created"); 
      jPanel2.revalidate(); 
      validate(); 
      num1--; 
     } 
    } 
} 
//actionperformed method of buuton b1. 
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { 

    String a1; 
    a1=t1.getText(); 

    //getting error t1 not found; 
    //I think it is because t1 is local aray of comboactionperformedmethod and cant be accessed by this method. I need ur help to solve this. 

    b1.setText(s1); 
} 
+0

請描述你的問題,並提供[MVCE](http://stackoverflow.com/help/mcve) –

回答

1

最簡單的方法是將您的文本區域字段聲明爲類成員變量,而不是在方法內部。然後該類中的其他方法可以訪問它們。

JTextField t[]=new JTextField[8]; 
//created aray of objects 

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

然後:

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

    String a1; 
    a1=t[0].getText(); 

還要注意訪問數組成員的正確方法。

+0

你是說在方法裏面創建一個方法?..你可以通過編輯我的代碼來解釋我。 –

+0

@AdityaSoni你需要閱讀和**瞭解**確切地說什麼方法,類,局部變量和成員變量。一旦你理解了這4件事情,解決方案將是顯而易見的。 –

+0

yaa我有你的觀點..我已經宣佈textfield數組作爲類成員(方法外),但也是t1.getText()獲取空字符串..和T1.setText()顯示空字符串在文本字段b1。 –

相關問題