简单的Java小程序 - 不会调用actionPerformed

时间:2013-04-30 21:32:07

标签: java swing timer applet

我正在研究一个简单的java applet,我在之前工作时applet启动,音乐播放一秒钟后启动一个简单的计时器,每秒计数(无限)。我试着在当天晚些时候运行它并且它已经停止了我想要的方式 - 我认为没有被调用的动作(也许我已经意外地改变了它)。

import java.awt.*;
import java.applet.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;

public class guitarGame extends Applet implements ActionListener, KeyListener {

AudioClip brainStew; 
Timer timer = new Timer (100, this);
String s = "";
char a;
int amount, amount2, selection;

public void init(){
    amount = 0;
    amount2 = 0;
    timer.setRepeats(true);
    timer.start();
    brainStew = getAudioClip(getDocumentBase(), "green day - brain stew.au");
    addKeyListener(this);
}

public void keyReleased(KeyEvent ae){}

public void keyPressed(KeyEvent ae){
    char keyChar = ae.getKeyChar();
    if (keyChar == 'a' ) {
        selection = 1;
    } else {
        selection = 2;
    }
    repaint();
}

public void keyTyped(KeyEvent ae){}

public void actionPerformed (ActionEvent ae){
    if(amount == 0){
        brainStew.play();
    }
    if(amount2 != 10){
        amount2++;
    }
    else{
        amount++;
        amount2 = 0;
    }
    repaint();
}

public void paint (Graphics g)
{
    //g.setColor(Color.GREEN);
    //g.fillRect(30,440,50,30);
    if (selection == 1){
        g.setColor(Color.YELLOW);
        g.fillRect(100,100,100,100);
    }
    if (selection == 2){
        g.setColor(Color.GREEN);
        g.fillRect(100,100,100,100);
    }
    super.paint(g);
    g.drawString(amount+"Seconds",400,400);
}
}

0 个答案:

没有答案