首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java OOP优化代码

Java OOP优化代码
EN

Stack Overflow用户
提问于 2013-05-27 22:26:20
回答 4查看 304关注 0票数 0

我正在做我的Java任务-扫雷游戏克隆。我有两个几乎相同的(只有文本标签和文本帧不同)的方法gameWon()和gameLost(),它们负责在游戏结束时显示“游戏胜利”/“游戏丢失”窗口。我知道代码复制是错误的做法,所以我想优化它。问题是我对OOP有点陌生,我也不知道该怎么做。也许我可以将这些方法合并成一个,以便在不同的情况下采取不同的行动,或者继承可能是有用的。我真的不知道,希望你们中的一些人能帮我一下。谢谢你的回答。

下面是这些方法的代码:

gameOver

代码语言:javascript
复制
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

代码语言:javascript
复制
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); 
}
EN

回答 4

Stack Overflow用户

发布于 2013-05-27 22:33:08

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

代码语言:javascript
复制
public static void gameOver(final String title, final String message) {
  .....
  F1 = new JFrame(title);
  .....
  JLabel textLabel = new JLabel(message ,SwingConstants.CENTER);
}

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

代码语言:javascript
复制
gameOver("Game Won", "Congratulations, you have won the game!");
票数 1
EN

Stack Overflow用户

发布于 2013-05-27 22:33:34

您可以做的最简单的事情是将标题和消息的字符串作为参数,或者接受一个布尔参数,该参数表示游戏是否获胜,并在设置字符串的方法中进行布尔测试,如下所示:

代码语言:javascript
复制
public static void gameOver(boolean won) {
    ....
    F1 = new JFrame(won?"Game Won":"Game Over");
    ....
}
票数 1
EN

Stack Overflow用户

发布于 2013-05-27 22:32:00

代码语言:javascript
复制
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); 
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16781299

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档