Java Timer Class actionPerformed更新两秒钟

时间:2014-03-03 08:25:36

标签: java swing timer actionlistener

我目前正在实施一个简单的篮球记分牌程序。对于记分板,我使用java中内置的javax.swing.Timer类。每次定时器调用actionPerformed时,我都试图让显示减1秒。截至目前,每次调用actionPerformed时它都会减少两个。任何提示或帮助将不胜感激。谢谢!

另外,如果我没有正确地问这个或太模糊,我很抱歉。我是新来的。如果您需要澄清或需要我解决问题,请告诉我。我已经包含了片段,然后是最后的完整代码。

这是我的代码实例化定时器的一部分:

Timer clock;
/.....
clock = new Timer(1000, this);
clock.addActionListener(this);

这里是更新的地方(在actionListener方法中):

if(event.getSource() == clock){
        if(seconds > 0 && seconds < 60){
            seconds--;
        }else{
            if(seconds == 0 && minutes != 0){
                seconds = 59;
                minutes--;
            }else{
                if(minutes == 0){
                    clock.stop();
                    Toolkit.getDefaultToolkit().beep();
                }
            }
        }
        if(seconds < 10){
            clockLabel.setText(minutes + ":0" + seconds);
        }else{
        clockLabel.setText(minutes + ":" + seconds);
        }
    }

以下是我的程序的所有代码:

import javax.swing.*;

import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.Timer;

