Como eu trabalho com o timer em java ?
Como faço um relogio e apresento num JLabel?
Como temermino seu intervalo e o utilizo ?
Se alguem puder me ajudar , desde já muto obrigado !!!
Como eu trabalho com o timer em java ?
Como faço um relogio e apresento num JLabel?
Como temermino seu intervalo e o utilizo ?
Se alguem puder me ajudar , desde já muto obrigado !!!
Um exemplo bem simples
package timer;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
/**
*
* @author fabiofalci
*/
public class TimerTest extends JPanel {
JLabel label;
static int number = 0;
public TimerTest() {
this.initialize();
}
protected void initialize() {
this.setLayout(null);
this.add(this.getButton());
this.go();
}
public JLabel getButton() {
if (this.label == null) {
this.label = new JLabel("");
this.label.setBounds(10, 10, 100, 22);
}
return this.label;
}
public void go() {
ActionListener action = new ActionListener() {
public void actionPerformed(@SuppressWarnings("unused") java.awt.event.ActionEvent e) {
TimerTest.this.label.setText((number++) + "");
}
};
Timer t = new Timer(1000, action);
t.start();
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(new TimerTest());
frame.setSize(300, 200);
frame.setVisible(true);
}
}
Ola !
Vlw pela ajuda , mais eu fui testar esse codigo
e esse trcho @SuppressWarnings num compila …
Q mais eu tenho q fazer ?
[quote=touresfinge]Ola !
Vlw pela ajuda , mais eu fui testar esse codigo
e esse trcho @SuppressWarnings num compila …
Q mais eu tenho q fazer ?[/quote]
Você tá usando qual versão do Java? Annotations só funcionam a partir da versão 5.
Se for o caso tira essa annotation
Tou usando 1.6 …
Eu tirei e funcionou blza … vlw
Só mais uma coisa …
Como eu pego a hora do sistema ?
Num tou conseguindo …
Num seria Timer agora = new Timer() ; naum ?
Brigado…
Date dataAtual = new Date();
// ou em milesegundos
System.currentTimeMillis();
Fiz ai com a hora
package timer;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
/**
*
* @author fabiofalci
*/
public class TimerTest extends JPanel {
JLabel label;
DateFormat dateFormat;
public TimerTest() {
this.dateFormat = new SimpleDateFormat("HH:mm:ss");
this.initialize();
}
protected void initialize() {
this.setLayout(null);
this.add(this.getButton());
this.go();
}
public JLabel getButton() {
if (this.label == null) {
this.label = new JLabel(getTime());
this.label.setBounds(10, 10, 100, 22);
}
return this.label;
}
public void go() {
ActionListener action = new ActionListener() {
public void actionPerformed(ActionEvent e) {
TimerTest.this.label.setText(getTime());
}
};
Timer t = new Timer(1000, action);
t.start();
}
public String getTime() {
return this.dateFormat.format(Calendar.getInstance().getTime());
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(new TimerTest());
frame.setSize(300, 200);
frame.setVisible(true);
}
}
Preciso usar um timer aqui, mas esse primeiro exemplo quando chega no 60 eu precisaria que formate-se 1:01 … assim por diante para hora 1:59:59, a questão é como adptar esse método?
/**
*
*/
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
/**
*
* @author fabiofalci
*/
public class TimerTest extends JPanel {
JLabel label;
static int number = 0;
public TimerTest() {
this.initialize();
}
protected void initialize() {
this.setLayout(null);
this.add(this.getButton());
this.go();
}
public JLabel getButton() {
if (this.label == null) {
this.label = new JLabel("");
this.label.setBounds(10, 10, 100, 22);
}
return this.label;
}
public void go() {
ActionListener action = new ActionListener() {
public void actionPerformed(@SuppressWarnings("unused") java.awt.event.ActionEvent e) {
TimerTest.this.label.setText((number++) + "");
}
};
Timer t = new Timer(1000, action);
t.start();
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(new TimerTest());
frame.setSize(300, 200);
frame.setVisible(true);
}
}
ai… soh usar Calendar…
package timer;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
/**
*
* @author fabiofalci
*/
public class TimerTest extends JPanel {
JLabel label;
DateFormat dateFormat;
Calendar calendar;
public TimerTest() {
this.dateFormat = new SimpleDateFormat("HH:mm:ss");
this.calendar = Calendar.getInstance();
this.calendar.set(Calendar.MILLISECOND, 0);
this.calendar.set(Calendar.SECOND, 0);
this.calendar.set(Calendar.MINUTE, 0);
this.calendar.set(Calendar.HOUR_OF_DAY, 0);
this.initialize();
}
protected void initialize() {
this.setLayout(null);
this.add(this.getButton());
this.go();
}
public JLabel getButton() {
if (this.label == null) {
this.label = new JLabel(getTime());
this.label.setBounds(10, 10, 100, 22);
}
return this.label;
}
public void go() {
ActionListener action = new ActionListener() {
public void actionPerformed(ActionEvent e) {
TimerTest.this.label.setText(getTime());
}
};
Timer t = new Timer(1000, action);
t.start();
}
public String getTime() {
this.calendar.add(Calendar.SECOND, 1);
return this.dateFormat.format(this.calendar.getTime());
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(new TimerTest());
frame.setSize(300, 200);
frame.setVisible(true);
}
}
Para eu parar este Timer qual método devo usar?
Eh soh chamar
t.stop();
Valeu cara, mas estou usando assim e não está parando o Timer:
método
public void startTimer(String evento) {
ActionListener action = new ActionListener() {
public void actionPerformed(@SuppressWarnings("unused") java.awt.event.ActionEvent e) {
lblDuracao.setText("Duração: " + getTime());
}
};
Timer t = new Timer(1000, action);
if(evento.equals("start")){
t.start();
}else{
System.out.println("stop timer");
t.stop();
}
}
botão stop
private void btnDesligarActionPerformed(java.awt.event.ActionEvent evt) {
System.out.println("desligar");
startTimer("stop");
}
Por que não está parando?
Como tu ta fazendo tu ta criando um Timer cada vez q aperta o botao de desligar.
Tem que separar a criacao do desligamento…
Coloquei 2 botoes no painel… um start e outro stop… da uma olhada agora
package timer;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
/**
*
* @author fabiofalci
*/
public class TimerTest extends JPanel {
JLabel label;
JButton startButton;
JButton stopButton;
DateFormat dateFormat;
Calendar calendar;
Timer timer;
public TimerTest() {
this.dateFormat = new SimpleDateFormat("HH:mm:ss");
this.calendar = Calendar.getInstance();
this.calendar.set(Calendar.MILLISECOND, 0);
this.calendar.set(Calendar.SECOND, 0);
this.calendar.set(Calendar.MINUTE, 0);
this.calendar.set(Calendar.HOUR_OF_DAY, 0);
this.initialize();
}
protected void initialize() {
this.add(this.getLabel());
this.add(this.getStartButton());
this.add(this.getStopButton());
this.go();
}
public JLabel getLabel() {
if (this.label == null) {
this.label = new JLabel(getTime());
this.label.setPreferredSize(new Dimension(100, 22));
}
return this.label;
}
public JButton getStartButton() {
if (this.startButton == null) {
this.startButton = new JButton("Start");
this.startButton.setPreferredSize(new Dimension(75, 22));
this.startButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (!timer.isRunning()) {
timer.start();
}
}
});
}
return this.startButton;
}
public JButton getStopButton() {
if (this.stopButton == null) {
this.stopButton = new JButton("Stop");
this.stopButton.setPreferredSize(new Dimension(75, 22));
this.stopButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (timer.isRunning()) {
timer.stop();
}
}
});
}
return this.stopButton;
}
public void go() {
ActionListener action = new ActionListener() {
public void actionPerformed(ActionEvent e) {
TimerTest.this.label.setText(getTime());
}
};
this.timer = new Timer(1000, action);
this.timer.start();
}
public String getTime() {
this.calendar.add(Calendar.SECOND, 1);
return this.dateFormat.format(this.calendar.getTime());
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(new TimerTest());
frame.setSize(300, 75);
frame.setVisible(true);
}
}
Certinho cara, existe algum método para eu dar um reset no Timer?, que no meu caso preciso mostrar o tempo de uma ligação, mas quando clico no botão que inicia e volta do ponto que estava, qual a melhor maneira para fazer isso?
Coloca o trecho de codigo
this.calendar.set(Calendar.MILLISECOND, 0);
this.calendar.set(Calendar.SECOND, 0);
this.calendar.set(Calendar.MINUTE, 0);
this.calendar.set(Calendar.HOUR_OF_DAY, 0);
Em um metodo separado… Dai chame esse metodo no construtor, para a primeira inicializacao, e no stop tb!!!
Dai ele vai zerar o calendar!
Show de bola, 5 estrelas!
Não é t.stop() e sim t.cancel(), o cara deve ter confundido com Threads.
Abraços.