Problems with setDefaultCloseOperation()
Hey guys. I need to organize my panels. I want to when i hit the button "Cadastrar nova conta", my PainelInicial screen close automatically. I wanted to use the method setDefaultCloseOperation, but my events are an a inner class, so i don't know how to do it.
PS: Sorry about my bad english and don't worry about my classes names and method names.
I hope you guys can help me.
Here is my code:
Code:
public class PainelInicial extends JFrame {
public PainelInicial() {
super("Banco IFSC");
this.setResizable(false);
this.setSize(350,70);
add(montaPainelInicial());
}
public JPanel montaPainelInicial() {
JPanel p = new JPanel();
JButton bAcessa = new JButton("Acessar sua conta");
JButton bCadastra = new JButton("Cadastrar nova conta");
bCadastra.addActionListener(new CadastraConta());
p.add(bAcessa);
p.add(bCadastra);
return p;
}
public static void main(String [] args) {
PainelInicial p = new PainelInicial();
p.show();
}
}
class CadastraConta implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
PainelCadastro p = new PainelCadastro();
p.show();
}
}
Re: Problems with setDefaultCloseOperation()
Here's some code I just copied out of one of my classes that extends JFrame:
Code:
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
MainWindow.this.weapon.shutdown();
}
});
This disables the default close operation and replaces it with an anonymous event handler that calls my application's shutdown method. The shutdown method closes any open resources and then calls System.exit().
You can do the same thing in your button's action listener. If you do not want the app to exit when you click the window manager's close button, don't use the WindowListener like I did.