如何将注意力集中在JTextField上? [英] How to Set Focus on JTextField?

查看:121
本文介绍了如何将注意力集中在JTextField上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我让我的游戏没有鼠标运行,所以使用指针不是一个选择。高分菜单将显示玩家何时失败。

I make my game run without mouse so using pointer is not a choice. High Score menu will show when player lose.

这是我的代码

    highScore=new MyTextField("Your Name");
    highScore.addKeyListener(this);
    highScore.setFont(font);
    highScore.requestFocusInWindow();

我试过了

highScore.setFocusable(true);
highScore.requestFocusInWindow();
highScore.requestFocus(true);
highScore.requestFocus();

但仍未获得关注我的 JTextField

but still not gained focus on my JTextField.

如何关注它?

推荐答案

如果你想要你的话当你的GUI显示时,要关注 JTextField ,你可以使用它:

If you want your JTextField to be focused when your GUI shows up, you can use this:

in = new JTextField(40);
f.addWindowListener( new WindowAdapter() {
    public void windowOpened( WindowEvent e ){
        in.requestFocus();
    }
}); 

其中 f 将是你的 JFrame 和中的是您的 JTextField

这篇关于如何将注意力集中在JTextField上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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