Duviiida implementaÇao de tempo [ajuda ae galera por favor urgente] [resolvido]

22 respostas
evertonsilvagomesjav

BOMMM GALERA SEGUINTE O TEMPO NAO ESTA SENDO EXIBIDO EM SEGUNDOS MESMO ESTA COM MAIS CASAS, EU QUERIA QUE EXIBISSE EM SEGUNDOS SOMENTE, SEGUE O COD:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Tempo.java
 *
 * Created on 18/10/2009, 11:53:32
 */

/**
 *
 * @author Everton
 */
public class Tempo extends javax.swing.JFrame {

	Long tempoInicial;
	Long tempoFinal ;
	Long resultado;
	
    /** Creates new form Tempo */
    public Tempo() {
        initComponents();
    }

    /** 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">
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        botaoIniciar = new javax.swing.JButton();
        botaoParar = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        textField2 = new java.awt.TextField();
        textField3 = new java.awt.TextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBackground(new java.awt.Color(204, 204, 204));
        jPanel1.setForeground(new java.awt.Color(153, 153, 153));

        jLabel1.setFont(new java.awt.Font("Monotype Corsiva", 3, 24)); // NOI18N
        jLabel1.setText("CONTAGEM DE TEMPO");

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 10)); // NOI18N
        jLabel2.setText("Resultado Final");

        botaoIniciar.setText("INICIAR");
        botaoIniciar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botaoIniciar(evt);
            }
        });

        botaoParar.setText("PARAR");
        botaoParar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botaoParar(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 10)); // NOI18N
        jLabel3.setText("TEMPO CORRENTE");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(160, Short.MAX_VALUE)
                .addComponent(jLabel1)
                .addGap(132, 132, 132))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(92, 92, 92)
                        .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, 236, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 10, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(120, 120, 120)
                        .addComponent(botaoIniciar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(textField2, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(121, 121, 121)))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(30, 30, 30)
                        .addComponent(botaoParar))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(textField3, javax.swing.GroupLayout.PREFERRED_SIZE, 152, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(31, 31, 31)
                        .addComponent(jLabel2)))
                .addGap(55, 55, 55))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(34, 34, 34)
                .addComponent(jLabel1)
                .addGap(61, 61, 61)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel3)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(textField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(textField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(52, 52, 52)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(botaoIniciar)
                    .addComponent(botaoParar))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>

    private void botaoIniciar(java.awt.event.ActionEvent evt) {
       
    	tempoInicial = System.currentTimeMillis();
    }

    private void botaoParar(java.awt.event.ActionEvent evt) {

    	tempoFinal = System.currentTimeMillis() ;
    	
    	
    	resultado = (tempoFinal - 1000) - (tempoInicial - 1000);
    	
    	textField3.setText(resultado.toString());
    	
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Tempo().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify
    private javax.swing.JButton botaoIniciar;
    private javax.swing.JButton botaoParar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private java.awt.TextField textField2;
    private java.awt.TextField textField3;
    // End of variables declaration

}

22 Respostas

T

Para dividir algo por 1000, você tem de usar “/ 1000”, não “- 1000”. OK?

evertonsilvagomesjav

hahusauhshushusahushushushusahu no vei que manota vlw ai thingol kkkkkkkkkkkkkk

evertonsilvagomesjav

thingol agora minha segundo duvida, eu tenho um jText onde eu queria que ficasse computando o tempo ate eu parar vc pode me dizer como eu faço nao sei nem como começar.

evertonsilvagomesjav

ajuda ae?

ViniGodoy

Dá uma olhada nesse tópico:
http://www.guj.com.br/posts/list/52964.java#279083

E nos dois depois dele, que estão na página 2. É por aí que vc começa. :wink:

evertonsilvagomesjav

Vini isso ai ta meio complexo eu acho né para o que eu quero, pq eu to pegando a hora pelo System.TimeMillis() e subtraindo so pra pegar o segundos queria a contagem apenas disso.

ViniGodoy

Sim, mas para “ficar fazendo isso sozinho até vc parar”, você vai precisar de um timer.

evertonsilvagomesjav

to tentando entender esses exemplos vini mas ta foda mesmo cara nao tenho experiencia ainda em java ta foda de aplicar no meu cod, tem como me ajudar? vo postar aqui o cod:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Tempo.java
 *
 * Created on 20/10/2009, 10:49:44
 */

package Interfaces;

/**
 *
 * @author everton
 */
public class Tempo extends javax.swing.JFrame {

    Long tempoInicial;

    /** Creates new form Tempo */
    public Tempo() {
        initComponents();
    }

