Results 1 to 6 of 6
  1. #1
    Snowball is offline Member
    Join Date
    Jun 2011
    Posts
    5
    Rep Power
    0

    Question Help! (Java GUI - Rock, Paper, Scissors)

    So, I am trying to make a Java GUI game of Rock, Paper, Scissors. However, whenever I run the program, the only problem I experience is when I click on the paper, and scissor buttons. A box is supposed to pop up saying whether you've won, lost, or tied. For some odd reason, that only works when I click on the rock button. Here is the code:

    Java Code:
    import java.awt.event.*;
    import javax.swing.*;
    import java.awt.*;
    import static java.lang.Math.*;
    
    public class Final extends JFrame
    {
    		private JLabel javaL,haha;
    		private JButton colorB;
    		private Container pane;
    		private what stopHandler;
    		private hi beginHandler;
    		private umm middleHandler;
    		private ButtonGroup ColorSelectBGroup;
    		private JRadioButton one,two,three;
    		private static final int WIDTH = 1000;
    		private static final int HEIGHT = 500;
    		
    		public Final()
    		{
    			stopHandler = new what();
    			haha = new JLabel("   Please pick either: Rock, Paper, or Scissors.");
    			one = new JRadioButton("Rock");
    			two = new JRadioButton("Paper");
    			three = new JRadioButton("Scissors");
    			colorB = new JButton("Random button here just for decoration.~ :D");
    			JLabel javaL = new JLabel("RANDOM LABEL THAT YOU ARE READING NOW.", SwingConstants.CENTER);
    			setTitle("Welcome to: Java Programming - Rock, Paper, Scissors (Tr0ll style)");
    			pane = getContentPane();
    			pane.setLayout(new GridLayout(2,1));
    			pane.add(haha);
    			pane.add(javaL);
    			pane.add(colorB);
    			ColorSelectBGroup = new ButtonGroup();
    			ColorSelectBGroup.add(one);
    			ColorSelectBGroup.add(two);
    			ColorSelectBGroup.add(three);
    			pane.add(one);
    			pane.add(two);
    			pane.add(three);
    			one.addActionListener(stopHandler);
    			two.addActionListener(beginHandler);
    			three.addActionListener(middleHandler);
    			setSize(WIDTH, HEIGHT);
    			setVisible(true);
    			setDefaultCloseOperation(EXIT_ON_CLOSE);
    		}
    		private class what implements ActionListener
    		{
    			public void actionPerformed(ActionEvent e)
    			{
    				int rock = 1;
    				int paper = 2;
    				int scissors = 3;
    				int random;
    				random = (int)(Math.random() * 3) + 1;
    				if (random == 2)
    				{
    					JOptionPane.showMessageDialog(null, "You've lost. Better luck next time. N00B.");
    					JOptionPane.showMessageDialog(null, "Also, guess wut? You've lost the game. TROLOLOLO!");
    				}
    				if (random == 1)
    				{
    					JOptionPane.showMessageDialog(null, "It's a tie. It's your lucky day.");
    				}
    				if (random == 3)
    				{
    					JOptionPane.showMessageDialog(null, "You've won. This time. But I promise that I will win eventually.");
    				}
    			}
    		}
    		private class hi implements ActionListener
    		{
    			public void actionPerformed(ActionEvent e)
    			{
    				int rock = 1;
    				int paper = 2;
    				int scissors = 3;
    				int random;
    				random = (int)(Math.random() * 3) + 1;
    				if (random == 3)
    				{
    					JOptionPane.showMessageDialog(null, "You've lost. Better luck next time. N00B.");
    					JOptionPane.showMessageDialog(null, "Also, guess wut? You've lost the game.");
    				}
    				if (random == 2)
    				{
    					JOptionPane.showMessageDialog(null, "It's a tie. It's your lucky day.");
    				}
    				if (random == 1)
    				{
    					JOptionPane.showMessageDialog(null, "You've won. This time. But I promise that I will win eventually.");
    				}
    			}
    		}
    		private class umm implements ActionListener
    		{
    			public void actionPerformed(ActionEvent e)
    			{
    				int rock = 1;
    				int paper = 2;
    				int scissors = 3;
    				int random;
    				random = (int)(Math.random() * 3) + 1;
    				if (random == 1)
    				{
    					JOptionPane.showMessageDialog(null, "You've lost. Better luck next time. N00B.");
    					JOptionPane.showMessageDialog(null, "Also, guess wut? You've lost the game.");
    				}
    				if (random == 3)
    				{
    					JOptionPane.showMessageDialog(null, "It's a tie. It's your lucky day.");
    				}
    				if (random == 2)
    				{
    					JOptionPane.showMessageDialog(null, "You've won. This time. But I promise that I will win eventually.");
    				}
    			}
    		}
    		public static void main(String[] args)
    		{
    			Final demoObject = new Final();
    		}
    }
    Can someone help me fix this problem?
    Last edited by DarrylBurke; 06-15-2011 at 09:02 PM. Reason: Added code tags

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,240
    Rep Power
    19

    Default

    Moved here from Reviews/Advertising

    db

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,416
    Rep Power
    25

    Default

    Where do you create the listeners that are to be called?

  4. #4
    Zeramat is offline Member
    Join Date
    Jun 2011
    Posts
    5
    Rep Power
    0

    Default

    You create listeners for all three buttons, but you only initialized the listener for the first raido button. Attached is a extract from your code with the change to initialize the other two listeners.

    Java Code:
    		public Final()
    		{
    			stopHandler = new what();
    /** --- add lines to end block --- **/
    			beginHandler = new hi();
    			middleHandler = new umm();
    /** -- end block for adding lines --- **/
    			haha = new JLabel("   Please pick either: Rock, Paper, or Scissors.");
    			one = new JRadioButton("Rock");

  5. #5
    Snowball is offline Member
    Join Date
    Jun 2011
    Posts
    5
    Rep Power
    0

    Default

    I see what you mean. Thank you so much for your help!

  6. #6
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Shall I suggest using more descriptive variables and method names?

    P.S. You troll
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

Similar Threads

  1. Replies: 3
    Last Post: 02-17-2011, 02:42 AM
  2. Rock, paper, scissors
    By StevenF in forum New To Java
    Replies: 3
    Last Post: 01-30-2011, 11:38 PM
  3. Rock Paper Scissors
    By 54byler in forum Advanced Java
    Replies: 2
    Last Post: 04-23-2009, 06:23 AM
  4. Need Help with Rock paper and Scissors Java Game
    By kingsun in forum New To Java
    Replies: 3
    Last Post: 11-17-2008, 03:35 AM
  5. Need help with Rock Paper Scissors Game
    By GettinGwap in forum New To Java
    Replies: 12
    Last Post: 10-19-2008, 06:15 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •