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")));
...
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 de Angel Torres
1