JApplet/JPanel 未接收 KeyListener 事件!

发布于 2024-08-09 04:39:39 字数 3093 浏览 3 评论 0原文

我无法让 JApplet 中的 JPanel 接收键盘事件。我不明白为什么!

请注意...

  1. 在输入之前单击面板(用鼠标)没有什么区别。这是迄今为止我在网上看到的最常见的建议。

  2. 我尝试使用“低级”java.awt.KeyEventDispatcher 接口。这也没有什么不同!

  3. 但是,如果我使用 Applet 而不是 JApplet,则 Applet 确实会接收键盘事件。但即使在这里,当我向该 Applet 添加面板时(该面板实际上是我所有应用程序/绘画逻辑所在的位置),我再次停止接收 kb 事件(在我的面板中)!

  4. 现在,我不能简单地使用 Applet(而不是 JApplet),因为除其他外,它的 onPaint 获取 Graphics(而不是 Graphics2D 对象)。所以,#3 不是我的解决方案。

  5. 在 JDK 附带的 AppletViewer 中,事情就像一个魅力。

我在这里迫切需要有人的帮助。过去 2-3 天尝试了各种排列,我现在都不记得了。

我的平台详细信息:

  1. Firefox 3.5.3

  2. x86 上的 Fedora 11(包含最新更新/补丁)

  3. Java 插件:尝试了这两个,没有什么区别。

    3.1 IcedTea Java Web 浏览器插件 1.6 (fedora-29.b16.fc11-i386)

    3.2 jdk1.6.0_16/jre/plugin/i386/ns7/libjavaplugin_oji.so

  4. 使用上面的 jdk1.6.0_16 来编译我的小程序源.

这是我的代码。非常感谢收到我的程序员同事的来信……因为我完全陷入困境了!

谢谢,

/SD

import java.awt.event.*;
import java.awt.*;

import javax.swing.*;
import javax.swing.event.*;


class MyAppletKeyListener implements KeyListener, MouseListener {

  public void keyPressed(KeyEvent e) {
    System.out.println("panel:keyPressed" + e.getKeyChar());
  }
  public void keyReleased(KeyEvent e) {
  }
  public void keyTyped(KeyEvent e) {
    System.out.println("panel:keyTyped" + e.getKeyChar());
  }

  public void mouseClicked(MouseEvent e) {
    System.out.println("panel:mouseClicked");
  }
  public void mouseEntered(MouseEvent e) { }
  public void mouseExited(MouseEvent e) { }
  public void mousePressed(MouseEvent e) { }
  public void mouseReleased(MouseEvent e) { }
}


public class TestApplet extends JApplet implements MouseListener {
  public void init() {
    System.out.println("applet:init");

    MyAppletKeyListener listener = new MyAppletKeyListener();

    // Panel related
    // Note: I'd like this red panel to handle 
    // all my keyboard and mouse events.
    JPanel panel = new JPanel();
    panel.setLayout(new FlowLayout());
    panel.add(new JButton("test"));
    panel.add(new JButton("test2"));
    panel.setFocusable(true);
    panel.requestFocus();
    panel.setBackground(new Color(200, 0, 0));
    panel.addKeyListener(listener);
    panel.addMouseListener(listener);

    // applet related
    // Note: Added this only for debugging. I do NOT want
    // to handle my mouse/kb events in the applet.
    addMouseListener(this);
    getContentPane().setLayout(new FlowLayout());
    getContentPane().add(panel);

  }

  public void mouseClicked(MouseEvent e) {
    System.out.println("applet:mouseClicked");
  }
  public void mouseEntered(MouseEvent e) { }
  public void mouseExited(MouseEvent e) { }
  public void mousePressed(MouseEvent e) { }
  public void mouseReleased(MouseEvent e) { }
}

HTML:

<html>
  <head>
  </head>
  <body>
    <applet id="myApplet" code="TestApplet.class"
        width="425"
        height="150" >
    </applet>
  </body>
</html>

I cannot get my JPanel within my JApplet to receive keyboard events. I CANNOT FATHOM why!

Note that...

  1. Clicking the panel (with mouse) before typing makes no difference. This is by far the most common advice I see given on the Net.

  2. I have tried using the 'low-level' java.awt.KeyEventDispatcher interface. That makes no different either!

  3. However, if I use Applet instead of JApplet, then the Applet DOES receive keyboard events. But even here, the moment I add a Panel to this Applet (the Panel is really where all my app/painting logic is), I once again stop receiving kb events (in my Panel)!

  4. Now, I cannot simply use Applet (instead of JApplet) because, among other things, its onPaint gets a Graphics (instead of a Graphics2D object). So, #3 is NOT a solution for me.

  5. Things work like a charm in AppletViewer that comes with JDK.

I desperately need someone's help here. Spent last 2-3 days trying all kinds of permutations I don't even recall now.

