Tienes un defecto de diseño muy habitual.
Los listeners de Swing deben ejecutarse a la mayor velocidad posible. En tu caso tienes un lazo dentro de uno de ellos, que además actualiza ese JTextField.
Lo que deberías hacer es trasladar ese contador a un thread independiente, con Swing Timer, o mejor todavía con un Executor Scheduler, y actualizar el JTextField desde ese thread, asegurándote de que el setText se ejecuta dentro del Event Dispatch Thread de Swing.
Ese listener del botón de inicio solamente debería poner en marcha el thread del temporizador, y salir.
Hola soy nuevo en esto de la programacion, estoy haciendo una aplicacion en la que le introduzco las horas, minutos y segundos, y hace una cuenta regresiva. Vale consigo mostrarlo por pantalla y en la barra de titulo de la ventana pero no consigo mostrarlo por un jTexfield. Debo de decir que estoy bastante perdio en esto de la programacion Aquí dejo el codigo:
import static java.lang.Thread.sleep;
import java.util.Scanner;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Apicacion extends javax.swing.JFrame {
public Apicacion() {
initComponents();
}
private static int nuMin;//El Contador de minutos
private static int nuSeg;//El Contador de de segundos
private static int nuHora;//El Contador de Horas
public int getNuMin() {
return nuMin;
}
public void setNuMin(int m) {
nuMin = m;
}
public int getNuSeg() {
return nuSeg;
}
public void setNuSeg(int s) {
nuSeg = s;
}
public int getNuHora() {
return nuHora;
}
public void setNuHora(int h) {
nuHora = h;
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
LabelTiempo = new javax.swing.JLabel();
LabelAreaTexto = new javax.swing.JLabel();
jScrollPane1 = new javax.swing.JScrollPane();
TextArea1 = new javax.swing.JTextArea();
TextHora = new javax.swing.JTextField();
TextMinuto = new javax.swing.JTextField();
TextSegundo = new javax.swing.JTextField();
BotonEmpezar = new javax.swing.JButton();
resultado = new javax.swing.JTextField();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel1.setBackground(new java.awt.Color(255, 255, 255));
jLabel1.setFont(new java.awt.Font("Vrinda", 1, 36)); // NOI18N
jLabel1.setForeground(new java.awt.Color(204, 0, 0));
jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel1.setText("NUEVA APLICACION");
LabelTiempo.setText("Tiempo");
LabelAreaTexto.setText("Texto");
TextArea1.setColumns(20);
TextArea1.setRows(5);
jScrollPane1.setViewportView(TextArea1);
TextHora.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
TextHoraActionPerformed(evt);
}
});
TextSegundo.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
TextSegundoActionPerformed(evt);
}
});
BotonEmpezar.setText("Empezar");
BotonEmpezar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
BotonEmpezarActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(198, 198, 198)
.addComponent(LabelTiempo))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(102, 102, 102)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(BotonEmpezar)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(TextHora, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(TextMinuto, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(TextSegundo, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGap(67, 67, 67)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(LabelAreaTexto))
.addGap(0, 228, Short.MAX_VALUE)))
.addContainerGap())
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(resultado, javax.swing.GroupLayout.PREFERRED_SIZE, 393, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jLabel1)
.addGap(33, 33, 33)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(LabelTiempo)
.addComponent(LabelAreaTexto))
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(26, 26, 26)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(TextHora, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(TextMinuto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(TextSegundo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(BotonEmpezar)))
.addGap(12, 12, 12)
.addComponent(resultado, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(133, Short.MAX_VALUE))
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
pack();
}// </editor-fold>
private void BotonEmpezarActionPerformed(java.awt.event.ActionEvent evt) {
int hora = Integer.parseInt(TextHora.getText());
int minuto = Integer.parseInt(TextMinuto.getText());
int segundo = Integer.parseInt(TextSegundo.getText());
int x=0, y=0, z=0;
String salida = "esfaagdfg";
resultado.setText(salida);
setNuHora(hora);
setNuMin(minuto);
setNuSeg(segundo);
try {//si ocurre un error al dormir el proceso(sleep(999))
for (;;) {//inicio del for infinito
if (getNuSeg() != 0) {//si no es el ultimo segundo
setNuSeg(getNuSeg() - 1); //decremento el numero de segundos
} else {
if (getNuMin() != 0) {//si no es el ultimo minuto
setNuSeg(59);//segundos comienzan en 59
setNuMin(getNuMin() - 1);//decremento el numero de minutos
} else {
if (getNuHora() != 0) {
setNuHora(getNuHora() - 1);//decremento el numero de horas
setNuMin(59);//minutos comienzan en 59
setNuSeg(59);//segundos comienzan en 59
} else {
break;//seacabo el tiempo fin hilo
}
}
}
x = getNuHora();
y = getNuMin();
z = getNuSeg();
System.out.println(x + " Horas " + y + " Minutos " + z + " Segundos");
salida = "dfgdfg" + x + " Horas " + y + " Minutos " + z + " Segundos";
resultado.setText(salida);
setTitle(getNuHora() + " Horas " + getNuMin() + " Minutos " + getNuSeg() + " Segundos");
sleep(1000);
}
} catch (InterruptedException ex) {
System.out.println(ex.getMessage());
}//Fin try
}
private void TextHoraActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void TextSegundoActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Apicacion().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton BotonEmpezar;
private javax.swing.JLabel LabelAreaTexto;
private javax.swing.JLabel LabelTiempo;
private javax.swing.JTextArea TextArea1;
private javax.swing.JTextField TextHora;
private javax.swing.JTextField TextMinuto;
private javax.swing.JTextField TextSegundo;
private javax.swing.JLabel jLabel1;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextField resultado;
// End of variables declaration
}