The assignment:
The problem I'm having is the inner class. Its being called as static?Create a GUI Java application that simulates the playing of the game "Rock, Paper, Scissors" between two players - the computer and a person. The application needs to use at least three buttons and one textfield. Create an inner class to respond to the pressing/clicking of a button and an anonymous inner class to respond to the closing of the GUI window.
Before starting to design your solution to this assignment, you may want to do a little background research by do a search on the Internet with key words of “rock paper scissors or "rps championship" or "rock paper scissors rules”. Don’t get carried away with creating a "world class" program before getting the basics working correctly.
Here's the source:
The errors I'm getting:Code:/** @version 04/12/2010 @author Kyle */ import javax.swing.*; import java.awt.event.*; import java.awt.*; import java.util.*; public class BuildGUI implements ActionListener { JPanel score, game, text; JFrame frame; JButton btnRock, btnPaper, btnScissors; JTextField status; JButton rock, paper, scissors, reset; JLabel lblPlayer, lblPlayerCount, lblComputer, lblComputerCount, lblTies, lblTiesCount; public static void main(String[] args){ new BuildGUI(); } public void actionPerformed(ActionEvent e){ class Rps{ //0 = rock //1 = paper //2 = scissors public int pcRoll(){ Random r = new Random(); int roll = r.nextInt(2); return roll; } // 0 = lose // 1 = win // 2 = tie // 3 = error public int compare(int player, int pc){ int value = 3; if (player == pc){ return 2; } switch (player){ case 0: if (pc == 1) { value = 0; } if (pc == 2) { value = 1; } break; case 1: if (pc == 0) { value = 1; } if (pc == 2){ value = 0; } break; case 2: if (pc == 0) { value = 0; } if (pc == 1) { value = 1; } break; } return value; } } Rps game = new Rps(); int pc, player, returned; String message = "IDK BRO"; if ( e.getSource() == rock ){ player = 0; } if ( e.getSource() == paper){ player = 1; } if ( e.getSource() == rock){{ player = 2; } pc = Rps.pcRoll(); returned = Rps.compare(player, pc); JOptionPane.showMessageDialog(null, "Player: " + player + " PC: " + pc); } } public BuildGUI(){ score = new JPanel(new FlowLayout()); game = new JPanel(new FlowLayout()); text = new JPanel(new FlowLayout()); frame = new JFrame(); frame.setLayout(new FlowLayout()); lblPlayer = new JLabel("Player: "); lblPlayerCount = new JLabel("0"); lblComputer = new JLabel("Computer: "); lblComputerCount = new JLabel("0"); lblTies = new JLabel("Ties: "); lblTiesCount = new JLabel("0"); score.add(lblPlayer); score.add(lblPlayerCount); score.add(lblComputer); score.add(lblComputerCount); score.add(lblTies); score.add(lblTiesCount); status = new JTextField("Make a Move!", 15); status.setEditable(false); text.add(status); rock = new JButton("Rock"); paper = new JButton("Paper"); scissors = new JButton("Scissors"); game.add(rock); game.add(paper); game.add(scissors); frame.add(score); frame.add(status); frame.add(game); rock.addActionListener( this ); paper.addActionListener( this ); scissors.addActionListener( this ); frame.setSize(250,155); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLocationRelativeTo(null); frame.setTitle("Rock Paper Scissors - Kyle"); frame.setVisible(true); } }
Any Ideas?BuildGUI.java:99: non-static method pcRoll() cannot be referenced from a static context
pc = Rps.pcRoll();
^
BuildGUI.java:100: non-static method compare(int,int) cannot be referenced from a static context
returned = Rps.compare(player, pc);
XI Wiki


