繁体   English   中英

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

[英]How to restart JFrame when button clicked?

我正在为纸牌游戏制作JFrame 我想在单击restartBtn时重新启动JFrame 谁能帮我?

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

这是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) {
    }
  }
}

你必须编写帧的重启代码。 考虑一开始的游戏状态以及所有组件的状态。 一般来说,你就会有一个setup点的地方,并start在某些阶段也是如此。 如果你可以设置这些了它会很容易只使用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.
}

因此,最好的方法是将您的游戏视为几个阶段, restart等操作应该是其他阶段的组合。 在不了解您的实际游戏代码(或计划)的情况下,很难具体回答这个问题。 但我希望有所帮助。 :)

编辑

这是一种更好的生成/洗牌的方法。

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

我根据你的喜好修改了你的代码,你想按下重启按钮重启游戏。

在PlayGame类的主要方法中,只需将其更改为此即可。

public class PlayGame {

    public static void main(String[] args) {

        GameFrame frame1 = new GameFrame();            
    }

}

除了第一行之外的内容,只需将其剪切并粘贴到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);
}

现在在actionPeformed(ActionEvent ae);里面actionPeformed(ActionEvent ae); 方法,对于你的重启按钮,写这个。

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

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

希望这可以解决您的查询。

问候

暂无
暂无

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

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