Preciso de fazer uma perguntinha ao final de minha aplicação do tipo… Deseja executar novamente?
Fiz um laço para execução do meu frame e controlo ele no WindowListener.
Oque preciso fazer para fazer aparecer o frame ao clicar em “sim”?
Fiz um codigo simples com minha duvida para que possam me orientar.
Desde ja agradeço a ajuda!
package Lixo;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class FechaJanela extends JFrame{
private JLabel Janela = new JLabel("Programa Executado");
public FechaJanela()
{
super("Teste para reabrir uma janela Novamente");
getContentPane().setLayout(null);
setSize(500,500);
Janela.setVisible(true);
Janela.setBounds(10,10,100,10);
Janela.setEnabled(true);
getContentPane().add(Janela);
getContentPane().repaint();
}
}
//Classe principal
package Lixo;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class ChamaJanela {
static TrataWin trataWin = new TrataWin();
public static boolean executa = true;
public static boolean rodaDeNovo = true;
public static void main(String Args[])
{
FechaJanela fecha;
while(executa )
{
if(rodaDeNovo)
{
rodaDeNovo = false;
fecha = new FechaJanela();
fecha.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fecha.setVisible(true);
fecha.addWindowListener(trataWin);
}
}
System.out.println("Saiu do Laço");
System.exit(0);
}
public static class TrataWin implements WindowListener
{
public void windowOpened(WindowEvent arg0) {
System.out.println("Metodo 1");
}
public void windowClosing(WindowEvent arg0) {
System.out.println("Metodo 2");
if(JOptionPane.showConfirmDialog(null,"Deseja executar novamente")==0)
{
rodaDeNovo = true;
}
else
{
executa = false;
}
}
public void windowClosed(WindowEvent arg0) {
System.out.println("Metodo 3");
}
public void windowIconified(WindowEvent arg0) {
System.out.println("Metodo 4");
}
public void windowDeiconified(WindowEvent arg0) {
System.out.println("Metodo 5");
}
public void windowActivated(WindowEvent arg0) {
System.out.println("Metodo 6");
}
public void windowDeactivated(WindowEvent arg0) {
System.out.println("Metodo 7");
}
}
}