2013-05-27 126 views
0

我正在开发我的Java任务 - 扫雷游戏克隆。我有两个几乎完全相同的(只有文本标签和文本框架不同的)方法gameWon()和gameLost(),它们负责在游戏结束时显示“游戏赢了!”/“游戏迷失”窗口。我知道代码重复是不好的做法,所以我想优化它。问题是,我对OOP有点新鲜,我不确定如何去做。也许我可以将这些方法合并成某种方式,在不同的情况下采取不同的行为,或者继承可能会有用。我真的不知道,希望你们中的一些人能够帮助我一点。感谢您的回答。Java OOP优化代码

下面是这些方法的代码:

GAMEOVER

public static void gameOver() { 

     F1 = new JFrame("Game Over"); 
     F1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     F1.setSize(360, 120); 
     Container content = F1.getContentPane(); 
     content.setBackground(Color.white); 
     content.setLayout(new FlowLayout()); 

     JLabel textLabel = new JLabel("Sorry, you have lost this game! Better luck next time.",SwingConstants.CENTER); 
     textLabel.setPreferredSize(new Dimension(360, 40)); 
     content.add(textLabel, BorderLayout.CENTER); 

     JButton button = new JButton("Exit"); 
     button.addActionListener(new ActionListener() { 

      public void actionPerformed(ActionEvent e) 
      { 
       System.exit(0); 
      } 
     }); 
     content.add(button); 

     button = new JButton("Restart This Game"); 
     button.addActionListener(new ActionListener() { 

      public void actionPerformed(ActionEvent e) 
      { 
       F1.dispose(); 
       Board.doRepaint(); 
      } 
     });   
     content.add(button); 

     button = new JButton("Play Again"); 
     button.addActionListener(new ActionListener() { 

      public void actionPerformed(ActionEvent e) 
      { 
       F1.dispose(); 
       restartGame(); 
      } 
     });   
     content.add(button); 

     F1.setLocationRelativeTo(null); 
     F1.setVisible(true); 
    } 

gameWon

public static void gameWon() { 
    F1 = new JFrame("Game Won"); 
    F1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    F1.setSize(360, 120); 
    Container content = F1.getContentPane(); 
    content.setBackground(Color.white); 
    content.setLayout(new FlowLayout()); 

    JLabel textLabel = new JLabel("Congratulations, you have won the game!",SwingConstants.CENTER); 
    textLabel.setPreferredSize(new Dimension(360, 40)); 
    content.add(textLabel, BorderLayout.CENTER); 

    JButton button = new JButton("Exit"); 
    button.addActionListener(new ActionListener() { 

    public void actionPerformed(ActionEvent e) 
    { 
     System.exit(0); 
    } 
    }); 
    content.add(button); 

    button = new JButton("Restart This Game");  
    button.addActionListener(new ActionListener() { 

    public void actionPerformed(ActionEvent e) 
    { 
     F1.dispose(); 
     Board.doRepaint(); 
    } 
    });  
    content.add(button); 

    button = new JButton("Play Again"); 
    button.addActionListener(new ActionListener() { 

    public void actionPerformed(ActionEvent e) 
    { 
     F1.dispose(); 
     restartGame(); 
    } 
    });  
    content.add(button); 

    F1.setLocationRelativeTo(null); 
    F1.setVisible(true); 
} 
+0

你可以创建一个通用的'GameComplete'版本,它需要几个字符串。这将允许您重复使用相同的代码并显示不同的文本 – Craig

回答

0
public static void gameEnd(boolean hasWon) { 

    String title = hasWon ? "Game Won" : "Game Over"; 
    F1 = new JFrame(title); 
    F1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    F1.setSize(360, 120); 
    Container content = F1.getContentPane(); 
    content.setBackground(Color.white); 
    content.setLayout(new FlowLayout()); 

    String message = hasWon ? "Congratulations, you have won the game!" : 
      "Sorry, you have lost this game! Better luck next time."; 
    JLabel textLabel = new JLabel(message,SwingConstants.CENTER); 
    textLabel.setPreferredSize(new Dimension(360, 40)); 
    content.add(textLabel, BorderLayout.CENTER); 

    JButton button = new JButton("Exit"); 
    button.addActionListener(new ActionListener() { 

     public void actionPerformed(ActionEvent e) 
     { 
      System.exit(0); 
     } 
    }); 
    content.add(button); 

    button = new JButton("Restart This Game"); 
    button.addActionListener(new ActionListener() { 

     public void actionPerformed(ActionEvent e) 
     { 
      F1.dispose(); 
      Board.doRepaint(); 
     } 
    });   
    content.add(button); 

    button = new JButton("Play Again"); 
    button.addActionListener(new ActionListener() { 

     public void actionPerformed(ActionEvent e) 
     { 
      F1.dispose(); 
      restartGame(); 
     } 
    });   
    content.add(button); 

    F1.setLocationRelativeTo(null); 
    F1.setVisible(true); 
} 
1

你应该只有一个方法,调用它gameOver(....),乍一看,你只需要两个参数,titlemessage。然后,更改只有两行代码:

public static void gameOver(final String title, final String message) { 
    ..... 
    F1 = new JFrame(title); 
    ..... 
    JLabel textLabel = new JLabel(message ,SwingConstants.CENTER); 
} 

然后,而不是调用两种方法,调用相同的方法使用不同的参数:

gameOver("Game Won", "Congratulations, you have won the game!"); 
1

你可以做最简单的事情就是把字符串标题和消息作为参数,或采取任何表示,如果比赛已经赢了,在设定的字符串的方法的布尔测试一个布尔参数,喜欢的东西:

public static void gameOver(boolean won) { 
    .... 
    F1 = new JFrame(won?"Game Won":"Game Over"); 
    .... 
} 
0

看起来有点在代码上更接近,仅仅按照其他答案中的建议来传递布尔或String参数是不够的。你需要的工作是确定所有(这里:两个)方法的通用代码和不同的。在你的情况,我会想出这样的:

  • 标题
  • 消息
  • 按钮1消息
  • 按钮1监听
  • 按钮2消息
  • 按钮2监听

    public static void showTwoButtonMessage(String title, String message, 
    String button1Message, ActionListener listener1, 
    String button2Message, ActionListener listener2){ 
    //... 
    } 
    

因此,你有一个整洁的小方法,你可以重新使用来显示任何两个按钮窗口。