尝试通过读取 Java 中的文本字段来获取机器人按键

发布于 2025-01-03 22:53:13 字数 942 浏览 1 评论 0原文

因此,我本质上想在文本字段中输入一个字母,然后让机器人做出响应并输入按键。我已经按照我想象的方式编写了这段代码,但事实并非如此,而且我有点陷入困境。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

变身佩奇 2025-01-10 22:53:13
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() throws AWTException
    {
        final Robot robot = new Robot();
        bookIDText = new JTextField();
        this.add(bookIDText);
        bookIDText.addActionListener( new ActionListener(){
            public void actionPerformed(ActionEvent ae) {
                String words = bookIDText.getText().toString();
                System.out.println("Action on " + words);
                if (words.equals("W"))
                {
                    System.out.println("Pressing key");
                    robot.keyPress(KeyEvent.VK_H);
                }
            }
        } );
    }

    public static void main(String[] args) throws Exception
    {
        RobotTest frame = new RobotTest();
        frame.pack();
        frame.setVisible(true);
        frame.setResizable( false );
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

输入 Shift+w Enter 时的典型输出。

Action on W
Pressing key
Press any key to continue . . .
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() throws AWTException
    {
        final Robot robot = new Robot();
        bookIDText = new JTextField();
        this.add(bookIDText);
        bookIDText.addActionListener( new ActionListener(){
            public void actionPerformed(ActionEvent ae) {
                String words = bookIDText.getText().toString();
                System.out.println("Action on " + words);
                if (words.equals("W"))
                {
                    System.out.println("Pressing key");
                    robot.keyPress(KeyEvent.VK_H);
                }
            }
        } );
    }

    public static void main(String[] args) throws Exception
    {
        RobotTest frame = new RobotTest();
        frame.pack();
        frame.setVisible(true);
        frame.setResizable( false );
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

Typical output on typing Shift+w Enter.

Action on W
Pressing key
Press any key to continue . . .
ゃ懵逼小萝莉 2025-01-10 22:53:13
try
    {
    Robot robot = new Robot(); 
    bookIDText = new JTextField ();
    this.add(bookIDText);
    String words = bookIDText.getText().toString();
    if (words.equalsIgnoreCase("W"))
        {
            robot.keyPress(KeyEvent.VK_H);
        }
    }
    catch (AWTException e) 
    { 
        e.printStackTrace(); 
    } 
try
    {
    Robot robot = new Robot(); 
    bookIDText = new JTextField ();
    this.add(bookIDText);
    String words = bookIDText.getText().toString();
    if (words.equalsIgnoreCase("W"))
        {
            robot.keyPress(KeyEvent.VK_H);
        }
    }
    catch (AWTException e) 
    { 
        e.printStackTrace(); 
    } 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文