SetText label java delay issue

I have a big problem with a java program. I am trying to implement a simple program. There is a label, and I want to print the text on this label, but it should be separate for 2 seconds.

I saw a problem with thread and Swing, and I implemented this code, but the result did not change. Could you help me 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);
    }
+3
source share
3 answers

, javax.swing.Timer.start() , 2 ( ). , ( ) . , ActionListener . , , :

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

, .

:

, , ( ) .

:

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

: tmr2 = new ... (4000, taskPerformer2);
: tmr3 = new ... (6000, taskPerformer3);

, stampChips1(int) , , .


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:..."

, , Timer.stop() , Timer.setRepeats(false).

+2

, , (2000, 4000, 6000), .

0
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 ran into a similar problem. You can call this function in an infinite loop if you want this to happen forever ...

0
source

Source: https://habr.com/ru/post/1775241/


All Articles