为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