Java Applet 中的键盘输入 [英] Keyboard input in Java Applet

查看:26
本文介绍了Java Applet 中的键盘输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Java Applet 中监听键盘输入的最佳方式是什么?

What is the best way to listen for keyboard input in a Java Applet?

我有一个可以打开 JFrame 的小程序,我正在使用 KeyListener 来监听键盘输入.这在我的开发环境 (eclipse) 中运行良好,但是当我通过浏览器运行小程序时(我尝试过 Firefox 和 IE),它不响应键盘事件.但是,如果我运行小程序,然后最小化和最大化框架,它就可以工作.我尝试以多种不同的方式将焦点设置到 JFrame,并以编程方式最小化和最大化它,但没有效果.我也尝试过按键绑定,但遇到同样的问题.

I have an applet which opens a JFrame and I am using a KeyListener to listen for keyboard input. This works fine in my development environment (eclipse), but when I run the applet through a browser (I have tried Firefox and IE) it does not respond to keyboard events. However, if I run the applet and then minimize and maximize the frame, it works. I have tried setting focus to the JFrame in many different ways and also programmatically minimizing and maximizing it, but to no effect. I have also tried key bindings, but with the same problem.

我已将我的代码精简到问题的最基本要素,并将其粘贴在下方.有人能看到我做错了什么或提出更好的解决方案吗?

I have trimmed my code down to the barest essentials of the problem and pasted it below. Can someone see what I am doing wrong or suggest a better solution?

public class AppletTest extends Applet 
{    
    private GuiTest guiTest; 

    public void init() {
        guiTest = new GuiTest();
        final AppletTest at = this;
        guiTest.addKeyListener(new KeyListener() {
            public void keyPressed(KeyEvent ke) {
                at.keyPressed(ke);
            }
            public void keyReleased(KeyEvent ke) {}
            public void keyTyped(KeyEvent e) {}             
        });
    }

    private void keyPressed(KeyEvent ke)
    {
        System.out.println("keyPressed "+KeyEvent.getKeyText(ke.getKeyCode()));
        getGuiTest().test(KeyEvent.getKeyText(ke.getKeyCode()));
    }
}

public class GuiTest extends JFrame {
    String teststring = "?";
    public GuiTest()
    {
        setSize(100,100);
        setEnabled(true);
        setVisible(true);
        setFocusable(true);
        requestFocus();
        requestFocusInWindow();
        toFront();
    }

    public void test(String t)
    {
        teststring = t;
        repaint();
    }

    public void paint(Graphics g)
    {
        g.setColor(Color.white);
        g.fillRect(0, 0, getWidth(), getHeight());
        g.setColor(Color.black);
        g.drawString(teststring, 50, 50);
    }
}

推荐答案

我解决了这个问题.如果我在小程序上按下按钮或鼠标事件后创建 JFrame,则 JFrame 上的键侦听器会起作用.显然,从 Applet.init() 创建框架意味着关键侦听器在通过浏览器打开时无法正常工作.

I solved the problem. If I create the JFrame following a button press or mouse event on the applet, the key listener on the JFrame works. Apparently, creating the frame from Applet.init() means that key listeners do not function correctly when opened through a browser.

然而,问题仍然存在——为什么?如果有人可以解释这一点,我将不胜感激.

However, the question remains - why? If someone can explain this, I would greatly appreciate it.

我认为可能是因为应该在事件调度线程上创建框架,但是使用 SwingUtilities.invokeLater 或 invokeAndWait 不起作用.

I thought it might be because the frame should be created on the event dispatch thread, but using SwingUtilities.invokeLater or invokeAndWait did not work.

这篇关于Java Applet 中的键盘输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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