如何在我的鼓组程序中实现KeyListener?

问题描述 投票:-1回答:2

我正在尝试制作一个基于键输入播放的鼓组,但键输入没有到达阵列列表。

我已经尝试将数组列表放在不同的程序中,我尝试过字符串和字符。

package instruments;

'' 'Java的' ''

鼓组

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.KeyEventDispatcher;
import java.awt.event.KeyEvent;
import java.awt.image.BufferStrategy;
import java.util.ArrayList;

import javax.swing.*;

public class Drumset extends Canvas  implements Runnable{

static KeyEvent keyevent;
static MyKeyListener keys = new MyKeyListener();
static ArrayList<String> instrument = new ArrayList<String>();//collects key inputs
public static final int width = 800, height = width;
public Thread thread;
public boolean running = false;


public Drumset(){
    this.addKeyListener(keys);
    new Window(width,height,"Drumset", this);

}


public synchronized void start(){
    thread = new Thread(this);
    thread.start();
    running = true;
}

public synchronized void stop(){
    try{
        thread.join();
        running = false;
    }catch(Exception e){
        e.printStackTrace();
    }
}



public void findInstrument(){

}


public void run() {
    long lastTime = System.nanoTime();
    double amountofticks = 60.0;
    double ns = 1000000000 / amountofticks;
    double delta = 0;
    long timer = System.currentTimeMillis();
    int frames = 0;
    while(running){
        long now = System.nanoTime();
        delta += (now - lastTime) / ns;
        lastTime = now;
        while (delta>= 1){
            tick();
            delta--;
        }
    findInstrument();
    if(running){
        render();
        frames ++;
    }
    if (System.currentTimeMillis() - timer > 1000){
        timer += 1000;
        System.out.println("FPS: " + frames);
        frames = 0;
    }
    }
    stop();

}

public void tick(){

}

public void render(){
    BufferStrategy buffer = this.getBufferStrategy();
    if (buffer == null){
        this.createBufferStrategy(3);
        return;
    }
    Graphics pen = buffer.getDrawGraphics();

    pen.setColor(Color.black);
    pen.fillRect(0,0,width,height);

    buffer.show();

}
public static void main(String args[]){
    new Drumset();

}

}


MyKeyListener

package instruments;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;

public class MyKeyListener implements KeyListener {


private ArrayList<Character> keyInstruments = new ArrayList<Character>();

public MyKeyListener() {
}

public void keyPressed(KeyEvent e) {
    int key = e.getKeyCode();
    System.out.println(key);
//          bass
            if (key == (KeyEvent.VK_B)){
                keyInstruments.add('b');
            }
//          hi-hat
            else if (key == (KeyEvent.VK_H)){
                keyInstruments.add('h');
            }
//          crash cymbal
            else if (key == (KeyEvent.VK_C)){
                keyInstruments.add('c');
            }
//          floor tom
            else if (key == (KeyEvent.VK_F)){
                keyInstruments.add('f');
            }
//          high tom
            else if (key == (KeyEvent.VK_T)){
                keyInstruments.add('t');
            }
//          low tom
            else if (key == (KeyEvent.VK_L)){
                keyInstruments.add('l');
            }
//          ride cymbal
            else if (key == (KeyEvent.VK_R)){
                keyInstruments.add('r');
            }
        }


public void keyReleased(KeyEvent e) {
    for(int c = 0; c < keyInstruments.size(); c++) {
        if(keyInstruments.get(c) == e.getKeyChar()) {
            keyInstruments.remove(c);
            return;
        }
    }
}
public int getSize(){
    return keyInstruments.size();
}

public void keyTyped(KeyEvent e) {
}

public ArrayList<Character> getKey() {
    return keyInstruments;
}

}

我希望输出是按下的键,也包含在KeyPressed方法中。

java keylistener
2个回答
0
投票

所以我需要做的就是在我的窗口类中添加keylistener而不是我的Drumset类。

JFrame frame = new JFrame(title);
frame.addKeyListener(listen);

-1
投票

在你的drumset类中,你初始化一个名为keys的静态MyKeyListener(),但是你似乎以后在代码中没有使用它。

我建议更换

public Drumset(){
        this.addKeyListener(new MyKeyListener());
        new Window(width,height,"Drumset", this);

    }

public Drumset(){
        this.addKeyListener(keys);
        new Window(width,height,"Drumset", this);

    }

这可能不是正确的答案,但它是我能做的最好的帮助。

© www.soinside.com 2019 - 2024. All rights reserved.