2013-05-01 65 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()