public class BasketballScoreboard extends JFrame implements ActionListener{

/**
 * 
 */
private static final long serialVersionUID = 1L;
JPanel panel;
JButton exit;//Button to exit program
JButton resetScores;//Button to reset both scores
Timer clock;
JLabel score;
JLabel fouls;
JLabel addOne;
JLabel addTwo;
JLabel addThree;
JLabel addFouls;
JLabel bonus;
JLabel homeBonus;
JLabel awayBonus;
JLabel clockLabel;
JLabel clockControls;
JButton resetFouls;
JButton addHomeFoul;
JButton addAwayFoul;
JButton home2;//Button to add 2 points to home score
JButton home1;//Button to add 1 points to home score
JButton home3;//Button to add 3 points to home score
JButton away1;//Button to add 1 points to away score
JButton away2;//Button to add 2 points to away score
JButton away3;//Button to add 3 points to away score
JButton startClock;
JButton stopClock;
JLabel home;//Home team name
JLabel away;//Away team name
JLabel homeFouls;
JLabel awayFouls;
private int homeFoulsNum = 0;
private int awayFoulsNum = 0;
JLabel homeScore;//Home score
JLabel awayScore;//Away Score
private int homeScoreNum = 0;
private int awayScoreNum = 0;
private int seconds = 00;
private int minutes = 8;
public BasketballScoreboard(){
    GridLayout scoreboardLayout = new GridLayout(14,3);

    panel = new JPanel();

    exit = new JButton("Exit");
    exit.setHorizontalAlignment(SwingConstants.CENTER);
    exit.addActionListener(this);

    clockLabel = new JLabel(minutes + ":0" + seconds);
    clockLabel.setHorizontalAlignment(SwingConstants.CENTER);
    clock = new Timer(1000, this);
    clock.addActionListener(this);

    startClock = new JButton("Start Clock");
    startClock.setHorizontalAlignment(SwingConstants.CENTER);
    startClock.addActionListener(this);

    clockControls = new JLabel("Clock Controls: ");
    clockControls.setHorizontalAlignment(SwingConstants.CENTER);

    stopClock = new JButton("Stop Clock");
    stopClock.setHorizontalAlignment(SwingConstants.CENTER);
    stopClock.addActionListener(this);

    score = new JLabel("Score: ");
    score.setHorizontalAlignment(SwingConstants.CENTER);

    fouls = new JLabel("Fouls: ");
    fouls.setHorizontalAlignment(SwingConstants.CENTER);

    bonus = new JLabel("Bonus: ");
    bonus.setHorizontalAlignment(SwingConstants.CENTER);

    homeBonus = new JLabel("");
    homeBonus.setHorizontalAlignment(SwingConstants.CENTER);

    awayBonus = new JLabel("");
    awayBonus.setHorizontalAlignment(SwingConstants.CENTER);

    addFouls = new JLabel("Add Foul: ");
    addFouls.setHorizontalAlignment(SwingConstants.CENTER);

    addOne = new JLabel("+1: ");
    addOne.setHorizontalAlignment(SwingConstants.CENTER);

    addTwo = new JLabel("+2: ");
    addTwo.setHorizontalAlignment(SwingConstants.CENTER);

    addThree = new JLabel("+3: ");
    addThree.setHorizontalAlignment(SwingConstants.CENTER);


    resetScores = new JButton("Reset Scores");
    resetScores.setHorizontalAlignment(SwingConstants.CENTER);
    resetScores.addActionListener(this);

    resetFouls = new JButton("Reset Fouls");
    resetFouls.setHorizontalAlignment(SwingConstants.CENTER);
    resetFouls.addActionListener(this);

    home = new JLabel(JOptionPane.showInputDialog("Enter Home Team Name"));
    home.setHorizontalAlignment(SwingConstants.CENTER);
    away = new JLabel(JOptionPane.showInputDialog("Enter Away Team Name"));
    away.setHorizontalAlignment(SwingConstants.CENTER);

    home1 = new JButton("Home +1");
    home1.setHorizontalAlignment(SwingConstants.CENTER);
    home1.addActionListener(this);


    home2 = new JButton("Home +2");
    home2.setHorizontalAlignment(SwingConstants.CENTER);
    home2.addActionListener(this);
    home2.setMnemonic('h');

    home3 = new JButton("Home +3");
    home3.setHorizontalAlignment(SwingConstants.CENTER);
    home3.addActionListener(this);

    addHomeFoul = new JButton("Home Foul +1");
    addHomeFoul.setHorizontalAlignment(SwingConstants.CENTER);
    addHomeFoul.addActionListener(this);

    addAwayFoul = new JButton("Away Foul +1");
    addAwayFoul.setHorizontalAlignment(SwingConstants.CENTER);
    addAwayFoul.addActionListener(this);

    away1 = new JButton("Away + 1");
    away1.setHorizontalAlignment(SwingConstants.CENTER);
    away1.addActionListener(this);

    away2 = new JButton("Away +2");
    away2.addActionListener(this);
    away2.setHorizontalAlignment(SwingConstants.CENTER);
    away2.setMnemonic('a');

    away3 = new JButton("Away +3");
    away3.setHorizontalAlignment(SwingConstants.CENTER);
    away3.addActionListener(this);

    homeScore = new JLabel("" + homeScoreNum);
    homeScore.setHorizontalAlignment(SwingConstants.CENTER);
    awayScore = new JLabel("" + awayScoreNum);
    awayScore.setHorizontalAlignment(SwingConstants.CENTER);

    homeFouls = new JLabel("" + homeFoulsNum);
    homeFouls.setHorizontalAlignment(SwingConstants.CENTER);
    awayFouls = new JLabel("" + awayFoulsNum);
    awayFouls.setHorizontalAlignment(SwingConstants.CENTER);

    panel.setLayout(scoreboardLayout);

    panel.add(new JLabel(""));
    panel.add(clockLabel);
    panel.add(new JLabel(""));

    panel.add(new JLabel(""));
    panel.add(home);
    panel.add(away);


    panel.add(score);
    panel.add(homeScore);
    panel.add(awayScore);

    panel.add(fouls);
    panel.add(homeFouls);
    panel.add(awayFouls);

    panel.add(bonus);
    panel.add(homeBonus);
    panel.add(awayBonus);

    panel.add(addOne);
    panel.add(home1);
    panel.add(away1);

    panel.add(addTwo);
    panel.add(home2);
    panel.add(away2);

    panel.add(addThree);
    panel.add(home3);
    panel.add(away3);

    panel.add(new JLabel(""));
    panel.add(new JLabel(""));
    panel.add(new JLabel(""));

    panel.add(addFouls);
    panel.add(addHomeFoul);
    panel.add(addAwayFoul);

    panel.add(clockControls);
    panel.add(startClock);
    panel.add(stopClock);


    panel.add(new JLabel(""));
    panel.add(new JLabel(""));
    panel.add(new JLabel(""));

    panel.add(resetScores);
    panel.add(resetFouls);
    panel.add(exit);

    this.add(panel);
}

@Override
public void actionPerformed(ActionEvent event){
    if(event.getSource() == home1){
        homeScoreNum++;
        homeScore.setText("" + homeScoreNum);
    }
    if(event.getSource() == home2){
        homeScoreNum += 2;
        homeScore.setText("" + homeScoreNum);
    }
    if(event.getSource() == home3){
        homeScoreNum += 3;
        homeScore.setText("" + homeScoreNum);
    }
    if(event.getSource() == addHomeFoul){
        homeFoulsNum ++;
        homeFouls.setText("" + homeFoulsNum);

        if(homeFoulsNum >= 6 && homeFoulsNum < 9){
            homeBonus.setText("\u2713");
        }
        if(homeFoulsNum >= 9){
            homeBonus.setText("\u2713\u2713");
        }

    }

    if(event.getSource() == away1){
        awayScoreNum++;
        awayScore.setText("" + awayScoreNum);
    }
    if(event.getSource() == away2){
        awayScoreNum += 2;
        awayScore.setText("" + awayScoreNum);
    }
    if(event.getSource() == away3){
        awayScoreNum += 3;
        awayScore.setText("" + awayScoreNum);
    }
    if(event.getSource() == addAwayFoul){
        awayFoulsNum++;
        awayFouls.setText("" + awayFoulsNum);

        if(awayFoulsNum >= 6 && awayFoulsNum < 9){
            awayBonus.setText("\u2713");
        }
        if(awayFoulsNum >= 9){
            awayBonus.setText("\u2713\u2713");
        }
        }

    if(event.getSource() == exit){
        System.exit(0);     }

    if(event.getSource() == resetScores){
        homeScoreNum = 0;
        awayScoreNum = 0;
        homeScore.setText("" + homeScoreNum);
        awayScore.setText("" + awayScoreNum);
    }

    if(event.getSource() == resetFouls){
        homeFoulsNum = 0;
        awayFoulsNum = 0;
        homeFouls.setText("" + homeFoulsNum);
        awayFouls.setText("" + awayFoulsNum);
        homeBonus.setText("");
        awayBonus.setText("");

    }

    //Here is where the event handling for the timer comes in.
            //I believe this is where the problem is.

             if(event.getSource() == clock){
        if(seconds > 0 && seconds < 60){
            seconds--;
        }else{
            if(seconds == 0 && minutes != 0){
                seconds = 59;
                minutes--;
            }else{
                if(minutes == 0){
                    clock.stop();
                    Toolkit.getDefaultToolkit().beep();
                }
            }
        }
        if(seconds < 10){
            clockLabel.setText(minutes + ":0" + seconds);
        }else{
        clockLabel.setText(minutes + ":" + seconds);
        }
    }

    if(event.getSource() == startClock){
        clock.start();
    }

    if(event.getSource() == stopClock){
        clock.stop();
    }


}

public static void main(String[] args){
    BasketballScoreboard first = new BasketballScoreboard();
    first.setTitle("Scoreboard");
    first.setSize(350, 300);
    first.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    first.setVisible(true);
}

}

1 个答案:

答案 0 :(得分:2)

下一行中的问题:

clock = new Timer(1000, this);
clock.addActionListener(this);

您添加ActionListener 2次,因为您的代码执行了2次。所以,删除clock.addActionListener(this);,一切都会有效。