    /** 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">
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jButtonIniciar = new javax.swing.JButton();
        jButtonParar = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        textField = new java.awt.TextField();
        jLabel3 = new javax.swing.JLabel();
        textFieldTempoFinal = new java.awt.TextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Monotype Corsiva", 3, 36)); // NOI18N
        jLabel1.setText("Tempo");

        jButtonIniciar.setText("Iniciar");
        jButtonIniciar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                iniciar(evt);
            }
        });

        jButtonParar.setText("Parar");
        jButtonParar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                parar(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N
        jLabel2.setText("Tempo Corrente");

        jLabel3.setFont(new java.awt.Font("Tahoma", 2, 14)); // NOI18N
        jLabel3.setText("Tempo FInal");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(23, 23, 23)
                        .addComponent(textField, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 258, Short.MAX_VALUE)
                        .addComponent(jButtonIniciar)
                        .addGap(18, 18, 18)
                        .addComponent(jButtonParar))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(36, 36, 36)
                        .addComponent(jLabel2))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(210, 210, 210)
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(215, 215, 215))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(170, 170, 170)
                        .addComponent(textFieldTempoFinal, javax.swing.GroupLayout.PREFERRED_SIZE, 186, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(218, 218, 218)
                        .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(83, 83, 83)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(textFieldTempoFinal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 64, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jButtonParar)
                        .addComponent(jButtonIniciar))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(textField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>

    private void iniciar(java.awt.event.ActionEvent evt) {

        tempoInicial = System.currentTimeMillis() / 1000;



    }

    private void parar(java.awt.event.ActionEvent evt) {

        Long tempoFinal = System.currentTimeMillis() / 1000;

        Long resultado = tempoFinal - tempoInicial;

        textFieldTempoFinal.setText(resultado.toString());

    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Tempo().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify
    private javax.swing.JButton jButtonIniciar;
    private javax.swing.JButton jButtonParar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private java.awt.TextField textFieldTempoCorrente; //nesse texField eu quero que o tempo fique correndo ate ser precionado o botao parar mas somente em segundos porrrrr favor, AJUDA AE GALERA.
    private java.awt.TextField textFieldTempoFinal;
    // End of variables declaration

}
evertonsilvagomesjav

alguem galera ajuda por favor galera?

evertonsilvagomesjav

GALERA POR FAVOR ME AJUDEM AE, eu preciso entregar esse projeto amanha a noite eu sei que ninguem aqui do guj tem haver com isso mas se tiver alguem ae que saiba do assunto e puder me ajudar ajuda ae galera por favor, so ta faltando a exibiçao do tempo corrente um text field ja olhei varios exemplos tentei fazer aqui mas nao da certo, segue o codigo como ta

import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JOptionPane;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Tempo.java
 *
 * Created on 18/10/2009, 11:53:32
 */

/**
 *
 * @author Everton
 */
public class Tempo extends javax.swing.JFrame {

    Long tempoInicial;
    Long tempoFinal;
    Long resultado;

  private Timer timer;
  private static final int INICIO = 0;
  private static final int DELAY_MILLISEGUNDOS = 1000;

    /** Creates new form Tempo */
    public Tempo() {
        initComponents();
    }

