0

I am writing a program and am unable to figure this out but I have a JButton called nextDay and I need it set up so once I click it, it switches to the JFrame day2 as the program starts out on day1. Any help is appreciated. Here is my code. I have a separate Main method which I wont include as it shouldn't need to be changed

import java.awt.Container;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class SoldierSimTest extends JFrame {

    private final JButton decision1;
    private final JButton decision2;
    private final JButton decision3;
    private final JButton decision4;
    private final JTextField situation;
    private JFrame day1;
    private JFrame day2;
    private JFrame day3;
    private JFrame day4;
    private JFrame day5;
    private JFrame day6;
    private JFrame day7;
    private final JButton nextDay;
    private final JButton exitGame;
    private final JButton newGame;

    public SoldierSimTest() {
        decision1 = new JButton("Storm it");
        decision2 = new JButton("Sneak around the flank");
        decision3 = new JButton("Sneak up and grenade spam 'em");
        decision4 = new JButton("Just dont");
        situation = new JTextField("You and your squad are ordered to take         
an enemy fort. How will you do so?");
        situation.setEditable(false);
        nextDay = new JButton("Next Day");
        exitGame = new JButton("Exit Game");
        newGame = new JButton("New Game");

        JPanel decisionsPanel = new JPanel();
        decisionsPanel.setLayout(new GridLayout(2, 2));
        decisionsPanel.add(decision1);
        decisionsPanel.add(decision2);
        decisionsPanel.add(decision3);
        decisionsPanel.add(decision4);

        JPanel optionsPanel = new JPanel();
        optionsPanel.setLayout(new GridLayout(1, 3));
        optionsPanel.add(newGame);
        optionsPanel.add(exitGame);
        optionsPanel.add(nextDay);

        JPanel situationsPanel = new JPanel();
        optionsPanel.setLayout(new GridLayout(1, 1));
        situationsPanel.add(situation);

        Container contentPane = getContentPane();
        contentPane.add(decisionsPanel, "South");
        contentPane.add(optionsPanel, "North");
        contentPane.add(situationsPanel, "Center");

    }
}
  • 3
    [How to use CardLayout](https://docs.oracle.com/javase/tutorial/uiswing/layout/card.html) – MadProgrammer Apr 24 '18 at 03:18
  • While @MadProgrammer has already suggested the approach that seems best for this situation, I'll offer some more general advice (with lots of other alternatives): See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) – Andrew Thompson Apr 24 '18 at 04:57
  • *"I have a separate Main method which I wont include as it shouldn't need to be changed"* Sure, but one advantage of posting a [mcve] is the ease with which others can test it. An MCVE should be 'ready to go' **exactly** as copy/pasted. No resolving imports, no adding a `main(..)` etc. – Andrew Thompson Apr 24 '18 at 05:00

2 Answers2

0

You can use card layout to switch panels. I used Jpanels as it seems to be the best option to use. For this example I used 2 panels.

public class SoldierSimTest extends JFrame
{

  private final JButton decision1;   
  private final JButton decision2;    
  private final JButton decision3;
  private final JButton decision4;   
  private final JTextField situation;  
  private JPanel day1Panel = new JPanel();  
  private JPanel day2Panel = new JPanel();  
  private final JButton nextDay;  
  private final JButton exitGame;   
  private final JButton newGame;    
  final static String DAY1 = "Day1";    
  final static String DAY2 = "Day2";

  public SoldierSimTest()
  {

    decision1 = new JButton("Storm it");
    decision2 = new JButton("Sneak around the flank");
    decision3 = new JButton("Sneak up and grenade spam 'em");
    decision4 = new JButton("Just dont");
    situation = new JTextField("You and your squad are ordered to take an enemy fort. How will you do so?");
    situation.setEditable(false);

    JPanel decisionsPanel = new JPanel();
    decisionsPanel.setLayout(new GridLayout(2, 2));
    decisionsPanel.add(decision1);
    decisionsPanel.add(decision2);
    decisionsPanel.add(decision3);
    decisionsPanel.add(decision4);

    JPanel situationsPanel = new JPanel();
    situationsPanel.add(situation);

    day1Panel.setLayout(new GridLayout(2, 1));
    day1Panel.add(situationsPanel);
    day1Panel.add(decisionsPanel);

    JPanel cards = new JPanel(new CardLayout());
    cards.add(day1Panel, DAY1);
    cards.add(day2Panel, DAY2);

    nextDay = new JButton("Next Day");
    exitGame = new JButton("Exit Game");
    newGame = new JButton("New Game");

    nextDay.addActionListener(new ActionListener()
    {

      @Override
      public void actionPerformed(ActionEvent e)
      {
        CardLayout cl = (CardLayout) (cards.getLayout());
        cl.show(cards, DAY2);

      }
    });

    JPanel optionsPanel = new JPanel();
    optionsPanel.setLayout(new GridLayout(1, 3));
    optionsPanel.add(newGame);
    optionsPanel.add(exitGame);
    optionsPanel.add(nextDay);

    Container contentPane = getContentPane();
    contentPane.add(optionsPanel, "North");
    contentPane.add(cards, "Center");
  }

}
pavithraCS
  • 709
  • 6
  • 23
0

The best way to do this if you don't necessarily need a new JFrame is the way @pavithraCS described. What I want to add is that normally, when you want a new "window" with different components to appear, you don't use a new JFrame because that opens a new window. Instead, using a new JPanel is more useful because you can stack them without having to switch to another window.

I hope this helps for the future.

Cr3ative
  • 21
  • 3