【发布时间】:2014-03-14 18:49:59
【问题描述】:
在这里,我正在尝试制作一个 Swing Java 应用程序,我从 JTextField t1 获取 String 并比较它是否与文本文件中的任何其他字符串匹配,然后在其中显示匹配的 String JTextField t2。但是,JTextField t1 甚至不读取用户的输入(我什至尝试过显示用户的输入)
*注意:-*'main' 或这个程序的 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");}
}
【问题讨论】:
-
你的方法没有参数。如果它不接受参数,您将永远无法使用参数调用它。
-
我添加了一个
static String a来进行编译,我没有尝试运行或分析您的代码以查看是否可以修复它。