0

I am making a GUI for a game, and I have a splash screen which I created using JFrame. I have a button which says play and what I want after that to happen is when I press play, I want to switch to another JFrame, which is going to have different stuff in it. However, I do not want the window to close and open another one, I want it to just switch from one frame to another frame.

I have no experience on GUI, if you have any information that would help it would be greatly appreciated.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433

1 Answers1

0

Here is an example:

import javax.swing.JFrame;
import javax.swing.JButton;


public class Test {
  public static void main(String[] args) {
    JFrame f = new JFrame();
    f.setBounds(10, 10, 500, 200);
    JButton b1 = new JButton("b1");
    b1.addActionListener((c) -> {
      buttonPressed(f);
    });
    f.setContentPane(b1);
    f.setVisible(true);
  }

  private static void buttonPressed(JFrame f) {
    JButton b2 = new JButton("b2");
    f.setContentPane(b2);
    f.revalidate();
  }

When b1 is pressed, the content pane for the frame is replaced with a new button. the revalidate() call is needed in order for the UI to refresh after the change.

Roberto Attias
  • 1,883
  • 1
  • 11
  • 21