你如何模拟一个JTextField的点击? JButton的doClick等效()? [英] How do you simulate a click on a JTextField? Equivalent of JButton doClick()?

查看:463
本文介绍了你如何模拟一个JTextField的点击? JButton的doClick等效()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个Java项目,需要有一个关键的preSS模拟在一个JTextField的点击。我正在寻找的是将JButton doClick()方法是等效的。

I am working on a Java project and need to have a keypress simulate a click on a JTextField. What I'm looking for is the equivalent of the JButton doClick() method.

我想有钥匙preSS中输入执行完全相同的功能上JTextField的点击。

I am trying to have the keypress "enter" perform the exact same function as a click on the JTextField.

不知道该提供什么样的其他信息。先谢谢了。

Not sure what other info to provide. Thanks in advance.

推荐答案

OK,感谢您的帮助。我想我没有说清楚,但现在我已经发现了一种方法,使我的code的工作由于一些自己的想法。

OK, thanks for the help. I guess I wasn't being clear, but I have now discovered a way to make my code work thanks to some of your ideas.

我已经想到刚刚创建被称为由两个功能的私有方法,但code的一部分需要知道的JTextField用户点击。我发现.getFocusOwner(),这让我与福克斯(JTextField的)引用当前项目。事情是这样的。

I had already thought of just creating a private method that was called by both functions, but part of the code needs to know which JTextField the user is clicked on. I discovered .getFocusOwner(), which allows me to reference the current item with Focus (the JTextField). Something like this

    public void keyPressed(KeyEvent e) {
        if(e.getKeyCode()==KeyEvent.VK_ENTER) {
            Object which = JFrame.getFocusOwner();                
            if(which.getClass() == JTextField.class)
                foo(which);
         }
    }

    public void mouseClicked(MouseEvent e) {                
        Object which = e.getSource();
        if(which.getClass()== JTextField.class) {
            foo(which);
        }
    }

有可能是一个更好的办法来做到这一点,但基本上我有JTextField的数组,程序运行正常,当用户点击了下一个JTextField的,但是当pressing进入我不知道该怎么称呼刚进入这样的JTextField我想模拟在JTextField中(这要求聚焦)点击。我想我应该刚才解释了我的整个问题。

There was probably a better way to do this, but basically I had an array of JTextFields and the program was functioning properly when users clicked on the next JTextField, but when pressing enter I didn't know how to call the JTextField that was just entered so I wanted to simulate a click on the JTextField (which calls for focus). I guess I should have just explained my whole problem.

感谢您。

这篇关于你如何模拟一个JTextField的点击? JButton的doClick等效()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