我正在为我的网站存储库制作存储库客户端,并且我需要能够检测用户何时输入某个ID以检测从哪个URL存储库中获取它。从JTextArea获取字符串
这里是我的代码:
JTextField repo = new JTextField();
JButton submit = new JButton("Download!");
String repod = repo.getText();
final String repoid = repod;
submit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(repoid.equals("pb")){
//this
}else if(repoid.equals("mb")){
//this
}else if(repoid.equals("repo29-update")){
//this
}else{
System.out.println("Contacting SERVER...");
System.out.println("Finding REPOID: "+repoid);
System.out.println("Not Found! Returning Error!");
JLabel err = new JLabel("Invalid REPOID");
JFrame a = new JFrame("Error");
a.setSize(300,100);
a.setLocationRelativeTo(null);
a.setResizable(false);
a.setAlwaysOnTop(true);
a.add(err);
a.setVisible(true);
}
}
});
当我经过这个剧本,我总是得到别人的选择,为什么呢?
这不是重复的,我将==改为.equals(),它仍然不起作用。
您创建了一个'JTextField',大约一纳秒后你会得到内容...所以,'repod'和''repoid'是空字符串。 –
'String repod = repo.getText();'< - 需要在你的'actionPerformed'中。只需检查'repod',不需要'repiod' –