2012-01-28 51 views
3

我正在为一款纸牌游戏制作JFrame。 我想在点击restartBtn时重新启动JFrame。谁能帮我?如何点击按钮时重新启动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) { 
    } 
    } 
} 

回答

6

您必须编码框架的重新启动。考虑开始时的游戏状态以及所有组件的状态。通常你会在某个地方有setup点,在某个阶段也有start。如果您可以设置这些,只需使用setupstart作为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. 
} 

所以去它的最好办法就是看你的游戏为几个阶段,如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; 
    } 
} 
+0

我设计一个游戏,玩家画5张牌,电脑绘制5张牌,并比较其卡是大(每回合1张比卡)。然后,我将显示玩家必须让玩家选择哪张牌与电脑相连的5张牌。所以我创建了2个按钮(重新启动)(退出),如果玩家想再次玩,他可以点击重新启动。 – Exorific 2012-01-28 06:41:24

+0

thx很多,但是,我试过你的方法,但我不能让它工作 – Exorific 2012-01-28 06:41:54

+0

伴侣,粘贴你的代码在这里:http://pastie.org/并发布链接。 – rtheunissen 2012-01-28 06:48:45

2

我根据自己的喜好修改了代码,希望通过按下重新启动按钮来重新启动游戏。

在您的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);方法,为您的重新启动按钮,写这个。

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

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

希望这可能会解决您的查询。

问候

+0

它适用于我的第二次尝试后发布的代码在构造函数的末尾,谢谢很多 – Exorific 2012-01-28 07:58:56

+0

@Exorific:呵呵,欢迎并保持微笑:-)问候 – 2012-01-28 08:00:02

+0

@Exorific:只是一个小建议,因为你保持跟踪计算机和人类的胜利数量,你最好在PlayGame类中声明这两个变量,而不是GameFrame类。这种方式即使重新开始游戏时,这些值仍将保持不变,并且您可以继续添加它们。 Regards – 2012-01-28 08:25:03

相关问题