Hello channi3 and welcome to the forums.
Take a look at this code example and compile it.
There is a main JFrame with a button on it. When this button is clicked, JFrame1 closes and opens JFrame2.
Its just a matter of invoking the run method for each form when an action is performed and setting the visibility of the previous form to false.
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Channi3 {
public static JFrame frame1 = new JFrame("Java Frame1");
public static JFrame frame2 = new JFrame("Java Frame2");
//Form1
private static void createAndShowGUI1() {
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton button = new JButton("HI! IM JFRAME1 - CLICK ME TO OPEN FRAME 2");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
[B]frame1.setVisible(false);[/B]
System.out.println("Opening JFrame 2");
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
[B]createAndShowGUI2();[/B]
}
});
}
});
frame1.getContentPane().add(button);
frame1.pack();
frame1.setVisible(true);
}
//Form2
private static void createAndShowGUI2() {
frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton button = new JButton("HI! IM JFRAME2 - CLICK ME TO OPEN FRAME 1");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
[B]frame2.setVisible(false);[/B]
System.out.println("Opening JFrame 1");
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
[B]createAndShowGUI1();[/B]
}
});
}
});
frame2.getContentPane().add(button);
frame2.pack();
frame2.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI1();
}
});
}
}