KeyListener无法正常工作? [英] KeyListener won't work?

查看:110
本文介绍了KeyListener无法正常工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个非常简单的2帧程序,称为Duck Simulator. 这有一个JFrame和2张图片.如果您想知道它的作用,那只是一个JFrame,上面有鸭子坐在池塘里的起始图片.它的JLabel说按D喝水!"并且当您按D时,应该将图像设置为鸭子喝水. 它显示了坐在JFrame池塘中的鸭子的打开图像,但是当我按D时,它什么也没做.

I am trying to make a very simple 2-frame program called Duck Simulator. This has a JFrame and 2 pictures. If you want to know what it does, it just is a JFrame with a starting picture of a duck sitting in a pond. It has a JLabel saying "Press D to drink water!" And when you press D, it is supposed to set the image to the duck drinking. It shows the opening image of the duck sitting in the pond in the JFrame, but when I press D, it doesn't do anything.

这是代码:

package net.ducksimulator.classes;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class a implements KeyListener {
static JFrame f;
public static void main (String[] args) {
    f = new JFrame("Duck Simulator ALPHA");
    try {
        f.setContentPane(new JLabel(new ImageIcon(ImageIO.read(new File("images/duck-sitting.png")))));
    } catch (IOException e) {
        System.out.println("Image doesn't exist.");
    }

    f.setResizable(false);
    f.setVisible(true);

    JLabel l = new JLabel("Press D to drink water!");
    l.setBounds(250,20,100,10);
    f.add(l);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.pack();
    f.setLocationRelativeTo(null);
}

public void drink() throws IOException {
    f.setContentPane(new JLabel(new ImageIcon(ImageIO.read(new File("images/duck-drinking.png")))));
}

public void sit() throws IOException {
    f.setContentPane(new JLabel(new ImageIcon(ImageIO.read(new File("images/duck-sitting.png")))));
}

@Override
public void keyPressed(KeyEvent e) {
    int key = e.getKeyCode();
    if (key == KeyEvent.VK_D) {
        try {
            drink();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }
}

@Override
public void keyReleased(KeyEvent e) {
    int key = e.getKeyCode();
    if (key == KeyEvent.VK_D) { 
        try {
            System.out.println("Bagels");
            sit();
        } catch (IOException e1) {

            e1.printStackTrace();
        }
    }
}

@Override
public void keyTyped(KeyEvent e) {
}
}

推荐答案

仅实现KeyListener是不够的,您需要指定要接收键事件的组件.

Simply implementing KeyListener isn't enough, you need to specify which component you want to receive key events.

这是问题开始的地方. KeyListener将仅针对具有焦点且可聚焦的组件引发事件.

This is where the problems start. KeyListener will only raise events for components that have focus AND are focusable.

更好的解决方案是使用键绑定API 可以更好地控制组件触发关键事件之前所需的焦点级别

A better solution would be to use the key bindings API which provides you with better control over the level of focus a component needs before it will trigger key events

至少出于两个原因,您应该避免使用setBounds.

You should avoid using setBounds for, at least, two reasons.

首先,将组件添加到的容器在布局管理器(在本例中为BorderLayout)的控制下,这使得setBounds的使用毫无意义,其次,您不控制将更改在不同平台(例如字体规格和渲染管道)上显示组件时可能需要的空间量.让布局经理在这里完成工作

Firstly, the container you adding the component to is under the control of a layout manager (BorderLayout in this instance), which makes the use of setBounds pointless and secondly, you don't control the factors which will alter the required amount of space a component might need when presented on different platforms, such as font metrics and rendering pipelines. Let the layout managers do there job

这篇关于KeyListener无法正常工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