Pessoal, eu uso NetBeans, e estou precisando fazer um timer… 1- Esse timer vai fazer uma contagem regreciva de 30segundos (de 30 até 0). 2- A contagem regreciva tem que ser mostrada numa jLabel, atualizando de 1 em 1 segundo 3- Quando clicar no botão cancelar, o timer para e volta para 30 segundos.
[color=red]Ps: [/color]Antes que os maliciosos pensem, não é trabalho de escola não *É pq eu fiz isso em delphi, e usava o componente timer, o código ficava simples… Ae resolvi tentar fazer em java…
O código que ficaria dentro do contador seria:
int tempo = 30
tempo := tempo -1;
String hihi = new Integer(tempo).toString();
jLabel11.setText(hihi);
Mas não sei como implementar o timer de 1 em 1 segundo… Qualquer ajuda é bem vinda… Valew!
Pow… Eu rodei o código deles aki, e dá certo…
Só que eu mecho com frame, então não precisa dakilo de criar um frame, criar uma jlabel, setar o tamanho deles e tal…
Mas nesses dae é pra mostrar as horas… não um contador de segundo…
Na verdade o que ele faz é pegar a hora do sistema…
Mas mesmo assim vo tentar entender a sintaxe e fazer aqui…
Por enquanto… Não Resolvido
[code]public class tempo4 extends javax.swing.JFrame {
DateFormat dateFormat;
Calendar calendar;
Timer timer;
boolean janela = true;
/** Creates new form tempo4 */
public tempo4() {
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();
initComponents();
}
protected void initialize() {
this.add(this.getLabel());
this.go();
}
public JLabel getLabel() {
if (this.jLabel1 == null) {
this.jLabel1 = new JLabel(getTime());
//this.label.setPreferredSize(new Dimension(100, 22));
}
return this.jLabel1;
}
public void go() {
ActionListener action = new ActionListener() {
public void actionPerformed(ActionEvent e) {
jLabel1.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());
}
//abaixo os métodos dos botoes
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if (!timer.isRunning()) {
timer.start();
}
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if (timer.isRunning()) {
timer.stop();
}
}
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
this.dispose();
JOptionPane.showMessageDialog(null, "Você irá para a próxima janela!");
new tempo4().setVisible(true);
} [/code]
Só q é o seguinte: ao clikar no 3jButton, ele fecha minha janela, e apos a mensagem, chama ela d novo…
Só q eu queria q ao abrir a janela d novo, ele continuasse o timer, com o tempo q ele parou antes, e não q reiniciasse a cada vez q abrir a janela d novo…