简体   繁体   English

Java:KeyListener不起作用

[英]Java: KeyListener Not working

I'm fairly new to Java, and I have totally self taught my self to this point. 我对Java还是很陌生,到目前为止,我已经完全自学了自我。

Right now I am trying to add a KeyListener to my JFrame class, I have no idea what I'm doing wrong and need some help. 现在,我试图将KeyListener添加到我的JFrame类中,我不知道自己在做什么错,需要一些帮助。 Any other tips are welcome. 欢迎其他任何提示。

My JFrame class: 我的JFrame类:

import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.*;

public class TestJavaFrame implements ActionListener, KeyListener {
    private static JFrame frame = new JFrame();

    // Componenets
    JLabel timeinmslabel = new JLabel("Enter the time in miliseconds:");

    JTextField timeinms = new JTextField();

    JRadioButton checkBox = new JRadioButton();
    JRadioButton checkBox2 = new JRadioButton();

    private boolean amountoftimes = false;

    public TestJavaFrame(String windowname) {
        frame.setName(windowname);
        frame.setResizable(true);
        frame.setSize(900, 500);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocation(150, 50);

        // JPanel
        JPanel panel = new JPanel();
        panel.setLayout(null);
        panel.setBounds(frame.getBounds());

        // Bounds for components
        timeinmslabel.setBounds((int) (frame.getBounds().getX() / 2), 125, 200, 25);
        timeinms.setBounds((int) (frame.getBounds().getX() / 2 + 185), 125, 200, 25);
        checkBox.setBounds((int) (frame.getBounds().getX() / 2 + 185), 40, 200, 25);
        checkBox2.setBounds((int) (frame.getBounds().getX() / 2 + 185), 70, 200, 25);

        // Action Listeners
        checkBox.addActionListener(this);
        frame.addKeyListener(this);

        // edit components
        checkBox.setText("Use clicked amount of times.");

        // add components
        panel.add(timeinmslabel);
        panel.add(timeinms);
        panel.add(checkBox);
        panel.add(checkBox2);
        frame.add(panel);

        frame.setVisible(true);

    }

    public void actionPerformed(ActionEvent e) {
        System.out.println(e);
    }

    @Override
    public void keyPressed(KeyEvent e) {
        System.out.println(e.getKeyChar());
    }

    @Override
    public void keyReleased(KeyEvent e) {
        System.out.println(e.getKeyChar());
    }

    @Override
    public void keyTyped(KeyEvent e) {
        System.out.println(e.getKeyChar());
    }
}

If you need my main, I'm perfectly fine with posting it, all it does right now is create this gui though. 如果您需要我的主工具,我可以很好地发布它,但是现在要做的就是创建此gui。

So when the user hits the specific key it stops the auto clicker 因此,当用户按下特定键时,它将停止自动点击器

Yes you can add the Key Binding to the panel. 是的,您可以将按键绑定添加到面板中。

A better approach is to create a menu bar for the various Actions support by your application. 更好的方法是为应用程序的各种Actions支持创建菜单栏。 Then you can have menu items to start/stop the clicker. 然后,您可以使用菜单项来启动/停止答题器。 When you create the menu items you can then assign an accelerator to the menu item and the menu item will create the key bindings for you automatically. 创建菜单项时,可以将加速器分配给菜单项,菜单项将自动为您创建按键绑定。

This is a better solution because the "key binding" is self documenting since it is part of the menu item. 这是一个更好的解决方案,因为“键绑定”是自记录的,因为它是菜单项的一部分。

Read the section from the Swing tutorial on How to Use Menus for more information and working examples to get you started. 阅读Swing教程中有关如何使用菜单的部分, 获取更多信息和工作示例,以开始使用。

as I said I'm trying to learn here. 正如我说的,我想在这里学习。

Keep a link to the tutorial handy for all Swing basics. 保留所有Swing基础知识到本教程的链接。 There are also sections on "Key Bindings" and "How to Use Actions". 也有关于“键绑定”和“如何使用动作”的部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM