2013-05-01 36 views
1

为Java中的类项目构建简单的Rock Paper Scissors GUI游戏。从Java中的单独类文件调用方法

试图去用MVC方法,我把resultsGUI()下面的代码从我的“Controller”类移动到我的“View”类。

我试图在Controller类中创建View类的一个实例,并调用这样的方法:view.resultsGUI();但在我的编译器中抛出异常错误。

我该如何调用resultsGUI方法,该方法驻留在我的View类中,以便在chooseWinner()方法(也在下文中)的底部执行,因为代码是chooseWinner()的一部分?

我是新手,非常感谢您的帮助。

chooseWinner以下方法:

public static void chooseWinner(int x) { 
    playerChoice = x; 

    String winningCombo = "" + Math.min(compChoice, playerChoice) 
      + Math.max(compChoice, playerChoice); 

    switch (Integer.parseInt(winningCombo)) { 
     case 1: 
      text = "Paper wins!"; 
      if (playerChoice == 2) { 
       playerWon = 1; 
      } 
      break; 
     case 2: 
      text = "Rock wins!"; 
      if (playerChoice == 1) { 
       playerWon = 1; 
      } 
      break; 
     case 3: 
      text = "Scissors wins!"; 
      if (playerChoice == 3) { 
       playerWon = 1; 
      } 
      break; 

    } 

    if (playerWon == 1) { 
     text1 = "Congrats, you win!"; 
     playerWon = 0; 
     win = win + 1; 
     total = total + 1; 
    } else if (playerWon == 2) { 
     text1 = "It's a tie!"; 
     playerWon = 0; 
    } else { 
     text1 = "Computer wins!"; 
     total = total + 1; 
    } 




} 
下面

resultsGUI方法: 公共无效resultsGUI(){ 的JFrame rFrame =新的JFrame( “比赛结果”); 容器面板= rFrame.getContentPane(); panel.setLayout(null);

JLabel l0 = new JLabel(controller.text1 + controller.text); 
    l0.setBounds(75, 10, 300, 35); 
    panel.add(l0); 


    //show the result in a new splash screen 

    JLabel l1 = new JLabel("Human's Choice"); 
    l1.setBounds(40, 35, 150, 35); 
    panel.add(l1); 

    JLabel l2 = new JLabel("Computer's Choice"); 
    l2.setBounds(215, 35, 150, 35); 
    panel.add(l2); 

    JLabel l3 = new JLabel(new ImageIcon(System.getProperty("user.dir") + "/image/" + (controller.playerChoice - 1) + ".jpg")); 
    l3.setBounds(10, 100, 170, 60); 
    panel.add(l3); 

    JLabel l4 = new JLabel(new ImageIcon(System.getProperty("user.dir") + "/image/" + (controller.compChoice - 1) + ".jpg")); 
    l4.setBounds(200, 100, 170, 60); 
    panel.add(l4); 

    JLabel l5 = new JLabel("Win/Loss rate: " + controller.win + "/" + controller.total); 
    l5.setBounds(125, 25, 150, 350); 
    panel.add(l5); 

    JLabel l6 = new JLabel("Tie: " + controller.tie); 
    l6.setBounds(125, 30, 125, 370); 
    panel.add(l6); 

    rFrame.setSize(400, 270); 
    rFrame.setVisible(true); 
} 

回答

3

您的控制应该有一个视图和模型的实例,而不仅仅是的任何视图和模型实例,而是活动的当前可视化视图和当前使用的模型。像这样的东西可能是你的类的主要方法:

public static void main(String[] args) { 
    View view = new View(); 
    Model model = new Model(); 
    Control control = new Control(view, model); 

    // start the GUI up 
} 

在控制类,你应该使用构造函数的参数设置类字段:

public class Control { 
    private View view; 
    private Model model; 

    public Control(View view, Model model) { 
    this.view = view; 
    this.model = model; 
    } 

    // now your control can call model and view methods. 
    // .... 
} 
+0

明白了一个版本的这个工作。谢谢 - – phamousphil 2013-05-02 00:29:57

+0

@phamousphil:不客气!感谢您回复我们的解决方案! – 2013-05-02 03:00:49

0

您需要编写与其他类的Game类。所以,你得到这样的:

class Game 
{  
    View view = new View(); 
    public void foo() 
    { 
     view.getResults(); 
    }  
} 

如果你不想实例化View类,你可以标记getResults功能静态和引用它是这样的:

View.getResults() 
相关问题