繁体   English   中英

将JTextField中的整数递增1

[英]Increment an integer in a JTextField by one

我想增加一个JTextField(价值tenSecs一一为每个实例),我的其他的JTextField( oneSecs )达到0。我有困难想提高我的价值tenSecs ,因为它始终为0时手动更改了JTextField数字,一旦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;线附近int i = 0; 我也可以肯定,如果有人可以朝正确的方向指点我,那么增加价值的方法并不是最好的方法

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

暂无
暂无

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

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