簡體   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