2014-03-06 44 views
0

我想打開一個帶有兩個jlabel的jpanel的JFrame,並使用循環方法和 重寫這些標籤。當然,在循環中,我有一個thread.sleep, 但是我無法弄清楚,我的線程開始運行1 .. 2 .. 3 ..當這個完成後,JFrame打開。Swing:寫入JLabel

這裏是我的代碼,到目前爲止,我還寫道:

FrmPruebaPlanillon vtnPruebaPlanillon = new FrmPruebaPlanillon(); 
    vtnPruebaPlanillon.setVisible(true); 

    boolean infinito = true; 
    while(infinito)//todo ver con cuidado 
    {  
     //enviamos los comando por fila para podrer rellenar los datos del 
     //taximetro con el boleto generado 
     System.out.println(FrmPrincipal.linea()+"Inició la prueba "+  (contadorDePrueba+1)); 
     //pp.getLblNEnvio().setText((contadorDePrueba+1)+""); 
     vtnPruebaPlanillon.getLblNEnvio().setText((contadorDePrueba+1)+""); 
     vtnPruebaPlanillon.getLblDatoEnviado().setText(fila[contadorDePrueba]); 
     //pp.getLblDatoEnviado().setText(fila[contadorDePrueba]); 



     //#######################################################################3 
     pruebaPorTabla(tipoPrueba, datosCsv); 
     //pruebaPorFila(tipoPrueba, fila[contadorDePrueba]); 
     //vtnFrmBoleto.setParametrosPrueba(tipoPrueba, tblPrueba, numeroPrueba, taximetro, empresa); 
     //pone un numero de prueba en la ventana boleto 
     if(contadorDePrueba == 0) 
     { 
      //vtnFrmBoleto.getLblNprueba().setText((String) tblPrueba.getModel().getValueAt(0, 0)); 
     }   
     //vtnFrmBoleto.setVisible(true); 
     contadorDePrueba++; 
     if(contadorDePrueba==dataTabla.length-1) 
     { 
      System.out.println("numero de lineas enviadas"+contadorDePrueba); 
      infinito=false; 
     } 

    }  
+0

*當然在循環中我有一個thread.sleep,但我無法弄清楚,我的線程開始運行1 .. 2 .. 3 ..當這個完成後,JFrame打開。看看這個問題:http://stackoverflow.com/questions/15600203/thread-sleep-inside-of-actionperformed-method –

+0

你正在睡在EDT(Event Dispatch Thread)線程? –

+0

@ PM77-1顯然。 :) –

回答

2

當然,在美國東部時間調用Thread.sleep();導致我在指出的CommNet這樣一個不可預知的行爲。改用適當的組件。在這種情況下,擺動Timer。這裏是你的一個小演示:

import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.SwingUtilities; 
import javax.swing.Timer; 

public class RepaintDemo { 

    JFrame frame = new JFrame("Repaint demo"); 
    JLabel labelFirst = new JLabel("First label"); 
    JLabel labelSecond = new JLabel("Second label"); 
    JLabel[] labels = { labelFirst, labelSecond }; 
    JPanel panel = new JPanel(); 
    Timer timer; 
    int i = 0; 

    public RepaintDemo() { 
     // Wait for 3 seconds and then add label 
     timer = new Timer(3000, new ActionListener() { 
      @Override 
      public void actionPerformed(ActionEvent e) { 
       panel.add(labels[i]); 
       panel.repaint(); 
       panel.revalidate(); 
       i++; 
       if (i == labels.length) { 
        timer.stop(); 
       } 
      } 
     }); 
     // Adds next label after every 3 seconds 
     timer.setDelay(3000); 
     timer.setRepeats(true); 
     timer.start(); 

     frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
     frame.add(panel); 
     frame.pack(); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       new RepaintDemo(); 
      } 
     }); 
    } 

} 

正如你所看到的,JFrame將在啓動時出現。 3秒後,會出現第一個標籤。再過3秒鐘,第二個標籤也會出現。

+0

感謝上帝的例子。 – Premier