java - 为什么我的 KeyListener 不做任何事情?

发布于 2024-12-26 21:48:51 字数 2519 浏览 4 评论 0原文

当我尝试按空格键时,没有任何反应。我用不同的键尝试过,但没有任何效果。有人可以告诉我我做错了什么吗?或者为什么这不起作用?

我还使用 Java SE 1.6 来编译它。

这是我的代码:

package bigbass1997;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.*;

@SuppressWarnings("serial")
public class Main extends JFrame implements KeyListener, MouseListener, MouseMotionListener{

    // VARIABLES
    public static String title = "Royal Casino";
    public static String author = "bigbass1997";
    public static String version = "0.0.0";

    GamePanel gp;

    public Main(){
        gp = new GamePanel();
        this.setSize(GamePanel.gameDim);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
        this.setTitle(title + " " + version);
        this.setResizable(false);
        this.setLocationRelativeTo(null);
        this.addKeyListener(this);
        this.addMouseListener(this);
        this.addMouseMotionListener(this);
        this.add(gp);
    }

    @Override
    public void keyPressed(KeyEvent e) {
        int key = e.getKeyCode();
        if(key == KeyEvent.VK_SPACE){
            Slots.slotsThread.start();
            System.out.println("Slot THREAD Started"); 
            GamePanel.slotsplaying = true;
        }
    }

    public static void main(String[] args) {
        @SuppressWarnings("unused")
        Main m = new Main();
    }

    @Override
    public void keyReleased(KeyEvent e) {
        // TODO Auto-generated method stub

    }


    @Override
    public void keyTyped(KeyEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseDragged(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseMoved(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseClicked(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseEntered(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseExited(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mousePressed(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseReleased(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }

}

When I try hitting the Space Bar nothing happens. I tried this with different keys but nothing works. Could someone please tell me what I am doing wrong? or why this isnt working?

Also I am using Java SE 1.6 to compile this.

Here is my Code:

package bigbass1997;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.*;

@SuppressWarnings("serial")
public class Main extends JFrame implements KeyListener, MouseListener, MouseMotionListener{

    // VARIABLES
    public static String title = "Royal Casino";
    public static String author = "bigbass1997";
    public static String version = "0.0.0";

    GamePanel gp;

    public Main(){
        gp = new GamePanel();
        this.setSize(GamePanel.gameDim);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
        this.setTitle(title + " " + version);
        this.setResizable(false);
        this.setLocationRelativeTo(null);
        this.addKeyListener(this);
        this.addMouseListener(this);
        this.addMouseMotionListener(this);
        this.add(gp);
    }

    @Override
    public void keyPressed(KeyEvent e) {
        int key = e.getKeyCode();
        if(key == KeyEvent.VK_SPACE){
            Slots.slotsThread.start();
            System.out.println("Slot THREAD Started"); 
            GamePanel.slotsplaying = true;
        }
    }

    public static void main(String[] args) {
        @SuppressWarnings("unused")
        Main m = new Main();
    }

    @Override
    public void keyReleased(KeyEvent e) {
        // TODO Auto-generated method stub

    }


    @Override
    public void keyTyped(KeyEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseDragged(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseMoved(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseClicked(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseEntered(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseExited(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mousePressed(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseReleased(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }

}

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

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

发布评论

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

评论(2

岛歌少女 2025-01-02 21:48:52

您将 KeyListener 添加到了错误的 JComponent。尝试一下:

this.getGlassPane().addKeyListener(this);

有关此问题的 Swing 库如何工作的更多信息可以在此处找到:http://docs.oracle.com/javase/tutorial/uiswing/components/rootpane.html

You're adding the KeyListener to the wrong JComponent. Try this:

this.getGlassPane().addKeyListener(this);

More information about how the Swing library works concerning this issue can be found here: http://docs.oracle.com/javase/tutorial/uiswing/components/rootpane.html

寻找我们的幸福 2025-01-02 21:48:51

这是一个焦点问题——KeyListener 仅在正在侦听的组件具有焦点时才起作用。请改用按键绑定。 Java Swing 教程中有一个关于该主题的不错的按键绑定教程

例如:

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.*;

@SuppressWarnings("serial")
public class BigBassMain extends JFrame {

    private static final String SPACE_BAR = "space bar";
   // VARIABLES
    public static String title = "Royal Casino";
    public static String author = "bigbass1997";
    public static String version = "0.0.0";

    GamePanel gp;

    public BigBassMain(){
        gp = new GamePanel();
        this.setSize(GamePanel.gameDim);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
        this.setTitle(title + " " + version);
        this.setResizable(false);
        this.setLocationRelativeTo(null);
        this.add(gp);

        ActionMap actionMap = gp.getActionMap();
        InputMap inputMap = gp.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);

        inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0), SPACE_BAR);
        actionMap.put(SPACE_BAR, new AbstractAction() {

         @Override
         public void actionPerformed(ActionEvent arg0) {
            Slots.slotsThread.start();
            System.out.println("Slot THREAD Started"); 
            GamePanel.slotsplaying = true;
         }
      });
    }

    public static void main(String[] args) {
        @SuppressWarnings("unused")
        BigBassMain m = new BigBassMain();
    }

}

另外,你真的不想让你的 GUI 类实现你的监听器,因为它要求一个可怜的小类做太多事情。相反,要么为侦听器使用匿名内部类,要么使用单独的类。

It's a focus issue -- KeyListeners only work if the component that is being listened to has the focus. Use Key Bindings instead. There's a decent Key Bindings tutorial on the subject at the Java Swing tutorials.

For example:

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.*;

@SuppressWarnings("serial")
public class BigBassMain extends JFrame {

    private static final String SPACE_BAR = "space bar";
   // VARIABLES
    public static String title = "Royal Casino";
    public static String author = "bigbass1997";
    public static String version = "0.0.0";

    GamePanel gp;

    public BigBassMain(){
        gp = new GamePanel();
        this.setSize(GamePanel.gameDim);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
        this.setTitle(title + " " + version);
        this.setResizable(false);
        this.setLocationRelativeTo(null);
        this.add(gp);

        ActionMap actionMap = gp.getActionMap();
        InputMap inputMap = gp.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);

        inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0), SPACE_BAR);
        actionMap.put(SPACE_BAR, new AbstractAction() {

         @Override
         public void actionPerformed(ActionEvent arg0) {
            Slots.slotsThread.start();
            System.out.println("Slot THREAD Started"); 
            GamePanel.slotsplaying = true;
         }
      });
    }

    public static void main(String[] args) {
        @SuppressWarnings("unused")
        BigBassMain m = new BigBassMain();
    }

}

Also, you really don't want to have your GUI classes implement your listeners as it's asking one poor little class to do too much. Instead either use anonymous inner classes for your listeners or separate classes.

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