Programa

He ido avanzando con el codigo, como podras ver:
Mi intencion es poner una ventanita que se cierre a los 3 segundos al pulsar el boton start.
Es decir, cuando pulso start en la pantalla, ¿aparecera un recuadro que ponga?:
Preparado 3 y vaya bajando hasta 0, y entonces esa ventana se cierre y empieze a funcionar el contador, ¿me podrias ayudar?
Gracias de nuevo
import java.awt.event.KeyEvent;
import javax.swing.JOptionPane;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* jframe.java
*
* Created on 11/04/2011, 09:32:56 AM
*/
/**
*
* @author J6
*/
public class jframe extends javax.swing.JFrame {
/** Creates new form jframe */
public jframe() {
initComponents();
jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/stop.gif")));
btnStop.addKeyListener(new java.awt.event.KeyAdapter() {
@Override
public void keyPressed(java.awt.event.KeyEvent evt) {
btnStopKeyPressed(evt);
}
});
}
int hora = 0, min = 0, seg = 000;
Thread hilo = new Thread() {
@Override
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();
jLabel1 = new javax.swing.JLabel();
jMenuBar1 = new javax.swing.JMenuBar();
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);
}
});
btnStop.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(java.awt.event.KeyEvent evt) {
btnStopKeyPressed(evt);
}
});
lblStatus.setText("Estado: ");
jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Reloj.gif"))); // NOI18N
setJMenuBar(jMenuBar1);
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)
.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)
.addGap(6, 6, 6)
.AddComponent(jLabel1))
.AddComponent(lblStatus, javax. Swing. GroupLayout.PREFERRED_SIZE, 141, javax. Swing. GroupLayout.PREFERRED_SIZE))
.addContainerGap(61, 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, 92, Short.MAX_VALUE)
.AddComponent(lblStatus))
.AddGroup(layout.createSequentialGroup()
.addGap(41, 41, 41)
.AddGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.AddComponent(jLabel1)
.AddComponent(lblTime, javax. Swing. GroupLayout.PREFERRED_SIZE, 42, javax. Swing. GroupLayout.PREFERRED_SIZE))
.addContainerGap(118, 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
btnStart.setVisible(false);
btnStop.setVisible(true);
jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Reloj.gif")));
if (!issuspended) {
hilo = new Thread() {
public void run() {
try {
int hora = 0, min = 0, seg = 000;
while (true) {
if (seg == 59) {
seg = 0;
min++;
}
if (min == 9) {
min = 0;
hora++;
}
if (hora == 1) {
btnStart.setVisible(true);
btnStop.setVisible(true);
JOptionPane.showMessageDialog(null, "Fallaste", "Error",
JOptionPane.ERROR_MESSAGE);
jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/stop.gif")));
...

1 respuesta

Respuesta
1
Ok mira donde declaras el hilo ponle esto
////////////////////////////////////////////////////////////////////////
hilo = new Thread() {
public void run() {
try {
int hora = 0, min = 0, seg = 000;
while (true) {
if (seg == 59) {
seg = 0;
min++;
}
if (min == 9) {
min = 0;
hora++;
}
seg++;
lblTime.setText(hora + ":" + min + ":" + seg);
if (hora==3)
{
lblTime.setVisible(false);
}
hilo.sleep(1);
}
} catch (java.lang.InterruptedException ie) {
System.out.println(ie.getMessage());
}
}
};
////////////////////////////////////////////////////////////////////
esto lo q hace es q alos 3 segundos oculta la etiqueta pero tu cambia en donde dice
lblTime.setVisible(false);
por
new jframeOTRO().setVisible(true);
Y asi alos 3 segumdos te abre otro
lo de mas te toca a ti :)
¿Te sirvio amigo? No olvides cerrar tu pregunta
Esta noche lo compruebo,que ayer tuve un dia de mucho jaleo.
Gracias de nuevo
Okas yas estas :)
La primera parte me la realiza bien,en la segunda me da fallo,cual es lo que deberia de hacer?
Gracias
Como no te entendi el codigo q te pase cambialo donde delcaras el hilo y lo q hace es q oculta la etiqueta pero ahi cambiaselo por el de abrir el nuevo form q quieres
No,si es lo que hago,pero aun asi me da fallo,no encuentro la solucion a ello
package mispruebas;
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();
jLabel1 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Semaforo");
jLabel1.setIcon(new javax.swing.ImageIcon("Reloj.gif"));
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(jLabel1, 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(jLabel1)).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
btnStart.setVisible(false);
btnStop.setVisible(true);
if (!issuspended) {
hilo = new Thread() {
public void run() {
try {
int hora = 0, min = 0, seg = 000;
while (true) {
if (seg == 59) {
seg = 0;
min++;
}
if (min == 9) {
min = 0;
hora++;
}
seg++;
lblTime.setText(hora + ":" + min + ":" + seg);
if (hora==3)
{
new NombredeTuOtraForma().setVisible(true);//.............................................................................ojo aqui
}
hilo.sleep(1);
}
} catch (java.lang.InterruptedException ie) {
System.out.println(ie.getMessage());
}
}
};
hilo.start();
lblStatus.setText("Estado: GO!!!");
} else {
hilo.destroy();
issuspended = false;
lblStatus.setText("Estado: Resumido");
}
}//GEN-LAST:event_btnStartMouseClicked
private void btnStopMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnStopMouseClicked
btnStart.setVisible(true);
btnStop.setVisible(false);
hilo.stop();
lblStatus.setText("STOP");
issuspended = false;
}//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;
private javax.swing.JLabel jLabel1;
// End of variables declaration//GEN-END:variables
}
Hola!!!
He probado lo que me has pasado,y no me sale la ventanita al pulsar el boton start,y que se cierre a los 3 segundos.
Eso si,errores de compilacion no me da ninguno,asi que haber si me puedes echar una "manita".
Eso si,elimine la linea esa de "ojo aqui",para que no me diera error de compilacion
Esq ahi donde puse ojo aquie ahi tienes q poner tu lo q quieras q haga a los 3 segundo de abrir como s ellama tu otro formulario o q rollo

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas