我能够将文本从文件中保存为字符串,并将其保存到JTextField中。但是,当我尝试使用机器人按Enter键以便它可以处理每个命令时它将无法工作!
if (source.getText().equals("Load"))
{
int returnVal = fc.showOpenDialog(source);
if (returnVal == JFileChooser.APPROVE_OPTION)
{
try{
File file = fc.getSelectedFile();
Scanner scan = new Scanner(file);
scan.useDelimiter(",");
while(scan.hasNext())
{
String open = scan.next();
panel1.command.setText(open);
panel1.loadFile();
}
}catch(Exception e1){
System.out.println(e1);
}
}
JOptionPane.showMessageDialog(source, "Loading a previous page",
"Loading", JOptionPane.INFORMATION_MESSAGE);
System.out.println();
}
用于加载文件。下面是让机器人按下输入的代码。
public synchronized void loadFile()
{
Robot robot;
try {
robot = new Robot();
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
} catch (AWTException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
任何帮助将不胜感激!