My platform details:

  1. Firefox 3.5.3

  2. Fedora 11 on x86 (with latest updates/patches)

  3. Java Plugin: tried both of these, made no difference.

    3.1 IcedTea Java Web Browser Plugin 1.6 (fedora-29.b16.fc11-i386)

    3.2 jdk1.6.0_16/jre/plugin/i386/ns7/libjavaplugin_oji.so

  4. Used the above jdk1.6.0_16 to compile my applet source.

Here's my code. Will greatly appreciate to hear from my fellow programmers... as I'm completely stuck!

Thanks,

/SD

import java.awt.event.*;
import java.awt.*;

import javax.swing.*;
import javax.swing.event.*;


class MyAppletKeyListener implements KeyListener, MouseListener {

  public void keyPressed(KeyEvent e) {
    System.out.println("panel:keyPressed" + e.getKeyChar());
  }
  public void keyReleased(KeyEvent e) {
  }
  public void keyTyped(KeyEvent e) {
    System.out.println("panel:keyTyped" + e.getKeyChar());
  }

  public void mouseClicked(MouseEvent e) {
    System.out.println("panel:mouseClicked");
  }
  public void mouseEntered(MouseEvent e) { }
  public void mouseExited(MouseEvent e) { }
  public void mousePressed(MouseEvent e) { }
  public void mouseReleased(MouseEvent e) { }
}


public class TestApplet extends JApplet implements MouseListener {
  public void init() {
    System.out.println("applet:init");

    MyAppletKeyListener listener = new MyAppletKeyListener();

    // Panel related
    // Note: I'd like this red panel to handle 
    // all my keyboard and mouse events.
    JPanel panel = new JPanel();
    panel.setLayout(new FlowLayout());
    panel.add(new JButton("test"));
    panel.add(new JButton("test2"));
    panel.setFocusable(true);
    panel.requestFocus();
    panel.setBackground(new Color(200, 0, 0));
    panel.addKeyListener(listener);
    panel.addMouseListener(listener);

    // applet related
    // Note: Added this only for debugging. I do NOT want
    // to handle my mouse/kb events in the applet.
    addMouseListener(this);
    getContentPane().setLayout(new FlowLayout());
    getContentPane().add(panel);

  }

  public void mouseClicked(MouseEvent e) {
    System.out.println("applet:mouseClicked");
  }
  public void mouseEntered(MouseEvent e) { }
  public void mouseExited(MouseEvent e) { }
  public void mousePressed(MouseEvent e) { }
  public void mouseReleased(MouseEvent e) { }
}

The HTML:

<html>
  <head>
  </head>
  <body>
    <applet id="myApplet" code="TestApplet.class"
        width="425"
        height="150" >
    </applet>
  </body>
</html>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

阪姬 2024-08-16 04:39:39

我在网上找到了这个,它解决了我的问题:

至于 KeyListener 的作用
不适用于 JApplet,因为它适用于
小程序你应该使用
KeyEventDispatcher接口。

公共类 AppletMain 扩展 JApplet 实现

java.awt.KeyEventDispatcher

此外,您还必须设置
KeyboardFocusManager 到面板

KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(this);

然后覆盖
的dispatchKeyEvent函数
接口:

<前><代码>@Override
公共布尔dispatchKeyEvent(KeyEvent e);

这允许您捕获 KeyEvents
就像使用 KeyListener 完成的一样。

I found this on the net, and it solves the issue for me:

As for the fact that KeyListener does
not work for JApplet as it does for
Applet you should use the
KeyEventDispatcher interface.

public class AppletMain extends JApplet implements

java.awt.KeyEventDispatcher

Furthermore you have to set the
KeyboardFocusManager to the Panel

KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(this);

Afterwards override the
dispatchKeyEvent function of the
interface:

@Override
public boolean dispatchKeyEvent(KeyEvent e);

This allows you to catch the KeyEvents
as it is done with KeyListener.

请持续率性 2024-08-16 04:39:39

我调查了与我当前项目相关的问题,并探讨了 JApplet 类的可聚焦性的一些问题。
正是因为为什么setFocusable(true);决定了这个问题。
您最终可能还需要添加焦点捕获调用,例如 requestFocusInWindow(); 以使其正常工作。

I investigated the problem connected to my current project and explored some problems with focusability of JApplet class.
It is because why setFocusable(true);decided the problem.
You also may eventually need to add focus-capture call such as requestFocusInWindow(); to make it work propertly.

久而酒知 2024-08-16 04:39:39

我在 Ubuntu 9.04 和 10.10 以及 Firefox 版本 3.6.11 和 3.6.14 中使用 sun-java-6 软件包和 openjdk 软件包时遇到了这个问题。我发现了两种解决方法:使用 Applet 而不是 JApplet,或者实现一个在 mousePressed(..) 函数中调用“requestFocus()”的 MouseListener。

I had this problem with the sun-java-6 packages and the openjdk packages in both Ubuntu 9.04 and 10.10 with firefox version 3.6.11 and 3.6.14. I've discovered two workarounds: use Applet rather than JApplet, or implement a MouseListener which calls "requestFocus()" in the mousePressed(..) function.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文