I have a Java application that should display Gui1 then go to Gui 2 which I successful did , Then from Gui2 go back to Gui 1 which I have problems in doing.
So how can I go back to Gui 1 when I press a button in Gui2
Gui1 code
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class Gui1 extends JFrame {
private JButton btn=new JButton("Open Gui2");
public Gui1()
{
super(" GUI1");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
btn.addActionListener(new ButtonListener());
btn.setActionCommand("Open");
add(btn);
}
class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e)
{
String cmd = e.getActionCommand();
if(cmd.equals("Open"))
{
dispose();
new Gui2();
}
}
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable(){
public void run()
{
new Gui1().setVisible(true);
}
});
}
}
Gui2 code
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Gui2 extends JFrame
{
private JButton btn1= new JButton("Go Back to Gui 1");
public Gui2()
{
super("Another GUI");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(btn1);
setVisible(true);
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…