如何使keyPress与keyListener一起使用 [英] How to make keyPress work with keyListener

查看:229
本文介绍了如何使keyPress与keyListener一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是来自Simon的代码,其中我已经运行显示了应该显示的正确段但是我的keyPress有问题,并且当我使用它时它不会使用箭头键点亮。不是一个伟大的编码器,我真的需要一些帮助。

Below is a code from "Simon" in which I have running showing the correct segments that are supposed to be shown but I am have trouble with my keyPress and it will not light up using the arrow keys when I use it. Not really a great coder and I really need some help with this.

import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.geom.Arc2D;

import javax.swing.Timer;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Simon extends JFrame implements KeyListener {

    // public DrawStuff game;
public static Simon simon;
Graphics2D g2;
Graphics2D g3;
Graphics2D g4;
Graphics2D g5;

public JFrame frame = new JFrame();
public JPanel panel = new JPanel();
private ActionListener timerAction;

public Simon() {
    frame = new JFrame();
    panel = new JPanel();
    // Sets up JFrame
    frame.setTitle("Simon Says");
    frame.setSize(500, 500);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    DrawStuff game = new DrawStuff();
    panel.setLayout(new BorderLayout());
    panel.add(game, BorderLayout.CENTER);
    frame.add(panel);
    frame.addKeyListener(this);
    // game.setFocusable(true);
    // game.requestFocus();
    frame.setVisible(true);

 }

 public class DrawStuff extends JPanel {

    public void paint(Graphics g) {

        g2 = (Graphics2D) g;
        g3 = (Graphics2D) g;
        g4 = (Graphics2D) g;
        g5 = (Graphics2D) g;
        // assume d == 145 && e == 90

        g2.setPaint(Color.BLUE.darker());
        g2.fill(new Arc2D.Double(45, 45, 400, 400, 145, 90, Arc2D.PIE));

        g3.setPaint(Color.RED.darker());
        g3.fill(new Arc2D.Double(45, 45, 400, 400, 235, 90, Arc2D.PIE));

        g4.setPaint(Color.GREEN.darker());
        g4.fill(new Arc2D.Double(45, 45, 400, 400, 325, 90, Arc2D.PIE));

        g5.setPaint(Color.YELLOW.darker());
        g5.fill(new Arc2D.Double(45, 45, 400, 400, 55, 90, Arc2D.PIE));
    }

 }

 public void keyPressed(KeyEvent e) {
    if (e.getKeyCode() == KeyEvent.VK_UP) {
        g5.setPaint(Color.YELLOW);
        //Color YELLOW = Color.WHITE;

    }
    if (e.getKeyCode() == KeyEvent.VK_LEFT) {
        g2.setPaint(Color.BLUE.darker());
        //Color BLUE = Color.WHITE;

    }
    if (e.getKeyCode() == KeyEvent.VK_DOWN) {
        g3.setPaint(Color.RED.darker());
        //Color RED = Color.WHITE;
    }
    if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
        g4.setPaint(Color.GREEN.darker());
        //Color GREEN = Color.WHITE;
    }
    panel.repaint();
}

public void keyReleased(KeyEvent e) {
    /**
     * if (e.getKeyCode() == KeyEvent.VK_UP) { Color YELLOW = Color.YELLOW;
     * repaint(); }
     * 
     * if (e.getKeyCode() == KeyEvent.VK_LEFT) { Color BLUE = Color.BLUE;
     * repaint(); }
     * 
     * if (e.getKeyCode() == KeyEvent.VK_DOWN) { Color RED = Color.RED;
     * repaint(); }
     * 
     * if (e.getKeyCode() == KeyEvent.VK_RIGHT) { Color GREEN = Color.GREEN;
     * repaint(); }
     **/
}

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

public static void main(String[] args) {
    simon = new Simon();

}
}


推荐答案

你在这里做了一些猜测,这不会起作用。相反,我建议你:

You're doing a bit of guessing here, and that's not going to work. Instead, I recommend that you:


  • 首先,使用Key Bindings,而不是KeyListener,因为Key Bindings允许你绕过KeyListener没有使用kludge的焦点问题。您可以在此处找到密钥绑定教程:密钥绑定教程

  • 接下来,永远不要尝试从绘制方法中提取Graphics对象,并在绘制方法之外使用它,除非在绘制方法直接调用的方法中。您已经提取并尝试在MouseListener中使用的Graphics对象不是长期存在的对象,当您尝试使用它们时甚至可能为null。

  • 相反,请更改您的类在MouseListener中的变量的状态,然后使用绘制方法中的那些变量的状态来告诉它绘制什么。请参阅执行自定义绘画教程,了解有关如何使用的更多信息用Swing绘制。

  • 确保当按下正确的键时,您将一个饼图切得更暗,并且所有其他饼图正常

  • First and foremost, use Key Bindings, not a KeyListener as Key Bindings allow you to get around the KeyListener focus issue without using a kludge. You can find the Key Binding tutorial here: Key Binding Tutorial.
  • Next, never try to extract a Graphics object from a painting method and use it outside of the painting method, unless within a method that is directly called by the painting method. That Graphics object that you've extracted and are trying to use within your MouseListener are not long-lived objects, and might even be null when you try to use them.
  • Instead, change the state of your class's variables within your MouseListener and then use the state of those variables within your painting method to tell it what to paint. Look at the Performing Custom Painting Tutorial for more on how to draw with Swing.
  • Make sure that when a proper key is pressed that you draw that one pie slice darker, and all the other pie slices normal.

其他问题:


  • 你的类扩展了JFrame然而你又创建了另一个JFrame。不要这样做,要么拥有并使用一个JFrame,要么不要同时使用它们,因为这样只会让你以后感到困惑。

  • 你的绘图JPanel应该有 paintComponent 方法重写了它的 paint 方法。

  • 您需要在覆盖范围内调用super的绘画方法。因此,如果您像我建议的那样覆盖paintComponent,请在覆盖的第一行调用 super.paintComponent(g); ,因为这将允许JPanel执行其操作家务绘画,包括擦除旧脏图像。

  • your class extends JFrame and yet you create another JFrame. Don't do this, either have and use one JFrame or the other, but don't have both as that will only confuse you later.
  • Your drawing JPanel should have its paintComponent method overridden not its paint method.
  • You need to call the super's painting method within your override. So if you're overriding paintComponent like I have suggested, call super.paintComponent(g); on the first line of your override, as this will allow the JPanel to do its house-keeping painting, including erasing old dirty images.

关于我正在谈论的一个例子,请参阅我的回答是类似的问题。这将创建此GUI:

For an example of just what I'm talking about, please see this answer of mine to a similar question. This will create this GUI:

< img src =https://i.stack.imgur.com/Crjwq.jpgalt =在此处输入图像说明>

这篇关于如何使keyPress与keyListener一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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