如何通过单击Java中的JButton来切换JFrame?

时间:2018-04-24 03:16:50

标签: java swing jframe jbutton

我正在编写一个程序,我无法解决这个问题,但是我有一个名为nextDay的JButton,我需要它设置所以一旦我点击它,它会在第1天开始时切换到JFrame day2。任何帮助表示赞赏。 这是我的代码。我有一个单独的Main方法,我不会包含它,因为它不需要更改

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");

    }
}

2 个答案:

答案 0 :(得分:0)

您可以使用卡片布局切换面板。我使用Jpanels,因为它似乎是最好的选择。在这个例子中,我使用了2个面板。

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");
  }

}

答案 1 :(得分:0)

如果您不一定需要新的JFrame,最好的方法是@pavithraCS描述的方式。我想要添加的是通常,当你想要一个新的"窗口"如果要显示不同的组件,则不要使用新的JFrame,因为这会打开一个新窗口。相反,使用新的JPanel更有用,因为您可以堆叠它们而无需切换到另一个窗口。

我希望这有助于未来。