[英]Swing : Write in a JLabel
I want to open a JFrame that has a jpanel with two jlabel, with a method that loops and rewrites those labels. 我想打开一个JFrame,它有一个带有两个jlabel的jpanel,一个循环并重写这些标签的方法。 Of course in the loop I have a thread.sleep, BUT I can't figure it out, my thread begins to run 1.. 2.. 3.. and when this finished the JFrame opens. 当然在循环中我有一个thread.sleep, 但我无法搞清楚,我的线程开始运行1 .. 2 .. 3 ..当这个结束时,JFrame打开。
Here is my code I have wrote so far: 这是我到目前为止编写的代码:
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;
}
}
Of course, calling Thread.sleep();
当然,调用Thread.sleep();
on EDT causes such an unpredictable behaviour as I pointed in my commnet. 在EDT上导致如我在通信网中指出的那种不可预测的行为。 Use proper component instead. 请改用适当的组件。 In this case, swing Timer
. 在这种情况下,摆动Timer
。 Here is a small demo for you: 这是一个小型演示:
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();
}
});
}
}
As you can see, JFrame
will appear upon startup. 如您所见, JFrame
将在启动时出现。 After 3 seconds, first label will appear. 3秒后,将出现第一个标签。 And after another 3 seconds, second label will appear too. 再过3秒钟,第二个标签也会出现。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.