【问题标题】:how to countdown using a button using netbeans如何使用netbeans使用按钮倒计时
【发布时间】:2023-03-14 22:07:01
【问题描述】:

我已经为学生创建了关于试卷的应用程序。在这个应用程序中有 JForm 和一个 10 sec 的计时器。在这个 JForm 中有 Jtextfield1 、 JLabel1 和 JButton。当学生按下按钮时,JLabel1 将显示从 10 、 9 到 0 的倒计时。然后 JTextField1 将无法编辑或输入任何数据。并且需要得到“timeup”的消息

以下是我尝试过的代码。但我没有得到我想的正确方法。

for(int i= 10; i>=0; i--)
{


    if(i==0)
    {
        jTextField1.enableInputMethods(false);
         JOptionPane.showMessageDialog(null, "time up");


    }
    else
    {
         jLabel3.setText(""+i);
    }
}

【问题讨论】:

    标签: java netbeans


    【解决方案1】:

    你可以使用Swing Timer来做。

    例如:

    private void startBtnActionPerformed(ActionEvent evt) {                                         
        startBtn.setEnabled(false);
        textField.setEditable(true);
        timeLbl.setText("10");
    
        Timer timer = new Timer(1000, new ActionListener() {
            int t = 9;
    
            @Override
            public void actionPerformed(ActionEvent e) {
                // each second this function will be called
    
                timeLbl.setText("" + t);
                if (t == 0) {
                    startBtn.setEnabled(true);
                    textField.setEditable(false);
                    timeLbl.setText("");
                    JOptionPane.showMessageDialog(Frame.this, "time up");
                    ((Timer) e.getSource()).stop();// Stop the timer
                }
                t--;
            }
        });
        timer.start();// Start the timer
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多