Fechar frame

2 respostas
Nelson_Rodrigo_Furla

bom dia pessoal,

segue um exemplo do problema que estou tendo em fechar um frame…
se alguem puder me ajudar, ficarei agradeciso.

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

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class tela extends javax.swing.JFrame implements ActionListener {

	public tela() {
		try {
			initComponents();
		} catch (Throwable e) {
			e.printStackTrace();
		}
	}

	public void initComponents() throws Throwable {
		setSize(300, 200);
		setTitle("Inicio");
		button2 = new JButton("ir p/ outro frame");
		button2.setBounds(30, 30, 200, 40);
		button2.addActionListener(this);
		label = new JLabel("antes");
		label.setBounds(30,90,100,40);
		getContentPane().add(label);
		getContentPane().add(button2);
		getContentPane().setLayout(null);
		setVisible(true);
	}

	public void frame1() throws Throwable {
		JFrame frame1 = new JFrame();
		frame1.setSize(600, 170);
		button = new JButton("COMO EU FAÇO PARA FECHAR ESSE FRAME");
		button.setBounds(30, 30, 550, 40);
		button.addActionListener(this);
		frame1.getContentPane().add(button);
		frame1.getContentPane().setLayout(null);
		frame1.setVisible(true);
	}

	public void actionPerformed(ActionEvent e) {

		if (e.getSource() == button2) {
			try {
				frame1();
			} catch (Throwable e1) {
				e1.printStackTrace();
			}
		}

		if (e.getSource() == button) {
			try {
				label.setText("depois");
				
				/*COMO EU FAÇO PARA FECHAR ESSE FRAME???? */
			} catch (Throwable e1) {
				e1.printStackTrace();
			}
		}

	}

	public static void main(String[] args) {

		new tela();

	}

	private JButton button2;
	private JButton button;
	private JLabel label;
	private static final long serialVersionUID = 1L;

}

2 Respostas

cristianosanchez

setVisible(false) para esconder o frame e dispose() para fechar os recursos utilizados.

http://java.sun.com/javase/6/docs/api/java/awt/Window.html#setVisible(boolean)
http://java.sun.com/javase/6/docs/api/java/awt/Window.html#dispose()

ViniGodoy

É só escrever
dispose();

Na linha onde está o comentário da dúvida.

Criado 31 de outubro de 2008
Ultima resposta 31 de out. de 2008
Respostas 2
Participantes 3