Ayuda java
Hola,me gustaria pedirte ayuda con un problema que tengo de java.Este codigo es de un programa que estoy desarrollando de un cronometro,y hay gente,que me ha dicho que podria optimizarlo si cambio el while por una clase de java,llamada timer,para que me haga mas "real" el paso del tiempo,que esta puesto en centesimas,sabrias ayudarme para implementarlo??
Gracias
public class jframe extends javax.swing.JFrame {
/** Creates new form jframe */
public jframe() {
initComponents();
}
int hora = 0, min =0, seg =000;
Thread hilo = new Thread()
{
public void run()
{
try
{
while(true)
{
if(seg==59) { seg=0; min++; }
if(min==9) { min=0; hora++; }
seg++;
lblTime.setText(hora+":"+min+":"+seg);
hilo.sleep(1);
}
} catch (java.lang.InterruptedException ie) { System.out.println(ie.getMessage()); }
}
};
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
lblTime = new javax.swing.JLabel();
btnStart = new javax.swing.JButton();
btnStop = new javax.swing.JButton();
lblStatus = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Semaforo");
lblTime.setText("Time");
btnStart.setText("Start");
btnStart.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
btnStartMouseClicked(evt);
}
});
btnStop.setText("Stop");
btnStop.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
btnStopMouseClicked(evt);
}
});
lblStatus.setText("Estado: ");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(lblStatus, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createSequentialGroup()
.addGap(38, 38, 38)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(btnStop)
.addComponent(btnStart))
.addGap(46, 46, 46)
.addComponent(lblTime, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(47, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(39, 39, 39)
.addComponent(btnStart)
.addGap(18, 18, 18)
.addComponent(btnStop)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 65, Short.MAX_VALUE)
.addComponent(lblStatus))
.addGroup(layout.createSequentialGroup()
.addGap(41, 41, 41)
.addComponent(lblTime, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(90, Short.MAX_VALUE))
);
pack();
}// </editor-fold>//GEN-END:initComponents
public boolean issuspended = false;
private void btnStartMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnStartMouseClicked
if(!issuspended)
{
hilo.start();
lblStatus.setText("Estado: GO!!!");
}
else
{
hilo.resume();
issuspended = false;
lblStatus.setText("Estado: Resumido");
}
}//GEN-LAST:event_btnStartMouseClicked
private void btnStopMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnStopMouseClicked
hilo.stop();
lblStatus.setText("STOP");
}//GEN-LAST:event_btnStopMouseClicked
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new jframe().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton btnStart;
private javax.swing.JButton btnStop;
private javax.swing.JLabel lblStatus;
private javax.swing.JLabel lblTime;
// End of variables declaration//GEN-END:variables
}
Gracias
public class jframe extends javax.swing.JFrame {
/** Creates new form jframe */
public jframe() {
initComponents();
}
int hora = 0, min =0, seg =000;
Thread hilo = new Thread()
{
public void run()
{
try
{
while(true)
{
if(seg==59) { seg=0; min++; }
if(min==9) { min=0; hora++; }
seg++;
lblTime.setText(hora+":"+min+":"+seg);
hilo.sleep(1);
}
} catch (java.lang.InterruptedException ie) { System.out.println(ie.getMessage()); }
}
};
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
lblTime = new javax.swing.JLabel();
btnStart = new javax.swing.JButton();
btnStop = new javax.swing.JButton();
lblStatus = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Semaforo");
lblTime.setText("Time");
btnStart.setText("Start");
btnStart.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
btnStartMouseClicked(evt);
}
});
btnStop.setText("Stop");
btnStop.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
btnStopMouseClicked(evt);
}
});
lblStatus.setText("Estado: ");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(lblStatus, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createSequentialGroup()
.addGap(38, 38, 38)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(btnStop)
.addComponent(btnStart))
.addGap(46, 46, 46)
.addComponent(lblTime, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(47, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(39, 39, 39)
.addComponent(btnStart)
.addGap(18, 18, 18)
.addComponent(btnStop)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 65, Short.MAX_VALUE)
.addComponent(lblStatus))
.addGroup(layout.createSequentialGroup()
.addGap(41, 41, 41)
.addComponent(lblTime, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(90, Short.MAX_VALUE))
);
pack();
}// </editor-fold>//GEN-END:initComponents
public boolean issuspended = false;
private void btnStartMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnStartMouseClicked
if(!issuspended)
{
hilo.start();
lblStatus.setText("Estado: GO!!!");
}
else
{
hilo.resume();
issuspended = false;
lblStatus.setText("Estado: Resumido");
}
}//GEN-LAST:event_btnStartMouseClicked
private void btnStopMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnStopMouseClicked
hilo.stop();
lblStatus.setText("STOP");
}//GEN-LAST:event_btnStopMouseClicked
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new jframe().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton btnStart;
private javax.swing.JButton btnStop;
private javax.swing.JLabel lblStatus;
private javax.swing.JLabel lblTime;
// End of variables declaration//GEN-END:variables
}
Respuesta de roberto mota
1