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

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

问题描述

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

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

我有一个可打开JFrame的小程序,我正在使用KeyListener侦听键盘输入.这在我的开发环境(日食)中可以正常工作,但是当我通过浏览器运行小程序(我尝试过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天全站免登陆