简体   繁体   English

JTextField不读取用户输入的数据

[英]JTextField is not reading the data input by the user

Here I am trying to make a Swing Java app, where I am getting a String from the JTextField t1 and comparing it whether it matches with any other string in a text file and then displaying the matched String in the JTextField t2 . 在这里,我尝试制作一个Swing Java应用程序,在该应用程序中,我从JTextField t1获取一个String并比较它是否与文本文件中的任何其他字符串匹配,然后在JTextField t2显示匹配的String However, the JTextField t1 doesn't even reads the input by the user(I have tried even displaying the input by the user) * Note:- *There is no problem with the 'main' or with the GUI of this program. 但是,JTextField t1甚至不读取用户的输入(我什至尝试显示用户的输入)* 注:- *此程序的“主”或GUI没有问题。

JButton b1;
JTextField t1,t2;

    public void actionPerformed(ActionEvent ae){try{
    String a=t1.getText();
    String search="";
    try{
        if(a.length()!=0){
           search=atomicnumber(a);
           t2.setText(a);}
        }catch(Exception x){System.out.println("Error");}

}catch (Exception x) {System.err.println("An Unexpected error encountered."+x);}
}
public static String atomicnumber(String a){try{
              boolean found=false;
              File atmno=new File("C:/Users/DELL/Periodic/text/AtomicNumber.txt");
              String e;
                  Scanner sc=new Scanner(atmno);
             while((e = sc.nextLine()) != null){
                  if (e.startsWith(a)){
                found=true;
                return e;//break;
                }
                  return("0");}}catch(IOException x){}
return("0");}
}

Create instance variable a and set this variable in the method actionPerformed and use this instance variable in another method atomicnumber . 创建实例变量a并在actionPerformed方法中设置此变量,并在另一个方法atomicnumber使用此实例变量。

You need to make the method atomicnumber as non-static to access it. 您需要将方法atomicnumber为非静态方法才能访问它。

You make an instance variable. 您使一个实例变量。 You need to make it 'visible'. 您需要使其“可见”。 Also where is your startsWith method? 另外,startsWith方法在哪里? Make sure you are passing the correct parameters. 确保传递正确的参数。

pseudo code 伪码

class SomeClass {
    var1 < THIS IS an instance method accessible to any method in that class

method1 {
    var2

}

method2 {
   something.doSomeMethodOn(var2) <<<<< THIS is NOT accessible
   something.doSomeMethodON(var1)<<< THIS IS!
}

In your actionperformed method you are creating a second String a variable in another scope : the scope of that function. 在执行动作的方法中,您将在另一个作用域(该函数的作用域)中创建另一个String变量 So you are not initializing the value to static String a but to String a (function scope). 因此,您不是将值初始化为静态String a而是初始化为String a(函数作用域)。 In the atomicNumber() function you are referencing the static String a variable which is not initialized... Try following solution, change: 在atomicNumber()函数中,您引用静态字符串一个未初始化的变量。尝试以下解决方案,进行更改:

String a = t1.getText(); 字符串a = t1.getText();

To

a = t1.getText(); 一个= t1.getText();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM