Sono un neofita con Java, e ho riscontrato un problema. Vorrei che alla pressione del JButton "retry" venisse riavviata la classe principale. Ho cercato in giro, e su StackOverflow ho trovato varie soluzioni, ma che non riesco ad implementare. Posto di seguito il codice:
MAIN
codice:import java.io.*; import java.util.*; import javax.swing.*; import java.awt.event.*; import java.awt.*; public class prova_main extends JFrame implements ActionListener{ JTextField text=new JTextField(); JButton button=new JButton("Invio"); public prova_main(){ super("prova"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); setLayout(null); setExtendedState(JFrame.MAXIMIZED_BOTH); text.setSize(100,50); text.setLocation(400,300); add(text).setVisible(true); button.setSize(100,50); button.setLocation(550,300); button.addActionListener(this); add(button).setVisible(true); } public void actionPerformed(ActionEvent e){ String x=text.getText(); int y=Integer.parseInt(x); if(y%2==0){ }else{ prova a=new prova(); a.setVisible(true); } } public static void main(String[] args){ new prova_main(); } }
CLASSE PROVA
codice:import javax.swing.*; import java.awt.event.*; import java.awt.*; import java.io.*; import java.util.*; public class prova extends JFrame implements ActionListener{ JButton exit=new JButton("ESCI"); JButton retry=new JButton("RIPROVA"); public prova(){ super("prova"); setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); setLayout(null); setSize(300,200); setLocation(660,300); setVisible(true); retry.setSize(100,50); retry.setLocation(30,100); retry.addActionListener(this); exit.setSize(100,50); exit.setLocation(140,100); exit.addActionListener(this); add(exit).setVisible(true); add(retry).setVisible(true); } public void actionPerformed(ActionEvent e){ if(e.getSource() == exit) System.exit(0); else if(e.getSource() == retry){ } } public static void main(String[] args){ new prova(); } }

Rispondi quotando
