Como usar tempo?

15 respostas
R

Tipo

como faço pra usar tempo, assim abre um frame e depois de 3s ele desaparece sozinho e o programa continua rodando !

tipo quando o eclipse é iniciado q aparece o logo e depois o programa abre!

15 Respostas

Z

Boa tarde galera !

Seria Thread.sleep() ??

[]s

R

cara eu não sei se é!

por isso quero uma luz :smiley:

Z

Boa tarde Galera,

Desculpe… acho que não fui claro…

Minha resposta anterior era um dica…

Tenta chamar o seu frame e depois que ele estiver na tela dar um Thread.sleep(3000).

[]s

G

Isso que você quer se chama SplashScreen…

R

alguem poderia postra um ex para e entender melhor ?

V

http://www.guj.com.br/articles/25

R

Thread.sleep()

tow usando tah pegando os 3 segundos direitinhoo

import java.awt.Point;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;




public class framenavedestruida extends JFrame {

	
	
	public framenavedestruida(){
		super("Nave Destruida");
		int w = this.getToolkit().getDefaultToolkit().getScreenSize().width;
		int h = this.getToolkit().getDefaultToolkit().getScreenSize().height;
		int z = 2;
		int x = (w - 800) / z;
		int y = (h - 500)/ z;
		this.setSize(800, 500);
		this.setLocation(x,y);
		this.setResizable(false);
		
		String local = System.getProperty("user.dir");  
		JLabel img = new JLabel(new ImageIcon(local+"\\src\\naveRyouta3.jpg"));
		img.setLocation(new Point(0,0));
		img.setSize(800,500);
	    img.setVisible(true);
		this.add(img);
		this.setVisible(true);
		
		
		 try {
             Thread.sleep(3000);
         } catch (InterruptedException e) {
         }
         this.setVisible(false);
		JOptionPane.showMessageDialog(null,"Pow você acertou a nave!\nParabéns" +
		" Você Destruiu a nave 1");
		
		
	}
	public static void main(String[]a){
		new framenavedestruida();
		
		
		
	}
	
	
}
quando execulto essa classe pega direitinho mais quando vou chamar essa classe em outra a imagen naum aparece mais e fica tudo branco dentro do frame!!!
V

Leia o link que te passei.

R

cara eu ja lii mais naum ajudo em nada!!!

mais mesmo assim obg !

V

Acrescente no topo do seu programa um “import javax.swing.Timer;”

Crie um timer no seu construtor assim:

Timer t = new Timer(3000, new ActionListener() { public void actionPerformed(ActionEvent evt) { dispose(); } }); t.setInitialDelay(0); t.setRepeats(false); t.start();

Agora tire aquele thread.sleep que você colocou dali. Ele está errado.

Prontinho! :slight_smile:

R

coloquei mais nadaa!!! o frame não fica os segundos

import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Timer;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;




public class framenavedestruida extends JFrame {

	private Thread thr1;
	
	public framenavedestruida(){
		super("Nave Destruida");
		int w = this.getToolkit().getDefaultToolkit().getScreenSize().width;
		int h = this.getToolkit().getDefaultToolkit().getScreenSize().height;
		int z = 2;
		int x = (w - 800) / z;
		int y = (h - 500)/ z;
		this.setSize(800, 500);
		this.setLocation(x,y);
		this.setResizable(false);
		
		
		String local = System.getProperty("user.dir");  
		JLabel img = new JLabel(new ImageIcon(local+"\\src\\naveRyouta3.jpg"));
		img.setLocation(new Point(0,0));
		img.setSize(800,500);
	    img.setVisible(true);
		this.add(img);
		JOptionPane.showMessageDialog(null,"Pow você acertou a nave!\nParabéns" +
		" Você Destruiu a nave 1");
		
		
		this.setVisible(true);
	    Timer t = new Timer(3000, new ActionListener() {  
	        public void actionPerformed(ActionEvent evt) {  
	           dispose();  
	        }

		
			 
	     });  
	     t.setInitialDelay(0);    
	     t.setRepeats(false);    
	     t.start();    
		
		
		
		
	}
	public static void main(String[]a){
		new framenavedestruida();
		
		
		
	}
	
	
}
V

Aproveitei e já corrigi um monte de coisas erradas no seu código.
Além disso, lembre-se que o JOptionPane para o programa na linha em que é chamado, portanto, seu timer não seria disparado.

Se sua intenção é fazer um jogo, recomendo que você comece a ler os tutoriais de Java do Ponto V!.
Até porque não é uma boa fazer jogos usando componentes do Swing, como o JLabel.

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.Timer;

@SuppressWarnings("serial")
public class FrameNaveDestruida extends JFrame {
	public FrameNaveDestruida() {
		super("Nave Destruida");
		this.setSize(800, 500);
		this.setLocationRelativeTo(null);
		this.setResizable(false);
		this.setLayout(new BorderLayout());
		
		String local = System.getProperty("user.dir");
		JLabel img = new JLabel(new ImageIcon(local + "\\src\\naveRyouta3.jpg"));
		this.add(img, BorderLayout.CENTER);
		
		Timer t = new Timer(0, new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				dispose();
			}
		});

		this.setVisible(true);
		t.setInitialDelay(3000);
		t.setRepeats(false);
		t.start();
		JOptionPane.showMessageDialog(this, "Pow você acertou a nave!\n" +
				"Parabéns Você Destruiu a nave !");
	}

	public static void main(String[] a) {
		new FrameNaveDestruida();
	}
}
R

ainda tah errado amigo!!!

era para o JOpitionPane aparecer depois q o frame foce feixado!!!

V

Então coloca a linha dele logo depois do dispose():

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.Timer;

@SuppressWarnings("serial")
public class FrameNaveDestruida extends JFrame {
	public FrameNaveDestruida() {
		super("Nave Destruida");
		this.setSize(800, 500);
		this.setLocationRelativeTo(null);
		this.setResizable(false);
		this.setLayout(new BorderLayout());
		
		String local = System.getProperty("user.dir");
		JLabel img = new JLabel(new ImageIcon(local + "\\src\\naveRyouta3.jpg"));
		this.add(img, BorderLayout.CENTER);
		
		Timer t = new Timer(0, new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				dispose();
				JOptionPane.showMessageDialog(FrameNaveDestruida.this, "Pow você acertou a nave!\n" +
				   "Parabéns Você Destruiu a nave !");
			}
		});

		this.setVisible(true);
		t.setInitialDelay(3000);
		t.setRepeats(false);
		t.start();
	}

	public static void main(String[] a) {
		new FrameNaveDestruida();
	}
}
R

agora tah certinhoo nessa classe!!!

Criado 23 de abril de 2011
Ultima resposta 25 de abr. de 2011
Respostas 15
Participantes 4