Chamar outra janela apos algum tempo

10 respostas
T

Bom dia galera!!!

Bom sou muito leigo em java, mas estou iniciando e estou amando essa linguagem.

Gostaria de saber quem pode me ejudar, pois presciso chamar outra janela apos 10 segundos que a tela inicial estiver ativa.

Como Fazer?

Valeu galera

10 Respostas

j0nny

estude sobre o método sleep da classe Thread…

marcelo.bellissimo

Ou… faça uma gambiarra nojenta com o currentTimeMillis()… :?

public void carregaTelaComDelay(long segundos) { long inicio = System.currentTimeMillis(); // verificação: System.out.println(Calendar.getInstance().get(Calendar.SECOND)); while (System.currentTimeMillis() < inicio + (segundos * 1000)){ // nada... } // verificação: System.out.println(Calendar.getInstance().get(Calendar.SECOND)); }

Éca… estude Thread… :lol:

viniciusgundim

Pode usar o seguinte:

public static final long TEMPO = (1000 * 3); //FAZ VERIFICAÇÃO A CADA 3 SEGUNDOS //****INICIA A TAREFA ELE VERIFICA A CADA UM MINUTO****// System.out.println("Inicio"); Timer timer = null; if (timer == null) { timer = new Timer(); TimerTask tarefa = new TimerTask() { public void run() { try { //chamar metodo new janela().show(); } catch (Exception e) { e.printStackTrace(); } } }; timer.scheduleAtFixedRate(tarefa, TEMPO, TEMPO); }

marcelo.bellissimo
viniciusgundim:
Pode usar o seguinte:
public static final long TEMPO = (1000 * 3); //FAZ VERIFICAÇÃO A CADA 3 SEGUNDOS
//****INICIA A TAREFA ELE VERIFICA A CADA UM MINUTO****//
        System.out.println("Inicio");  
         Timer timer = null;  
         if (timer == null) {  
             timer = new Timer();  
             TimerTask tarefa = new TimerTask() {  
                 public void run() {  
                     try {  
                         //chamar metodo
                         System.out.println("Teste Agendador");
                         enviar_email();
                          
                     } catch (Exception e) {  
                         e.printStackTrace();  
                     }  
                 }  
             };  
             timer.scheduleAtFixedRate(tarefa, TEMPO, TEMPO);  
         }

É uma boa alternativa também... apenas para conhecimento, a classe TimerTask implementa a interface Runnable, assim como a classe Thread... por isso o funcionamento delas é muito parecido...

T

Entao galera não entendi muito bem. baixo segue o codigo da minha janela, aonde quando clico no botao ela chama a outra janela, mas queria fazer o seguinta assim que a tela ser executada apos 10 segundo dela ativa ela chamar a outra.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.Timer;

  


public class Entra extends JFrame implements ActionListener {

	private JButton tela;
	private JPanel pn1,pn2;
	private JLabel lb1,lb2;
 	private Timer t;
 	
	ImageIcon  busgrande= new ImageIcon("busgrande.png");
	ImageIcon  ld = new ImageIcon("load.gif");
	
    public Entra() {
    	
    	super("Empresa de transporte Intermunicipal");
    	setSize(350,350);
    	setDefaultCloseOperation(EXIT_ON_CLOSE);
    	pn1=new JPanel (new GridLayout(1,1));
    	pn2=new JPanel (new GridLayout(1,1));
    	lb1= new JLabel(ld);
    	    	
    	tela =new JButton(busgrande);
    	tela.addActionListener(this);
    	tela.setBackground(Color.WHITE);
    	
    	pn1.add(tela);
    	pn2.add(lb1);
    	
    	pn2.setBackground(Color.WHITE);
   
    	getContentPane().add(pn1,BorderLayout.CENTER);
    	getContentPane().add(pn2,BorderLayout.SOUTH);
    	  	 
    	show();
    }
    
     public void actionPerformed (ActionEvent evento)
    {
    
    	if (evento.getSource()==tela)
    	{
    		
    		Onibus o = new Onibus();
    		setVisible(false);
    	}
    		
    }
    
  	 
   
    public static void main(String [] args)
    {
    	Entra E = new Entra();
    }
    
    
}
viniciusgundim

Testa aiii:

  1. import javax.swing.*;

  2. import java.awt.*;

  3. import java.awt.event.*;

  4. import java.io.*;

  5. import javax.swing.Timer;

  6. public class Entra extends JFrame implements ActionListener {

  7. public static final long TEMPO = (1000 * 10);

  8. private JButton tela;
    
  9. private JPanel pn1,pn2;
    
  10. private JLabel lb1,lb2;
    
  11. private Timer t;
    
  12. ImageIcon  busgrande= new ImageIcon("busgrande.png");
    
  13. ImageIcon  ld = new ImageIcon("load.gif");
    
  14. public Entra() {
    
  15. initComponents();
             //****INICIA A TAREFA ELE VERIFICA A CADA UM MINUTO****//
    
    System.out.println(Inicio);
    
    Timer timer = null;
    
    if (timer == null) {
    
    timer = new Timer();
    
    TimerTask tarefa = new TimerTask() {
    
    public void run() {
    
    try {
    
    //chamar metodo
    
    System.out.println(Teste Agendador);
    
    } catch (Exception e) {  
                   e.printStackTrace();  
               }  
           }  
       };  
       timer.scheduleAtFixedRate(tarefa, TEMPO, TEMPO);
    

    }

  16. super("Empresa de transporte Intermunicipal");
    
  17. setSize(350,350);
    
  18. setDefaultCloseOperation(EXIT_ON_CLOSE);
    
  19. pn1=new JPanel (new GridLayout(1,1));
    
  20. pn2=new JPanel (new GridLayout(1,1));
    
  21. lb1= new JLabel(ld);
    
  22. tela =new JButton(busgrande);
    
  23. tela.addActionListener(this);
    
  24. tela.setBackground(Color.WHITE);
    
  25. pn1.add(tela);
    
  26. pn2.add(lb1);
    
  27. pn2.setBackground(Color.WHITE);
    
  28. getContentPane().add(pn1,BorderLayout.CENTER);
    
  29. getContentPane().add(pn2,BorderLayout.SOUTH);
    
  30. show();
    
  31. }
    
  32. public void actionPerformed (ActionEvent evento)
    
  33. {
    
  34. if (evento.getSource()==tela)
    
  35. {
    
  36. Onibus o = new Onibus();
    
  37. setVisible(false);
    
  38. }
    
  39. }
    
  40. public static void main(String [] args)
    
  41. {
    
  42. Entra E = new Entra();
    
  43. }
    
  44. }

marcelo.bellissimo

É só usar qualquer um dos exemplos, pode ser o meu mesmo, pra fins de teste... no final do código, voce chama seu método...

Exemplo:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;

public class Entra extends JFrame implements ActionListener {

	private JButton tela;
	private JPanel pn1, pn2;
	private JLabel lb1, lb2;
	private Timer t;

	ImageIcon busgrande = new ImageIcon("busgrande.png");
	ImageIcon ld = new ImageIcon("load.gif");

	public Entra() {

		super("Empresa de transporte Intermunicipal");
		setSize(350, 350);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		pn1 = new JPanel(new GridLayout(1, 1));
		pn2 = new JPanel(new GridLayout(1, 1));
		lb1 = new JLabel(ld);

		tela = new JButton(busgrande);
		tela.addActionListener(this);
		tela.setBackground(Color.WHITE);

		pn1.add(tela);
		pn2.add(lb1);

		pn2.setBackground(Color.WHITE);

		getContentPane().add(pn1, BorderLayout.CENTER);
		getContentPane().add(pn2, BorderLayout.SOUTH);

		show();
	}

	public void actionPerformed(ActionEvent evento) {

		if (evento.getSource() == tela) {

			Onibus o = new Onibus();
			setVisible(false);
		}

	}

	public static void main(String[] args) {
		carregaTelaComDelay(10);
	}

	public static void carregaTelaComDelay(long segundos) {
		long inicio = System.currentTimeMillis();
		while (System.currentTimeMillis() < inicio + (segundos * 1000)) {
			// nada...
		}
		Entra E = new Entra();
	}
}
T

Blz cara mas e o seguinte ela nao carrega a primeira pagina ele da o tempo e chama a segunda tela

marcelo.bellissimo

Que primeira página? Só existe um frame aí…

T

Cara resolvi nao tinha criado uma label e por isso o erro vlw pessoal

Criado 18 de maio de 2010
Ultima resposta 18 de mai. de 2010
Respostas 10
Participantes 4