简体   繁体   English

停止摆动计时器

[英]Stopping a Swing Timer

I'm creating a little "Game" where you have to click buttons that flash a color and if you press it while the color is still on the button, the button stays that color. 我正在创建一个小的“游戏”,您必须单击闪烁颜色的按钮,如果在颜色仍在按钮上的状态下按该按钮,则按钮将保持该颜色。 So far I have the timers setting the colors on and off but I'm having trouble stopping the timer if the button is pressed. 到目前为止,我可以使用计时器来设置颜色的开和关,但是如果按下按钮,则无法停止计时器。 This is my code so far. 到目前为止,这是我的代码。

    //Changes To The Colors
    ActionListener timerListener = new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent evt) {

            jButton1.setBackground(Color.blue);

        }
    };

    int timerDelay = 1750;
    Timer timer = new Timer(timerDelay, timerListener);

    //Changes Colors Back To Default
    ActionListener defaultTime = new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent evt) {

            jButton1.setBackground(null);

        }
    };

    int waiter = 1000;
    Timer defaultState = new Timer(waiter, defaultTime);

    timer.start();
    timer.setRepeats(true);
    defaultState.start();
    defaultState.setRepeats(true);

And seen as I'm using Netbeans I added in the ActionPerformed option this is where I am getting the issues. 就像我正在使用Netbeans一样,我在ActionPerformed选项中添加了此功能,这就是解决问题的地方。 It's not letting me call the timer.stop(); 这不是让我打电话给timer.stop();。

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         

    if(jButton1.getBackground().equals(Color.blue)){
        jButton1.setBackground(Color.blue);
        timer.stop();
        defaultState.stop();

    }

} 

Right now I'm only using one button just to get the hang of the whole swing timer thing 现在我只用一个按钮就可以抓住整个摇摆计时器

When you are running this code jButton1 's color is blinking. 运行此代码时, jButton1的颜色闪烁。 Once blue and once default. 一次为蓝色,一次为默认。 jButton1ActionPerformed() in this method you are going to stop the timer if button's background color is blue( if(jButton1.getBackground().equals(Color.blue)) ). jButton1ActionPerformed()在此方法中,如果按钮的背景色为蓝色( if(jButton1.getBackground().equals(Color.blue)) ),您将停止计时器。 This is not true when the button is default(It's set default, because you are setting the color null ). 当按钮为默认按钮时(设置为默认,因为您将颜色设置为null ),情况并非如此。 It's blinking because timer is repeating. 它闪烁,因为计时器正在重复。 If the button is blue, you can stop the timer without any trouble. 如果按钮为蓝色,则可以毫无问题地停止计时器。

If the background color is null, if(jButton1.getBackground().equals(Color.blue)) condition is false. 如果背景色为null, if(jButton1.getBackground().equals(Color.blue))条件为false。 That's why your times isn't stop. 这就是为什么您的时代不会停止的原因。

Click the button when it's background is blue. 当背景为蓝色时,单击按钮。 Your timer will be stopped. 您的计时器将停止。

If you need to stop the timer with the button color, 如果您需要使用按钮颜色停止计时器,

ActionListener timerListener = new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent evt) {

        jButton1.setBackground(Color.blue);
        timer.stop();
        defaultState.stop();
    }
};

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM