简体   繁体   English

如何在单击按钮时重新启动JFrame?

[英]How to restart JFrame when button clicked?

I am making a JFrame for a card game. 我正在为纸牌游戏制作JFrame I want to restart the JFrame when the restartBtn is clicked. 我想在单击restartBtn时重新启动JFrame Can anyone help me? 谁能帮我?

The PlayGame class is to launch the frame1 PlayGame类将启动frame1

public class PlayGame {

  public static void main(String[] args) {
        GameFrame frame1 = new GameFrame();

        // Set Icon
        Image icon = Toolkit.getDefaultToolkit().getImage("image/poker_icon.gif");
        frame1.setIconImage(icon);

        frame1.setVisible(true);
        frame1.setSize(600, 700);
        frame1.setTitle("Card Game");

        // Set to exit on close
        frame1.setDefaultCloseOperation(GameFrame.EXIT_ON_CLOSE);
  }
}

This is the GameFrame class is for the JFrame constructor. 这是GameFrame类用于JFrame构造函数。

public class GameFrame extends JFrame implements ActionListener {

  public JLabel restartLbl;  
  public JButton restartBtn

  public GameFrame() {

    restartLbl = new JLabel(restart);
    restartBtn = new JButton();

    restartBtn..addActionListener(this);
  }


  public void actionPerformed(ActionEvent e) {
    if (e.getSource() == restartBtn) {
    }
  }
}

You'll have to code the restart of the frame. 你必须编写帧的重启代码。 Consider the state of the game at the start and what all the components' states are. 考虑一开始的游戏状态以及所有组件的状态。 Generally you'd have a setup point somewhere and start as well at some stage. 一般来说,你就会有一个setup点的地方,并start在某些阶段也是如此。 If you can set those up it would be easy to just use setup and start as restart . 如果你可以设置这些了它会很容易只使用setupstartrestart

public void actionPerformed(ActionEvent e) {
    if (e.getSource() == restartBtn) {
        restart();
    }
  }

public void restart(){
    stop(); // if necessary
    setup(); // set everything to initial state
    start(); // start game
}

public void stop(){
    // stop any timers, threads, operations etc.
}

public void setup(){
    // set to initial state.
    // something like recreate the deck, 
    // clear hands and table, shuffle, and deal.
}

public void start(){
    // code to initiate the game.
}

So the best way to go about it is to see your game as several stages, and actions such as restart should be a combination of others. 因此,最好的方法是将您的游戏视为几个阶段, restart等操作应该是其他阶段的组合。 Without knowing anything about your actual game code (or plans for it), it's difficult to answer this specifically. 在不了解您的实际游戏代码(或计划)的情况下,很难具体回答这个问题。 But I hope that helps. 但我希望有所帮助。 :) :)

EDIT 编辑

This is a better way of going about generating / shuffling cards. 这是一种更好的生成/洗牌的方法。

   public class GenRandom {

   int []  cards = new int [GameFrame.NUMBER_OF_CARDS];

   public void generateCards() {
      for (int i = 0; i < cards.length; i++) { // for each index of the array...
         int card; // declare card
         do {
            card = (int) (Math.random() * 51) + 1; // random between 1 and 52
         } while (contains(card)); // regenerate random card if array already contains.
         cards[i] = card; // card is unique, so assign value to array index
      }
   }

   private boolean contains(int t){
      for(int i = 0; i < GameFrame.NUMBER_OF_CARDS; i++){ // for each index...
         if(cards[i] == t){
            return true; // if the generated card is already in the array..
         }
      }
      return false; // otherwise reached end, so return false.
   }

   public int [] getAllCards() {
      return cards;
   }
}

I had modified your code as per your liking, that you wanted to restart the game with the press of the Restart Button. 我根据你的喜好修改了你的代码,你想按下重启按钮重启游戏。

Here in your PlayGame class's main method, just change it to this. 在PlayGame类的主要方法中,只需将其更改为此即可。

public class PlayGame {

    public static void main(String[] args) {

        GameFrame frame1 = new GameFrame();            
    }

}

Whatever was inside this except for the first line, just cut and paste that to the GameFrame class's constructor, along with the previous things as it is like this : 除了第一行之外的内容,只需将其剪切并粘贴到GameFrame类的构造函数中,以及之前的内容,如下所示:

public GameFrame()
{
    // Your previous code as it is, and paste the below lines, after your already 
    // written code, at the end of the constructor.
    // Set Icon
    Image icon = Toolkit.getDefaultToolkit().getImage("image/poker_icon.gif");
    setIconImage(icon);
    setSize(600, 700);
    setTitle("Card Game");

    // Set to exit on close
    setDefaultCloseOperation(GameFrame.EXIT_ON_CLOSE);
    setVisible(true);
}

Now inside the actionPeformed(ActionEvent ae); 现在在actionPeformed(ActionEvent ae);里面actionPeformed(ActionEvent ae); method, for your Restart Button, write this. 方法,对于你的重启按钮,写这个。

if (e.getSource() == restartBtn) {

    this.dispose();
    new GameFrame();
}

Hopefully this might solve your query. 希望这可以解决您的查询。

Regards 问候

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM