Primero les paso mi clase lógica, donde manejo el incremento del tiempo segun el estado del thread. Oh si, es un thread.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package cronometro; | |
public class Logica2 implements Runnable{ | |
Vista2 vis; | |
public void run(){ | |
Tiempo time=new Tiempo(); | |
while(true){ | |
int msec=time.getMilisec(); | |
int sec=time.getSegundo(); | |
int min=time.getMinuto(); | |
int hora=time.getHora(); | |
try{ | |
Thread.sleep(1000); //Lo duermo 1000 milisegundos, lo que equivale a 1 segundo | |
} | |
catch(InterruptedException e){ | |
} | |
// msec++; | |
// if(msec>=100){ | |
// sec++; | |
// msec=0; | |
// } | |
sec++; | |
if(sec>=60){ | |
min++; | |
sec=0; | |
} | |
if(min>=60){ | |
hora++; | |
min=0; | |
} | |
if(hora>=24){ | |
hora=0; | |
} | |
if(vis.getAction()) | |
time=new Tiempo(); | |
if(vis.getEstado()){ | |
time.setHora(hora); | |
time.setMilisec(msec); | |
time.setMinuto(min); | |
time.setSegundo(sec); | |
vis.Acomodar(time); //es el método de la vista que acomoda todo de manera visualmente agradable | |
} | |
} | |
} | |
public Logica2(Vista2 vista){ | |
vis=vista; | |
} | |
} |
Ahora esta es mi clase Tiempo, la cual contiene todos los datos de, pues, el tiempo.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package cronometro; | |
public class Tiempo { | |
private Integer hora; | |
private Integer minuto; | |
private Integer segundo; | |
private Integer milisec; | |
public Tiempo(){ | |
hora=0; | |
minuto=0; | |
segundo=0; | |
milisec=0; | |
} | |
public void setHora(int hora){ | |
this.hora=hora; | |
} | |
public void setMinuto(int minuto){ | |
this.minuto=minuto; | |
} | |
public void setSegundo(int segundo){ | |
this.segundo=segundo; | |
} | |
public void setMilisec(int milisec){ | |
this.milisec=milisec; | |
} | |
public Integer getHora(){ | |
return hora; | |
} | |
public Integer getMinuto(){ | |
return minuto; | |
} | |
public Integer getSegundo(){ | |
return segundo; | |
} | |
public Integer getMilisec(){ | |
return milisec; | |
} | |
} |
Ahora finalmente, mi clase Vista y la interfaz que implementa. El metodo que usa la vista para acomodar todo es el que recibe de la interfaz, y su unico parámetro es un objeto de Tiempo, que contiene todo lo que necesita para acomodar los números.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package cronometro; | |
import javax.swing.*; | |
import java.awt.*; | |
import java.awt.event.*; | |
public class Vista2 extends JFrame implements Interfaz2{ | |
JPanel p1, p2, p3; | |
JLabel hr, min, sec, msec, dot1, dot2; | |
JButton start, restart, stop, resume; | |
private boolean state=false; | |
private boolean action=false; | |
public Vista2(){ | |
super("Cronometro"); | |
this.setDefaultCloseOperation(EXIT_ON_CLOSE); | |
this.setBounds(50, 50, 480, 300); | |
p1=new JPanel(new GridLayout(1,5)); | |
p3=new JPanel(); | |
hr=new JLabel("00"); min=new JLabel("00"); sec=new JLabel("00"); msec=new JLabel("00"); | |
dot1=new JLabel(":"); dot2=new JLabel(":"); | |
hr.setFont(new Font("Arial", Font.BOLD, 70)); | |
min.setFont(new Font("Arial", Font.BOLD, 70)); | |
sec.setFont(new Font("Arial", Font.BOLD, 70)); | |
dot1.setFont(new Font("Arial", Font.BOLD, 70)); | |
dot1.setBounds(140,60,100,100); | |
dot2.setFont(new Font("Arial", Font.BOLD, 70)); | |
dot2.setBounds(280,60,100,100); | |
p1.add(hr); | |
add(dot1); | |
p1.add(min); | |
add(dot2); | |
p1.add(sec); | |
msec.setBounds(420, 85,100,100); | |
add(msec); | |
this.add(p1, BorderLayout.CENTER); | |
p3.setPreferredSize(new Dimension(44,44)); | |
this.add(p3, BorderLayout.WEST); | |
p2=new JPanel(new FlowLayout()); | |
start=new JButton("Iniciar"); start.setPreferredSize(new Dimension(100,20)); | |
restart=new JButton("Reiniciar"); restart.setPreferredSize(new Dimension(100, 20)); | |
stop=new JButton("Detener"); stop.setPreferredSize(new Dimension(100, 20)); | |
resume=new JButton("Continuar"); resume.setPreferredSize(new Dimension(100, 20)); | |
p2.add(start); p2.add(restart); | |
p2.add(stop); p2.add(resume); | |
this.add(p2, BorderLayout.SOUTH); | |
start.setEnabled(true); | |
stop.setEnabled(false); | |
restart.setEnabled(false); | |
resume.setEnabled(false); | |
this.setVisible(true); | |
start.addActionListener(new ActionListener(){ | |
public void actionPerformed(ActionEvent e){ | |
state=true; | |
start.setEnabled(false); | |
stop.setEnabled(true); | |
restart.setEnabled(false); | |
resume.setEnabled(false); | |
} | |
}); | |
stop.addActionListener(new ActionListener(){ | |
public void actionPerformed(ActionEvent e){ | |
state=false; | |
stop.setEnabled(false); | |
restart.setEnabled(true); | |
resume.setEnabled(true); | |
} | |
}); | |
restart.addActionListener(new ActionListener(){ | |
public void actionPerformed(ActionEvent e){ | |
hr.setText("00"); | |
min.setText("00"); | |
sec.setText("00"); | |
msec.setText("00"); | |
restart.setEnabled(false); | |
start.setEnabled(true); | |
resume.setEnabled(false); | |
action=true; | |
} | |
}); | |
resume.addActionListener(new ActionListener(){ | |
public void actionPerformed(ActionEvent e){ | |
state=true; | |
stop.setEnabled(true); | |
resume.setEnabled(false); | |
restart.setEnabled(false); | |
} | |
}); | |
} | |
public boolean getEstado(){ | |
return state; | |
} | |
public boolean getAction(){ | |
return action; | |
} | |
public void Acomodar(Tiempo time){ | |
//Estetica | |
if(time.getHora()<10) | |
hr.setText("0"+time.getHora().toString()); | |
else | |
hr.setText(time.getHora().toString()); | |
if(time.getMilisec()<10) | |
msec.setText("0"+time.getMilisec().toString()); | |
else | |
msec.setText(time.getMilisec().toString()); | |
if(time.getMinuto()<10) | |
min.setText("0"+time.getMinuto().toString()); | |
else | |
min.setText(time.getMinuto().toString()); | |
if(time.getSegundo()<10) | |
sec.setText("0"+time.getSegundo().toString()); | |
else | |
sec.setText(time.getSegundo().toString()); | |
action=false; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package cronometro; | |
public interface Interfaz2 { | |
public void Acomodar(Tiempo time); | |
} |
La clase main simplemente crea el objeto de Vista2 y arranca el thread. No hace nada más.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package cronometro; | |
public class Test { | |
public static void main(String[] args){ | |
//Vista vis=new Vista(); | |
//(new Thread(new Logica(vis))).start(); | |
Vista2 vis=new Vista2(); | |
(new Thread(new Logica2(vis))).start(); | |
} | |
} |
Y así declaro terminada mi tortura :)
Un glande, like si estas aquí por Macías.
ResponderEliminar