开发者

Problem with delay setText label java

I have a big problem with a java program. I try to implement a simple software. There is a label and i wanna stamp a text in this label but this must be separate by 2 sec.

I have seen a problem with the thread and Swing and I implemented this code but the result not change. Can you help me please Thanks

 public void stampChips1(int numberDecrement){

        ActionListener taskPerformer = new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                jLabel1.setText("L'avversario ha deciso di togliere : . ");
            }
        };
        new javax.swing.Timer(2000, taskPerformer).start();


        ActionListener taskPerformer2 = new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                jLabel1.setText("L'avversario ha deciso di togliere : . . ");
            }
        };
        new javax.swing.Timer(2000, taskPerformer2).start();


        ActionListener taskPerformer3 = new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                jLabel1.setText("L'avversario ha deciso di togliere : . . .");
            }
        };
        new 开发者_开发问答javax.swing.Timer(2000, taskPerformer3).start();



        jLabel1.setText("L'avversario ha deciso di togliere : " + numberDecrement);
    }


Every time you call javax.swing.Timer.start() in your code, it will continue to fire events every 2 seconds (as set in your constructors). After a few intervals, you eventually have three different timers trying (and succeeding) to set the text in the same label. The text of the label will depend on which Timer's ActionListener has last finished executing. If you want a Timer to fire once and stop you should set it up like this:

javax.swing.Timer tmr = new javax.swing.Timer(2000, taskPerformer);
tmr.setRepeats(false);
tmr.start();

Now each timer will fire once only, eliminating any overwriting issues on the label.

Added per request in comments:

When you set up each timer, set it up to only fire once (and as camickr stated), set each timer with a different interval.

The first timer should be:

javax.swing.Timer tmr1 = new javax.swing.Timer(2000, taskPerformer);
tmr1.setRepeats(false);
tmr1.start();

The second timer should be similar: tmr2 = new ... (4000, taskPerformer2);
The third timer should be similar: tmr3 = new ... (6000, taskPerformer3);

The problem with your initial code is that when your stampChips1(int) method executes it first sets up three Timers to fire events EVERY two seconds, and then writes to the label.

time
0s - jLabel1 = "L'avversario ha deciso di togliere : " + numberDecrement
~2s - jLabel1 = "L'avversario ha deciso di togliere : . "
~2s - jlabel1 = "L'avversario ha deciso di togliere : . . "
~2s - jlabel1 = "L'avversario ha deciso di togliere : . . ."
~4s - jLabel1 = "L'avversario ha deciso di togliere : . "
~4s - jlabel1 = "L'avversario ha deciso di togliere : . . "
~4s - jlabel1 = "L'avversario ha deciso di togliere : . . ."

All of your timers are firing close to the same time, and they won't stop until you call Timer.stop() or tell it to fire only once with Timer.setRepeats(false).


Also using your basic code you would need to have different intervals (2000, 4000, 6000) otherwise all the Timers will fire at the same time.


void uiFunction(JLabel label) {
new Thread() {
    public void run() {

            final jLabel1=label;
            SwingUtilities.invokeLater(new Runnable() {
                jLabel1.setText("L'avversario ha deciso di togliere : . ");
                jLabel1.repaint();
            }
            Thread.sleep(2000);
             SwingUtilities.invokeLater(new Runnable() {
                jLabel1.setText("L'avversario ha deciso di togliere : .. ");
                jLabel1.repaint();
            }
            Thread.sleep(2000);

          SwingUtilities.invokeLater(new Runnable() {
                jLabel1.setText("L'avversario ha deciso di togliere : ... ");
                jLabel1.repaint();
            }

    }
}.run();

}

I was facing similar kind of problem. You can call this function in infinite loop if you want this to happen for ever...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