Pessoal o problema é o seguinte, não estou conseguindo fazer com que o tempo apareça rodando no textfield, ele só atualiza o tempo a cada clique no botão e não cada segundo. Dentro da classe cronometro o tempo corre normalmente.
Teste esse código, usei um Thread e setei o tempo em um JLabel, mas funciona igualmente com JTextField
packagejavaapplication55;importjava.util.logging.Level;importjava.util.logging.Logger;publicclasstesteextendsjavax.swing.JFrame{intcontador=0;publicteste(){initComponents();newThread(){publicvoidrun(){while(true){try{sleep(1000);contador++;intseg=contador%60;intmin=contador/60;inthora=min/60;tempo.setText(String.format("%02d:%02d:%02d",hora,min,seg));}catch(InterruptedExceptionex){Logger.getLogger(teste.class.getName()).log(Level.SEVERE,null,ex);}}}}.start();}@SuppressWarnings("unchecked")// <editor-fold defaultstate="collapsed" desc="Generated Code"> privatevoidinitComponents(){tempo=newjavax.swing.JLabel();setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);javax.swing.GroupLayoutlayout=newjavax.swing.GroupLayout(getContentPane());getContentPane().setLayout(layout);layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(javax.swing.GroupLayout.Alignment.TRAILING,layout.createSequentialGroup().addContainerGap(121,Short.MAX_VALUE).addComponent(tempo,javax.swing.GroupLayout.PREFERRED_SIZE,168,javax.swing.GroupLayout.PREFERRED_SIZE).addGap(111,111,111)));layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(javax.swing.GroupLayout.Alignment.TRAILING,layout.createSequentialGroup().addContainerGap(127,Short.MAX_VALUE).addComponent(tempo,javax.swing.GroupLayout.PREFERRED_SIZE,57,javax.swing.GroupLayout.PREFERRED_SIZE).addGap(116,116,116)));pack();}// </editor-fold> /** * @param args the command line arguments */publicstaticvoidmain(Stringargs[]){/* Set the Nimbus look and feel *///<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */try{for(javax.swing.UIManager.LookAndFeelInfoinfo:javax.swing.UIManager.getInstalledLookAndFeels()){if("Nimbus".equals(info.getName())){javax.swing.UIManager.setLookAndFeel(info.getClassName());break;}}}catch(ClassNotFoundExceptionex){java.util.logging.Logger.getLogger(teste.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);}catch(InstantiationExceptionex){java.util.logging.Logger.getLogger(teste.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);}catch(IllegalAccessExceptionex){java.util.logging.Logger.getLogger(teste.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);}catch(javax.swing.UnsupportedLookAndFeelExceptionex){java.util.logging.Logger.getLogger(teste.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);}//</editor-fold>/* Create and display the form */java.awt.EventQueue.invokeLater(newRunnable(){publicvoidrun(){newteste().setVisible(true);}});}// Variables declaration - do not modify privatejavax.swing.JLabeltempo;// End of variables declaration }
Renan_Nogueira
Consegui solucionar com a thread e o while atualizando o valor do textfield, muito obrigado!