2015-04-27 77 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。 –

相关问题