Pode parer imbecil minha pergunta, mas realmente não estou sabendo como fazer.
Alguém sabe me informar como fechar um Frame? Tipo o comando “Close” do Delphi? Tipo cliclar no icone “X” de fechar da janela, mas quero um botão q faça isso…
Agradeço,
Fábio.
O método hide() do JFrame, ou dispose().
O problema do hide(), dispose() ou setVisivle(false) é que com eles não passa no evento “windowClosing”…
eu dou uma mensagem no windowClosing… fechando o Frame pelo “X” de fechar passa corretamente e exibe minha mensagem, com esses metodos acima não passa no windowClosing e minha mensagem não aparece…
keller
Setembro 14, 2004, 5:43pm
#4
System.exit(0);
ou…
se for JFrame…
seuJFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
é JFrame sim mas o System.exit(0) não vai sair da aplicação?
Não é isso que eu quero.
Tenho duas telas, uma chama a outra, quero fechar a segunda e continuar na aplicação.
keller
Setembro 15, 2004, 6:35pm
#6
entao use…
seuJFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
O problema é que:
meuJFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
é igual a se eu fechasse com System.exit(0);
pois os dois modos vão finalizar a aplicação e eu quero apenas fechar o segundo JFrame.
um exemplo disso:
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
public class FecharJFrame extends JFrame {
public FecharJFrame() {
super (“Como Fechar um JFrame”);
Container tela = getContentPane();
tela.setLayout(new FlowLayout());
JButton btn = new JButton("Abrir Outro JFrame");
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
SeguntaTela segTela = new SeguntaTela();
segTela.setVisible(true);
}
});
tela.add(btn);
setSize(300, 150);
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] args) {
FecharJFrame app = new FecharJFrame();
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private class SeguntaTela extends JFrame {
public SeguntaTela() {
super (“Quero Fechar essa Tela”);
Container tela = getContentPane();
tela.setLayout(new FlowLayout());
JButton btn = new JButton("hide()");
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
hide();
}
});
JButton btn2 = new JButton("dispose()");
btn2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dispose();
}
});
JButton btn3 = new JButton("setVisible(false)");
btn3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
setVisible(false);
}
});
JButton btn4 = new JButton("System.exit(0)");
btn4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
JLabel label = new JLabel("Experimente Fechar com 'ALT + F4' ou 'X' da Barra de Titulos");
JLabel label2 = new JLabel("Alguém sabe fechar com mesmo efeito do 'ALT + F4'?");
tela.add(btn);
tela.add(btn2);
tela.add(btn3);
tela.add(btn4);
tela.add(label);
tela.add(label2);
EventWindowClosing eventClosing = new EventWindowClosing(this);
this.addWindowListener(eventClosing);
setSize(600, 150);
setLocationRelativeTo(null);
}
}
private class EventWindowClosing extends WindowAdapter {
private JFrame owner;
public EventWindowClosing(JFrame owner) {
this.owner = owner;
}
public void windowClosing(WindowEvent e) {
JOptionPane.showMessageDialog(owner, "Fechou Corretamente.", "Informação",
JOptionPane.INFORMATION_MESSAGE);
}
}
}
o JFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
faz o mesmo efeito de System.exit(0);
sai da segunda tela…
coloquei um exemplo desse problema:
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
public class FecharJFrame extends JFrame {
public FecharJFrame() {
super (“Como Fechar um JFrame”);
Container tela = getContentPane();
tela.setLayout(new FlowLayout());
JButton btn = new JButton("Abrir Outro JFrame");
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
SeguntaTela segTela = new SeguntaTela();
segTela.setVisible(true);
}
});
tela.add(btn);
setSize(300, 150);
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] args) {
FecharJFrame app = new FecharJFrame();
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private class SeguntaTela extends JFrame {
public SeguntaTela() {
super (“Quero Fechar essa Tela”);
Container tela = getContentPane();
tela.setLayout(new FlowLayout());
JButton btn = new JButton("hide()");
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
hide();
}
});
JButton btn2 = new JButton("dispose()");
btn2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dispose();
}
});
JButton btn3 = new JButton("setVisible(false)");
btn3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
setVisible(false);
}
});
JButton btn4 = new JButton("System.exit(0)");
btn4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
JLabel label = new JLabel("Experimente Fechar com 'ALT + F4' ou 'X' da Barra de Titulos");
JLabel label2 = new JLabel("Alguém sabe fechar com mesmo efeito do 'ALT + F4'?");
tela.add(btn);
tela.add(btn2);
tela.add(btn3);
tela.add(btn4);
tela.add(label);
tela.add(label2);
EventWindowClosing eventClosing = new EventWindowClosing(this);
this.addWindowListener(eventClosing);
setSize(600, 150);
setLocationRelativeTo(null);
}
}
private class EventWindowClosing extends WindowAdapter {
private JFrame owner;
public EventWindowClosing(JFrame owner) {
this.owner = owner;
}
public void windowClosing(WindowEvent e) {
JOptionPane.showMessageDialog(owner, "Fechou Corretamente.", "Informação",
JOptionPane.INFORMATION_MESSAGE);
}
}
}
keller
Setembro 16, 2004, 9:46am
#10
entao use o
seuFrame.hide();
ou
seuFrame.setVisible(false);
ou
seuFrame.dispose();
espero ter ajudado…
eumlmp
Agosto 19, 2010, 12:21pm
#11
Ola, pelo que eu entendi, você deseja executar sua janelinha de mensagem tanto quando fechando com o “X” quanto com o botão correto?!
Logo eu sugiro que vc dento do metodo ActionPerformed do seu botão vc lance esta exeção especifica:
public void botaoActionPerformed(MouseEvent me) throws WindowClosing { //não sei a chamada da exeption, ai vc vê lá
//seus tratamentos
throw(new WindowClosing);
this.defuse();
//encerrando a janela
}
Espero ter ajudado.