我试图将我的其他JTextfield(tenSecs)达到0的每个实例的JTextfield(oneSecs)的值增加一。我很难尝试照原样增加tenSecs JTextfield的值始终为0。手动更改数字时,一旦oneSecs达到0,tenSecs就会还原为0。

javax.swing.Timer tm = new javax.swing.Timer(1000, new ActionListener() {
    public void actionPerformed(ActionEvent evt) {
        AddOneActionPerformed(evt);
    }
});

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

    if (!tm.isRunning()) {
        tm.start();
    } else {
        tm.stop();
    }

    ScheduledExecutorService e= Executors.newSingleThreadScheduledExecutor(); //Start new scheduled executor service to invoke a timer that start wehn button is pressed
    e.scheduleAtFixedRate(new Runnable() {

        @Override
        public void run() {
            //Follow this Override to do task
            SwingUtilities.invokeLater(new Runnable() {
                //Override will let the task run

                @Override
                public void run() {
                    oneSecsDisplay.setIcon(new ImageIcon("images\\" + TextGrabber() + ".png"));
                    oneSecs.setText( DigitValue.getText());

                    int i = 0;

                    if (Integer.parseInt(oneSecs.getText()) == i) {
                        tenSecs.setText(Integer.toString(i++));
                    }
                }
            });
        }
    }, 0, 100, TimeUnit.MILLISECONDS); //Update will be for every 100 milliseconds concurrent to system time
}

// Variables declaration - do not modify
private javax.swing.JTextField oneSecs;
private javax.swing.JTextField tenSecs;
// End of variables declaration


我确定错误发生在int i = 0;行附近。我也可以肯定,如果有人可以朝正确的方向指点我,那么增加价值的方法并不是最好的方法

最佳答案

如果我对您的理解正确,请尝试tenSecs.setText(String.valueOf(Integer.parseInt(tenSecs.getText()) + 1));

09-12 10:03