亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

定時器啟動后可切換按鈕不起作用

定時器啟動后可切換按鈕不起作用

阿晨1998 2023-02-16 17:21:20
我正在嘗試制作一個可切換的按鈕來啟動和停止倒數計時器。它會啟動計時器但不會停止。我有點像 java 和 swing 之類的初學者,有人能幫忙嗎?這是一些代碼:private static Timer timer;timer = new Timer(1000, new ActionListener() {    @Override    public void actionPerformed(ActionEvent e) {        l1.setText(Integer.toString(timeLeft / 60) + ":" + Integer.toString(timeLeft % 60));        timeLeft--;        if (timeLeft == 0) {            boolean rest = false;            if (rest) {                timeLeft = workTime;                JOptionPane.showMessageDialog(null, "Times Up!");                rest = false;            } else {                timeLeft = restTime;                JOptionPane.showMessageDialog(null, "Times Up!");                rest = true;            }        }    }});b1.addActionListener(new ActionListener() {    public void actionPerformed(ActionEvent evt) {        boolean start = true;        if (start == true){            timer.start();            b1.setText("stop");            start = false;        } else if (start == false){            timer.stop(); //the part that doesn't work            b1.setText("start");            start = true;        }    }});這不是確切的代碼,而是重要的部分
查看完整描述

1 回答

?
弒天下

TA貢獻1818條經驗 獲得超8個贊

因為在 b1.addActionListener 上你設置了 boolean start = true; 這意味著每當你回到你的 actionPerformed 監聽器時,start = true你總是需要在 actionPerformed 之外聲明和初始化。


b1.addActionListener(new ActionListener() {

    public void actionPerformed(ActionEvent evt) {

        boolean start = true;

        if (start == true){

            timer.start();

            b1.setText("stop");

            start = false;

        } else if (start == false){

            timer.stop(); //the part that doesn't work

            b1.setText("start");

            start = true;

        }

    }

});

建議:您還可以聲明您的boolean start;類后并在默認構造函數中初始化start=true;,您可以在代碼中使用 start 變量。


或者簡單的 例子


private static Timer timer;

boolean start = true;

boolean rest = false;

timer = new Timer(1000, new ActionListener() {

    @Override

    public void actionPerformed(ActionEvent e) {

        l1.setText(Integer.toString(timeLeft / 60) + ":" + Integer.toString(timeLeft % 60));

        timeLeft--;

        if (timeLeft == 0) {

            if (rest) {

                timeLeft = workTime;

                JOptionPane.showMessageDialog(null, "Times Up!");

                rest = false;

            } else {

                timeLeft = restTime;

                JOptionPane.showMessageDialog(null, "Times Up!");

                rest = true;

            }

        }

    }

});

b1.addActionListener(new ActionListener() {

    public void actionPerformed(ActionEvent evt) {

        if (start == true){

            timer.start();

            b1.setText("stop");

            start = false;

        } else if (start == false){

            timer.stop(); //the part that doesn't work

            b1.setText("start");

            start = true;

        }

    }

});


查看完整回答
反對 回復 2023-02-16
  • 1 回答
  • 0 關注
  • 136 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號