Results 1 to 6 of 6
- 06-15-2011, 06:43 PM #1
Member
- Join Date
- Jun 2011
- Posts
- 5
- Rep Power
- 0
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:
Can someone help me fix this problem?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(); } }Last edited by DarrylBurke; 06-15-2011 at 09:02 PM. Reason: Added code tags
- 06-15-2011, 08:57 PM #2
Moved here from Reviews/Advertising
db
- 06-15-2011, 11:05 PM #3
Where do you create the listeners that are to be called?
- 06-16-2011, 01:03 AM #4
Member
- Join Date
- Jun 2011
- Posts
- 5
- Rep Power
- 0
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");
- 06-16-2011, 02:28 AM #5
Member
- Join Date
- Jun 2011
- Posts
- 5
- Rep Power
- 0
I see what you mean. Thank you so much for your help!
- 06-16-2011, 06:37 AM #6
Similar Threads
-
Help with setting up a GUI for a Rock, Paper, Scissors game
By thorobred in forum New To JavaReplies: 3Last Post: 02-17-2011, 02:42 AM -
Rock, paper, scissors
By StevenF in forum New To JavaReplies: 3Last Post: 01-30-2011, 11:38 PM -
Rock Paper Scissors
By 54byler in forum Advanced JavaReplies: 2Last Post: 04-23-2009, 06:23 AM -
Need Help with Rock paper and Scissors Java Game
By kingsun in forum New To JavaReplies: 3Last Post: 11-17-2008, 03:35 AM -
Need help with Rock Paper Scissors Game
By GettinGwap in forum New To JavaReplies: 12Last Post: 10-19-2008, 06:15 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks