尝试通过读取 Java 中的文本字段来获取机器人按键
因此,我本质上想在文本字段中输入一个字母,然后让机器人做出响应并输入按键。我已经按照我想象的方式编写了这段代码,但事实并非如此,而且我有点陷入困境。
package robottest;
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class RobotTest extends JFrame
{
JTextField bookIDText;
public RobotTest()
{
try
{
Robot robot = new Robot();
bookIDText = new JTextField ();
this.add(bookIDText);
String words = bookIDText.getText().toString();
if (words == "W")
{
robot.keyPress(KeyEvent.VK_H);
}
}
catch (AWTException e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
RobotTest frame = new RobotTest();
frame.pack();
frame.setVisible(true);
frame.setResizable( false );
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
So, I essentially want to type in a letter into the text field and then have the Robot respond and enter a key press. I've written this code how I imagined it would work, but it doesn't and I'm kinda stuck for ideas.
package robottest;
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class RobotTest extends JFrame
{
JTextField bookIDText;
public RobotTest()
{
try
{
Robot robot = new Robot();
bookIDText = new JTextField ();
this.add(bookIDText);
String words = bookIDText.getText().toString();
if (words == "W")
{
robot.keyPress(KeyEvent.VK_H);
}
}
catch (AWTException e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
RobotTest frame = new RobotTest();
frame.pack();
frame.setVisible(true);
frame.setResizable( false );
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
输入 Shift+w Enter 时的典型输出。
Typical output on typing Shift+w Enter.