Java: Changing a label value while threading

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-23 05:19:07

问题


i want to create a little loop where after i press a button on a GUI, a value will change every second; i have tried using a thread but i can't seem to get it working correctly. What now happens is that the program just pauses for 10 seconds after pressing the button. Could you help me?

This is what my code looks like

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

for (x = 0; x <= 10; x++)
    {

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
            Thread.currentThread().interrupt(); here.
        }


        nummerlabel.setText(String.valueOf(x));
    }



}

回答1:


I think what you're looking for is the Timer class which comes with the swing package. Have a read of the documentation, it should help you solve your problem.




回答2:


A simple rule is to use an ExecutorService and run your code as runnable. This doesn't block the global flow of the program. Remember to shutdown the executor.

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

    ExecutorService executorService =  Executors.newFixedThreadPool(1);     
    for (int x = 0; x <= 10; x++) {     
        final int y=x;
        executorService.execute(new Runnable() {
            public void run() {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            //nummerLabel should be accessed via final reference                        
            nummerlabel.setText(String.valueOf(y));
            }
        });

    }
    executorService.shutdown();

}



回答3:


Calling Thread.sleep() in an action listener (or in any event handler) will cause your program to be unresponsive for the duration of the sleep() call.

GUI Frameworks like Java Swing typically have one Event Dispatch Thread (EDT) that handles all keyboard and mouse input. Event handlers are called in the EDT, and since there is only one EDT, they must be called one at a time. Your program can not respond to the next event until the handler for the current event returns.

Like Jimmy Jutt said in his answer, the correct way to make things happen later, or at periodic intervals in a GUI program is to use some kind of a timer (e.g., javax.swing.Timer). The timer can be used to schedule future timed events, for which you write handler functions in the same way that you write handlers for key and mouse events.




回答4:


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

    Thread thread = new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                for (int i = 0; i <= 10; i++) {
                    nummerlabel.setText(String.valueOf(i));

                    TimeUnit.SECONDS.sleep(1);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    });

    thread.start();
}


来源:https://stackoverflow.com/questions/33105688/java-changing-a-label-value-while-threading

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!