2

我有一些简单的代码,我试图将键盘事件输入到 Java 小程序中。使用 appletviewer 运行代码时运行良好,但是当我从浏览器加载它时(尝试了 Chrome 和 Firefox),JApplet 不会专注于点击。

使用 Applet 而不是 JApplet 尝试完全相同的代码没有问题。

这是我的代码:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Test extends JApplet {
    String s = "";

    public void init() {
            setFocusable(true);
            setEnabled(true);
            addKeyListener(new KeyAdapter() {
                    @Override
                    public void keyPressed(KeyEvent e) {
                            s = "KEY PRESSED: " + e.getKeyCode();
                            repaint();
                    }

            });
            addMouseListener(new MouseAdapter() {
                     @Override
                     public void mousePressed(MouseEvent e) {
                             boolean ret = requestFocusInWindow();
                             s = "requestFocusInWindow: " + ret;
                             repaint();
                     }
            });
            requestFocusInWindow();
    }

    public void start() {
            requestFocusInWindow();
    }

    public void paint(Graphics g) {
            super.paint(g);
            requestFocusInWindow();
            g.setColor(Color.BLACK);
            s = "Focus owner: " + isFocusOwner() + ", " + s;
            g.drawString(s, 24, 24);
    }
}
4

1 回答 1

2
  • 应通过将代码包装在块Event Dispatch Thread中的重写init()方法中来创建小程序SwingUtilities.invokeAndWait()

  • 不要KeyListener用于JApplet/Swing 组件使用KeyBindings

  • requestFocusInWindow()在创建所有内容并将所有内容添加到容器后调用JApplet(虽然这不是键绑定所必需的)

  • 也不要画图,paint()而是添加JPanel到容器中并覆盖paintComponent(..)

这是一个小例子,它是一个简单JLabel的带有虚拟标签和文本字段的容器,仅使用KeyBindingfor A;所以当按下A时,它将被添加到JLabel文本中:

在此处输入图像描述

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.lang.reflect.InvocationTargetException;
import javax.swing.AbstractAction;
import javax.swing.JApplet;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;

public class Test extends JApplet {

    @Override
    public void init() {
        try {
            SwingUtilities.invokeAndWait(new Runnable() {
                @Override
                public void run() {

                    final JLabel label = new JLabel("Text:");
                    final JLabel label2 = new JLabel("Dummy label");
                    final JTextField jtf = new JTextField("Dummy Field");
                    label2.setFocusable(true);
                    label.setFocusable(true);

                    //allwos user to add letter A to JLabel
                    label.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_A, 0), "A");
                    label.getActionMap().put("A", new AbstractAction() {
                        @Override
                        public void actionPerformed(ActionEvent ae) {
                            String tmp = label.getText();
                            label.setText(tmp + "A");

                        }
                    });

                    setLayout(new GridLayout(3, 1));

                    add(label);
                    add(label2);
                    add(jtf);
                }
            });
        } catch (InterruptedException | InvocationTargetException ex) {
            ex.printStackTrace();
        }
    }
}
于 2012-12-06T17:28:54.137 回答