简体   繁体   中英

How to restart JFrame when button clicked?

I am making a JFrame for a card game. I want to restart the JFrame when the restartBtn is clicked. Can anyone help me?

The PlayGame class is to launch the 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.

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. If you can set those up it would be easy to just use setup and start as restart .

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. 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.

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 :

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); method, for your Restart Button, write this.

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

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

Hopefully this might solve your query.

Regards

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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