package cronometro;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;
public class Cronometro extends JFrame implements Runnable, ActionListener{
//atributos ventana
private JPanel panelSur;
private JPanel panelNorte;
private JLabel ventanaHora;
private JButton arrancarParar;
private JButton reiniciar;
//atributos reloj
private boolean arrancar= false;
private int hora;
private int minutos;
private int segundos;
private StringBuffer tmp;
public Cronometro(){
//propiedades de la ventana
final Cronometro c = this;
this.setTitle("Cronometro");
this.setBounds(10, 10, 310, 200);
this.setVisible(true);
this.setLayout(null);
//paneles
panelNorte = new JPanel();
panelSur = new JPanel();
//propiedades Paneles
panelNorte.setBounds(0, 0, 310, 100);
panelSur.setBounds(0, 100, 310, 100);
panelSur.setLayout(new FlowLayout());
this.add(panelNorte);
this.add(panelSur);
//label
ventanaHora = new JLabel("aca va la hora");
Font letra=ventanaHora.getFont();
ventanaHora.setFont(new Font(letra.getFontName(),letra.getStyle(),50));
//propiedades label
ventanaHora.setSize(200, 80);
ventanaHora.setBorder(new LineBorder(Color.BLUE));
panelNorte.add(ventanaHora);
//botones
arrancarParar = new JButton("Arrancar/Parar");
reiniciar= new JButton("Reiniciar");
//propiedades Botones
arrancarParar.setSize(100, 50);
arrancarParar.setMnemonic('A');
arrancarParar.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(arrancar==true){
arrancar=false;
}
else{
arrancar=true;
new Thread(c).start();
}
System.out.println("se presiono el boton arrancarParar");
}
});
reiniciar.setSize(100, 50);
reiniciar.setMnemonic('R');
reiniciar.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
arrancar=false;
hora= 0;
minutos= 0;
segundos= 0;
System.out.println("se presiono el boton reiniciar");
}
});
panelSur.add(arrancarParar);
panelSur.add(reiniciar);
//formatear la hora
this.mostrarHora();
//arrancar el reloj
}
public void run(){
while(arrancar==true){
try{
//paramos 1 segundo
Thread.sleep(1000);
}catch(Exception e){
e.printStackTrace();
}
//incrementar 1 segundo
segundos++;
if(segundos==60){
segundos=0;
minutos++;
if(minutos==60)
{
minutos=0;
hora++;
if(hora==24)
hora=0;
}
}
this.mostrarHora();
}
}
private void mostrarHora() {
//formatear hora
tmp= new StringBuffer();
if(hora<10)
tmp.append(0);
tmp.append(hora);
tmp.append(":");
if(minutos<10)
tmp.append(0);
tmp.append(minutos);
tmp.append(":");
if(segundos<10)
tmp.append(0);
tmp.append(segundos);
ventanaHora.setText(tmp.toString());
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
}
-----------------------------------------------------------------------------------------------------------
package cronometro;
public class TestCronometro {
public static void main (String args[]){
Cronometro cronometro = new Cronometro();
}
}
No hay comentarios:
Publicar un comentario