我正在尝试将JTextfield(tenSecs
)的值增加一,以便我的其他JTextfield(oneSecs
)达到0的每个实例。我很难尝试增加我的值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;
行周围。我也相信,如果有人能够指出我正确的方向,我增加价值的方式并不是最好的方法
答案 0 :(得分:1)
如果我理解正确,请尝试tenSecs.setText(String.valueOf(Integer.parseInt(tenSecs.getText()) + 1));