Fechar JFrame

5 respostas
gqferreira

Olá pessoal!

Como faço para fechar um JFrame atrávez de uma Thread?
Tenho uma classe que é uma Thread e quero que quando ela terminar, ela feche uma janela.
Tentei isso:

InstalarFrame frame = new InstalarFrame();
		frame.dispose();

Mas não deu certo…
Essa janela está aberta.

5 Respostas

ViniGodoy

É assim mesmo.
Pode colocar mais código, por favor?

PS: Todo sistema tem pelo menos 1 thread. Se funciona na thread inicial do sistema, tem que funcionar para outras threads também.

gqferreira

Colocar código ta meio complicado, é muito, mas a parte em questão eu esquematizei na imagem.

Obs: Quem tem que fechar a MeuJframe é a MinhaThread


gqferreira

Poxa… :shock: Achei que o meu problema era simples… :?

Ironlynx

gqferreira, tá vaga a sua figura.Tem certeza que depois dos 10 segundos sua Thread foi finalizada?E não consegue fechar como?O que tá dizendo no prompt?Congelou?

E vc pode postar o seu código aqui sem precisar usar a tag code(quando é muito grande, geralmente maior do que 500 linhas).Zipa ele(os .java) num arquivo zip e posta como anexo.(Acho que dificilmente ficará maior do que 500KB, certo?500KB zipados de .java é codigo á beça, quase um windows…huahauha)

gqferreira

Fiz uma gambiarra aqui que funcionou mas com certeza dever ter jeito melhor..^^

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.WindowConstants;


public class Pai extends javax.swing.JFrame {

	{
		//Set Look & Feel
		try {
			javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName());
		} catch(Exception e) {
			e.printStackTrace();
		}
	}

	private JLabel lb;
	private JButton bt;

	/**
	 * Auto-generated main method to display this JFrame
	 */
	public static void main(String[] args) {
		Pai inst = new Pai();
		inst.setLocationRelativeTo(null);
		inst.setVisible(true);
	}


	public Pai() {
		try {
			setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
			getContentPane().setLayout(null);
			{
				lb = new JLabel();
				getContentPane().add(lb);
				lb.setText("PAI");
				lb.setBounds(88, 51, 173, 103);
				lb.setFont(new java.awt.Font("Arial Black",1,72));
			}
			{
				bt = new JButton();
				getContentPane().add(bt);
				bt.setText("Chamar filho");
				bt.setBounds(88, 183, 165, 28);
				bt.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						Filho f = new Filho();
						f.setVisible(true);
						
						MinhaThread m = new MinhaThread();
						Thread t = new Thread(m);
						t.start();
						
						m.filho=f;
						
					}
				});
			}

			this.setSize(361, 269);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
import javax.swing.JLabel;


public class Filho extends javax.swing.JFrame {
	private JLabel lb;

	/**
	* Auto-generated main method to display this JFrame
	*/
		
	public Filho() {
		try {
			getContentPane().setLayout(null);
			{
				lb = new JLabel();
				getContentPane().add(lb);
				lb.setText("FILHO");
				lb.setBounds(44, 48, 329, 139);
				lb.setFont(new java.awt.Font("Arial Black",1,72));
			}

			setSize(400, 300);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
public class MinhaThread implements Runnable{

	public Filho filho;
	
	@Override
	public void run() {
		try {
			Thread.sleep(10000);
			
			filho.dispose();//    <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
			
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

}

Tô muito errado? :lol: ... Pelo menos agora consegui fazer funcionar.. :?

Criado 18 de julho de 2009
Ultima resposta 23 de jul. de 2009
Respostas 5
Participantes 3