I have tried to succesfully merge my Rock Paper Scissors project with the gui I made for it.
Summary of my code.
--I click start, and it makesvisible 3 boxes, rock paper scissors.
--I start a for loop which loops 3 times starting at these boxes being visible.
--It goes down various methods of checking the button(The button in its listener turns a boolean true)
My Problem: It doesn't act like the traditional for loop im used to. It shoots down the loops and still executes stuff not even in the for loop. It's like the for loop is instantanious. It's not like with Scanner, and it had to wait for information to continue it's like its not expecting user to put a button then is still executing below.
Here is the code for reference, I threw it together in rather of minutes so don't judge it to perfection.
[/B]package rpsgui; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.IOException; import java.util.Random; import java.util.Scanner; import javax.swing.*; import javax.swing.Icon; import javax.swing.JDialog; import javax.swing.JFrame; public class RPSgui { JButton plr; JButton rock; JButton paper; JButton sci; JButton scei; static int tie=0; static int wins=0; static int lose=0; static boolean rck=false; static boolean papr=false; static boolean scissor=false; static boolean prock=false; static boolean ppaper=false; static boolean pscissors=false; public static void main(String[] args) { // Creates JFrame. JFrame jf = new JFrame("RPS - @Xyn") ; jf.setSize(600, 600); jf.setLocation(600,300); jf.setVisible(true); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jf.setLayout(new GridLayout(7,44)); // Create Panel JPanel jp = new JPanel(); // Creates Play JButton. final JButton plr = new JButton("PLAY NOW"); final JButton rock = new JButton("Rock"); final JButton paper=new JButton("Paper"); final JButton scei=new JButton("Scissors"); // Set things plr.setSize(200, 200); plr.setVisible(true); rock.setSize(300,300); rock.setVisible(false); paper.setSize(300,300); paper.setVisible(false); scei.setSize(300,300); scei.setVisible(false); //set more things // Add things to JFrame. jf.add(plr); jf.add(jp); jf.add(rock); jf.add(paper); jf.add(scei); // Action Listener plr.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { try{ System.out.print("Welcome to Tyler Rock, Paper, Scissors =)"); plr.setVisible(false); rock.setVisible(true); paper.setVisible(true); scei.setVisible(true); System.out.print("What do you think it is?"); } catch(Exception l){ System.out.println("Some error "); } // Calling the Introduction Method. System.out.println("Welcome to your most advanced rock, paper scissors game yet."); System.out.println(""); System.out.print(">>"); for (int x=0;x<3;x++){ // Creating booleans, Variables. ECT. rock.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { try{ System.out.println("You have pick rock."); prock=true; } catch(Exception l){ System.out.println("Some error "); } } }); paper.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { try{ System.out.println("YOU PICKED PAPER"); ppaper=true; } catch(Exception l){ System.out.println("Some error "); } } }); scei.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { try{ System.out.println("YOU PICKED SCISSORS"); pscissors=true; } catch(Exception l){ System.out.println("Some error "); } } }); //Creating my objects I guess. Random rnd = new Random(); // My Random Int Out of 3 choices. int cp = rnd.nextInt(3); //Sets boolean of the computers choice. if(cp>1){ scissor=true; } if(cp>0&&cp<2){ papr=true; } if(cp>-1&&cp<1){ rck=true; } if (ppaper){ if(rck){ System.out.println("Computer Picked Rock, you picked Rock. You TIE!"); System.out.print(">>"); tie++; } if(papr){ System.out.println("Computer Picked Paper, you picked Rock. You LOSE!"); System.out.print(">>"); lose++; } if(scissor){ System.out.println("Computer Picked Scissors, you picked Rock. You WIN!"); System.out.print(">>"); wins++; } } if (prock){ if(rck){ System.out.println("Computer Picked Rock, you picked Paper. You WIN!"); System.out.print(">>"); wins++; } if(papr){ System.out.println("Computer Picked Paper, you picked Paper. You TIE!"); System.out.print(">>"); tie++; } if(scissor){ System.out.println("Computer Picked Scissors, you picked Paper. You LOSE!"); System.out.print(">>"); lose++; } } if (pscissors){ if(rck){ System.out.println("Computer Picked Rock, you picked Scissors. You LOSE!"); System.out.print(">>"); lose++; } if(papr){ System.out.println("Computer Picked Paper, you picked Scissors. You WIN!"); System.out.print(">>"); wins++; } if(scissor){ System.out.println("Computer Picked Scissors, you picked Scissors. You TIE!"); System.out.print(">>"); tie++; } } } } }); System.out.print("You had "+wins+" wins and "+lose+" losses and "+tie+" ties."); }}
Thanks guys =)