    /** 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">                          
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        botaoIniciar = new javax.swing.JButton();
        botaoParar = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        textField2 = new java.awt.TextField();
        textField3 = new java.awt.TextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBackground(new java.awt.Color(204, 204, 204));
        jPanel1.setForeground(new java.awt.Color(153, 153, 153));

        jLabel1.setFont(new java.awt.Font("Monotype Corsiva", 3, 24)); // NOI18N
        jLabel1.setText("CONTAGEM DE TEMPO");

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 10)); // NOI18N
        jLabel2.setText("Resultado Final");

        botaoIniciar.setText("INICIAR");
        botaoIniciar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botaoIniciar(evt);
            }
        });

        botaoParar.setText("PARAR");
        botaoParar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botaoParar(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 10)); // NOI18N
        jLabel3.setText("TEMPO CORRENTE");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(160, Short.MAX_VALUE)
                .addComponent(jLabel1)
                .addGap(132, 132, 132))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(92, 92, 92)
                        .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, 236, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 10, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(120, 120, 120)
                        .addComponent(botaoIniciar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(textField2, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(121, 121, 121)))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(30, 30, 30)
                        .addComponent(botaoParar))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(textField3, javax.swing.GroupLayout.PREFERRED_SIZE, 152, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(31, 31, 31)
                        .addComponent(jLabel2)))
                .addGap(55, 55, 55))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(34, 34, 34)
                .addComponent(jLabel1)
                .addGap(61, 61, 61)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel3)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(textField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(textField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(52, 52, 52)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(botaoIniciar)
                    .addComponent(botaoParar))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>                        

    private void botaoIniciar(java.awt.event.ActionEvent evt) {                              

        tempoInicial = System.currentTimeMillis() / 1000;

          if(timer == null)
         {
             timer = new Timer();
             timer.schedule(new Timer(), INICIO, DELAY_MILLISEGUNDOS); // nao sei se esta certo tentei fazer aqui mais nao sei se ta certo e o NEW TIMER() esta dando erro aqui. EU PRECISO QUE O TEMPO FIQUE MOSTRANDO EM SEGUNDOS QUANDO CLICADO NO BOTAO INICIAR E PARE QUANDO CLICADO NO BOTAO PARAR. AJUUUUUDA AE GALERA.
         }  
         else
         {
            timer.cancel();

         }

    }                             

    private void botaoParar(java.awt.event.ActionEvent evt) {                            

        tempoFinal = System.currentTimeMillis() / 1000;
        resultado = tempoFinal - tempoInicial;

        textField3.setText(resultado.toString());

    }                           

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Tempo().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton botaoIniciar;
    private javax.swing.JButton botaoParar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private java.awt.TextField textField2;
    private java.awt.TextField textField3;
    // End of variables declaration
evertonsilvagomesjav

???

evertonsilvagomesjav

ALGUEM AJUDA AE PORRR FAVOR?

evertonsilvagomesjav

SO VIM AGRADECER A AJUDA DO CRIADOR, VLW MESMO CARA MUITO OBRIGADO AI PELA AJUDA QUE VC ME DEU!!!

ViniGodoy

É cara, de mão beijada é difícil mesmo. Só se aprende quebrando a cabeça. E nem adianta upar o tópico, colocar o título e texto em maiúsculas, ou implorar por ajuda, práticas essas que são consideradas completamente anti-profissionais no fórum.

Pelo menos, te passei três exemplos, cheios de comentários, e que já resolveu a vida de muita gente.

Agora, você poderia postar sua solução? Assim alguém desesperado que procure por ela poderá usar como referência seu próprio tópico. :wink:

evertonsilvagomesjav

Cara e o seguinte eu nunca tinha usado Runnable eu usei exatamente seu exemplo eu to agradecendo o Criador pq ele me explicou passo a passo o que cada coisa do seu exemplo estava fazendo nao sabia o que era inner class ele me falou, to agradecendo isso nao sabia o que era EventQueue.invokerLater ele me explicou, to agradecendo isso, nao teria como eu entender seu exemplo pq nunca tinha visto aquilo na minha vida fica meio dificil, se pra vc e facil eu nao posso fazer nada pq vc ja tem bastante experiencia em programaçao e eu nao estou apenas começando e agradeço de verdade a ajuda que ele me deu em explicar o seu exemplo pra mim.

ViniGodoy

Ok cara, e a sua solução como ficou? Posta aí pro resto do povo do fórum. :smiley:

Eu poderia ter te explicado, mas geralmente não tenho tempo para isso. Até por isso o máximo que faço é deixar tópicos completos e comentados por aqui, e guardar os links para eles.

evertonsilvagomesjav
private class ClockRunnable implements Runnable {
            public void run() {
                try {
                    while (true) {

                        EventQueue.invokeLater(new Runnable() {
                            public void run() {
                               
                                textField.setText(""+(time++));
                            }
                        });     
                            Thread.sleep(1000);
                    }
                }
                catch (InterruptedException e) {

                }
            }
        }

  
private void iniciar(java.awt.event.ActionEvent evt) {        
clockThread = new Thread(new ClockRunnable());
        clockThread.setDaemon(true);
        clockThread.start();
        textField.setText("");
        
        jButtonParar.setEnabled(true);

      tempoInicial = System.currentTimeMillis() / 1000;
      textFieldTempoFinal.setText(null);
}

 private void parar(java.awt.event.ActionEvent evt) {                       

        clockThread.interrupt();
        textField.setText(""+(time-1));
        time = 0;
        jButtonParar.setEnabled(false);

        Long tempoFinal = System.currentTimeMillis() / 1000;

        Long resultado = tempoFinal - tempoInicial;

        textFieldTempoFinal.setText(resultado.toString());


    }
evertonsilvagomesjav

Eu entendo cara o foda que nao tenho muito conhecimento entao muito dos topicos que eu vejo completos eu nao entendo por isso queria ajuda nao para fazer pra mim pq nao vo andar com perna de ninguem mas alguem que podesse me explicar o que esta acontecendo e realizando assim eu ia entender pra fazer o meu.

ViniGodoy

Sim, sim, eu não tava te criticando… estava constatando.

A gente que entende também se quebrou muito pra chegar aqui, muito café, noites mal dormidas, prazos apertados e até choramingação para professor. :lol:

evertonsilvagomesjav

Criticas de alguem ai como vcs são sempre bem vindas, eu to na luta pra poder aprender e se Deus quiser ainda chego la xD.

lina

Oi,

Claro que você irá conseguir! Continue estudando! Utilize o fórum para retirar suas dúvidas!
Todas são bem-vindas.

Tchauzin!

criador

Pois é.
Também não tinha muito tempo de sobra para ajudá-lo.
Por isso que fiz desta forma e tenho plena conciência que não é a melhor forma de se implementar isto que me pedisse.
Porém é uma solução e se aplicou ao seu problema.

Bom, apesar de não ter sido a melhor forma, valeu o esforço :slight_smile:
Também não sou nenhum expert, estou aprendendo muito com este fórum e sugiro que você sempre siga como exemplo o GUJ que está sendo um grande “professor” pra mim.

Abraço!

Criado 18 de outubro de 2009
Ultima resposta 22 de out. de 2009
Respostas 22
Participantes 5