爲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);
}
明白了一個版本的這個工作。謝謝 - – phamousphil 2013-05-02 00:29:57
@phamousphil:不客氣!感謝您回覆我們的解決方案! – 2013-05-02 03:00:49