Bah… to com um problema, queria salvar um arquivo automaticamente quando o programa vai ser encerrado
[code]import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class FrameLogin extends FrameInicial{
PanelLogin pl;
public FrameLogin(Cofrinho cof, String nomeAr){
setTitle("Tela de Login");
this.add(pl = new PanelLogin(this, cof, nomeAr));
this.pack();
this.add(pl);
this.setLocationRelativeTo(null);
this.setLayout(null);
this.setContentPane(pl);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.setResizable(false);
this.setSize(pl.getSize());
}
public void fechaJanela(){
this.setVisible(false);
this.dispose();
}
class PanelLogin extends PanelInicial{
JPasswordField pf;
JTextField tf;
JButton ok;
String nome;
String senha;
String s;
boolean statusNome;
boolean statusSenha;
private JFrame frame;
JLabel tx1;
JLabel pf1;
private Cofrinho c;
int count = 0;
String nomeArquivo;
public PanelLogin(JFrame f, Cofrinho cof, String nomeAr){
super(f);
nomeArquivo = nomeAr;
c = cof;
frame = f;
tx1 = new JLabel("Digite o nome");
tf = new JTextField("************");
tf.addActionListener(this);
pf1 = new JLabel("Digite a senha");
pf = new JPasswordField("*********");
pf.addActionListener(this);
ok = new JButton("Seguir");
ok.addActionListener(this);
JPanel buttonPanel = new JPanel(); //use FlowLayout
JPanel buttonPanel2 = new JPanel();
JPanel buttonPanel3 = new JPanel();
buttonPanel.add(tx1);
buttonPanel.add(tf);
buttonPanel2.add(pf1);
buttonPanel2.add(pf);
buttonPanel3.add(ok);
add(buttonPanel, BorderLayout.PAGE_START);
add(buttonPanel2, BorderLayout.CENTER);
add(buttonPanel3, BorderLayout.PAGE_END);
}
public boolean validaLogin(String login, String senha){
boolean usuario = false;
boolean adm = false;
if(c.getNome().equals(login) == true && c.getSenha().equals(senha)){
usuario = true;
}
if(login.equals(c.getNomeAdm()) == true && senha.equals(c.getSenhaAdm()) == true){
adm = true;
}
if(usuario == true | adm == true)
return true;
else
return false;
}
private void gravaObjeto (File f, Cofrinho o){
try {
FileOutputStream fos = new FileOutputStream (f);
ObjectOutputStream os = new ObjectOutputStream (fos);
os.writeObject (o);
os.close ();
}catch (IOException e) {
System.out.println ("Erro ao gravar objeto.");
}
}
public void actionPerformed(ActionEvent e) {
String resp;
boolean status;
if(c != null)
if(e.getSource() == ok){
if(c.getNome() != null && c.getSenha() != null && count < 2){
resp = new String(pf.getPassword());
status = this.validaLogin(tf.getText(), resp);
if(status == true){
FramePrincipal fp = new FramePrincipal(c);
this.setVisible(false); // Faz "desaparecer" essa janela
this.fecha();
}
else if(status == false){
JOptionPane.showMessageDialog(null, "Nome, ou senha Incorretos!", "Failed to Login!", JOptionPane.ERROR_MESSAGE);
count++;
}
}
else if(count >= 2){
JOptionPane.showMessageDialog(null, "O programa vai ser encerrado!", "Failed to Login!", JOptionPane.ERROR_MESSAGE);
c.setNome(c.getNomeAdm()); // seta os nomes para o de administrador
c.setSenha(c.getSenhaAdm()); // set's
File f = new File(nomeArquivo); // cria um file com o nome do arquivo ja carregado
this.gravaObjeto(f, c); // grava o arquivo
fecha();
System.exit(0);
}
else{
c = new Cofrinho();
resp = new String(tf.getText());
c.setNome(resp);
resp = new String(pf.getPassword());
c.setSenha(resp);
status = this.validaLogin(tf.getText(), resp);
if(status == true){
FramePrincipal fp = new FramePrincipal(c);
this.setVisible(false);
this.fecha();
}
}
}
}
public void fecha(){
frame.dispose();
}
}
}
[/code]
Alguem tem alguma ideia ? tentei fazer, mais não deu certo.