Hei! Driver nå å koder en enkel versjon av "Stein, saks, papir"... Har laget denne uten GUI, og dette fungerer fint. Prøver nå å lage en versjon med en enkel GUI, men det var ikke like enkelt. Når jeg trykker på knappene er det ingenting som skjer... Mistenker at det er for snevert scope på koden som er tenkt å forandre "resultat-labelen". Har jobbet en god stund med det nå, håper det er noen her som kan rette pekefingeren på hva jeg har gjort feil...
Her er koden min:
Her er koden min:
Kode
import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.*; public class RockPaperScissorsPanel extends JPanel { Scanner scan = new Scanner (System.in); Random rand = new Random(); String choices[] = {"Rock", "Paper", "Scissors"}; // creates the choices int numberOfWins = 0, numberOfLosses = 0, numberOfTies = 0; // to count the users int computerChoice, userChoice; private JButton rock, paper, scissors; private JLabel ties, wins, losses, result; //sets up the panel with buttons for choices and an ok GUI public RockPaperScissorsPanel() { rock = new JButton ("Rock"); paper = new JButton ("Paper"); scissors = new JButton ("Scissors"); JPanel buttonPanel, resultPanel, endPanel; ButtonListener listener = new ButtonListener(); rock.addActionListener (listener); paper.addActionListener (listener); scissors.addActionListener (listener); buttonPanel = new JPanel (); buttonPanel.setPreferredSize (new Dimension (500, 40)); buttonPanel.setBackground (Color.black); buttonPanel.add (rock); buttonPanel.add (paper); buttonPanel.add (scissors); ties = new JLabel("Ties: " + numberOfTies); wins = new JLabel("Wins: " + numberOfWins); losses = new JLabel("Losses: " + numberOfLosses); endPanel = new JPanel(); endPanel.setPreferredSize (new Dimension(500, 40)); result = new JLabel (""); resultPanel = new JPanel(); resultPanel.setPreferredSize (new Dimension(500, 40)); resultPanel.add (ties); resultPanel.add (wins); resultPanel.add (losses); setPreferredSize (new Dimension (500, 120)); setBackground (Color.cyan); add (buttonPanel); add (endPanel); add (resultPanel); } public class ButtonListener implements ActionListener { public void actionPerformed (ActionEvent event) { if (event.getSource() == rock) { userChoice = 0; // the computers choice computerChoice = rand.nextInt(3); if (computerChoice == 0) { result = new JLabel("The user chose " + choices[0] + " and the computer chose " + choices[2] + ", so the user wins."); numberOfWins = numberOfWins + 1;} if (computerChoice == 1) { result = new JLabel("The user chose " + choices[0] + " and the computer chose " + choices[1] + ", so the computer wins."); numberOfLosses = numberOfLosses + 1;} if (computerChoice == 2) { result = new JLabel("The user chose " + choices[0] + " and the computer chose " + choices[0] + ", so the it is a tie."); numberOfTies = numberOfTies+ 1;} } if (event.getSource() == paper){ userChoice = 1; // the computers choice computerChoice = rand.nextInt(3); if (computerChoice == 0) { result = new JLabel("The user chose " + choices[1] + " and the computer chose " + choices[0] + ", so the user wins."); numberOfWins = numberOfWins + 1;} if (computerChoice == 1) { result = new JLabel("The user chose " + choices[1] + " and the computer chose " + choices[2] + ", so the computer wins."); numberOfLosses = numberOfLosses+ 1;} if (computerChoice == 2) { result = new JLabel("The user chose " + choices[1] + " and the computer chose " + choices[1] + ", so the it is a tie."); numberOfTies = numberOfTies + 1;} } if (event.getSource() == scissors){ userChoice = 2; if (computerChoice == 0) { result = new JLabel("The user chose " + choices[2] + " and the computer chose " + choices[1] + ", so the user wins."); numberOfWins = numberOfWins + 1;} if (computerChoice == 1) { result = new JLabel("The user chose " + choices[2] + " and the computer chose " + choices[0] + ", so the computer wins."); numberOfLosses = numberOfLosses + 1;} if (computerChoice == 2) { result = new JLabel("The user chose " + choices[2] + " and the computer chose " + choices[2] + ", so the it is a tie."); numberOfTies = numberOfTies + 1;} } } } }
Kode
import java.util.*; import javax.swing.JFrame; public class RockPaperScissors { public static void main (String[] args) { //creates the main frame JFrame frame = new JFrame ("Rock, Paper, Scissors"); frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); //creates the games panel RockPaperScissorsPanel panel = new RockPaperScissorsPanel(); frame.getContentPane().add (panel); frame.pack(); frame.setVisible(true); } }