Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (566)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Help with non-working JButton  (Read 452 times)
0 Members and 1 Guest are viewing this topic.
Offline Nicky-G-

Senior Newbie





« Posted 2012-08-10 22:22:44 »

Hello I'm new to the forum, I wanted to make a proper introduction but I can't find the right place to make it Huh

I'm having some trouble with my JButtons. I'm trying to make a simple rock-paper-scissors game where the player picks either rock, paper, or scissors, but for some reason my JButtons for the user to pick won't work. For right now I just want the player score to increase by one whenever the user clicks on one of the buttons. I know this is very newbie stuff but I've gone over the code a hundred times and I just can't figure out whats wrong. Thanks in advance.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import javax.swing.*;


public class GamePanel implements ActionListener{
   
   int playersco = 0;
   static int compsco = 0;
   
   
   
   String playerroc, playersis, playerpap, player;
   static String compprint, comproc, comppap, compsis, comp;
   JButton roc, pap, sis;
   JPanel selectpanel, gamtex, compout;
   JLabel playerscore, compscore, compchoice;
   
   public JPanel ContentPane(){
     
      JPanel masterPanel = new JPanel();
      masterPanel.setLayout(null);
     
      gamtex = new JPanel();
      gamtex.setLayout(null);
      gamtex.setLocation(100, 10);
      gamtex.setSize(155,20);
      gamtex.setBackground(Color.yellow);
      masterPanel.add(gamtex);
     
      playerscore = new JLabel(""+ playersco +" Player");
      playerscore.setLocation(0,0);
      playerscore.setSize(90,20);
      playerscore.setHorizontalAlignment(0);
      gamtex.add(playerscore);
     
      compscore = new JLabel(""+compsco+" Computer");
      compscore.setLocation(20,0);
      compscore.setSize(190,20);
      compscore.setHorizontalAlignment(0);
      gamtex.add(compscore);
     
      compout = new JPanel();
      compout.setLayout(null);
      compout.setLocation(130,35);
      compout.setSize(110,100);
      masterPanel.add(compout);
      //compout.setBackground(Color.DARK_GRAY);
     
      compchoice = new JLabel(compprint);
      compchoice.setLocation(0,0);
      compchoice.setSize(85,20);
      compout.add(compchoice);
     
      selectpanel = new JPanel();
      selectpanel.setLayout(null);
      selectpanel.setLocation(0,15);
      selectpanel.setSize(100,190);
      //selectpanel.setBackground(Color.green);
     masterPanel.add(selectpanel);
     
      roc = new JButton("Rock");
      roc.setLocation(10,5);
      roc.setSize(80,30);
      roc.addActionListener(this);
      selectpanel.add(roc);
     
      pap = new JButton("Paper");
      pap.setLocation(10,45);
      pap.setSize(80,30);
      pap.addActionListener(this);
      selectpanel.add(pap);
     
      sis = new JButton("Sissors");
      sis.setLocation(10,85);
      sis.setSize(80,30);
      sis.addActionListener(this);
      selectpanel.add(sis);
     
      masterPanel.setOpaque(true);
      return masterPanel;
   }
   
   public void actionPerformed(ActionEvent e){
     
      if(e.getSource() == roc ){
         playersco = playersco +1;
         player = playerroc;
      }else if(e.getSource() == pap){
         playersco = playersco +1;
         player = playerpap;
      }else if(e.getSource() == sis){
         playersco = playersco +1;
         player = playersis;
      }
     
   
   }
   
   
   public static  void comprandom(){
     
      Random randomGenerator = new Random();
       int randomInt = randomGenerator.nextInt(3);
       if(randomInt == 1){
         
          comp = compsis;
          compprint = "Sissors!";
         compsco = compsco+1;
       }else if(randomInt == 2){
         
          comp = comproc ;
          compprint = "Rock!";
         compsco = compsco+1;
       }else
       {
          comp = comppap ;
          compprint = "Paper!";
         compsco = compsco + 1;
       }
   }
     
   

   
   private static void showgame(){
     
      JFrame frame = new JFrame("Rock, Paper, Sissors");
     
      GamePanel show = new GamePanel();
      frame.setContentPane(show.ContentPane());
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setSize(280, 190);
      frame.setVisible(true);
     
   }
   
   public static void main(String[] args){
     
      SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                  comprandom();
               showgame();
               
             
            }
        });
     
         
   
   }


}
Offline coltonoscopy

Junior Member


Medals: 2



« Reply #1 - Posted 2012-08-10 22:37:35 »

Nicky,

Welcome to the forums! Cheesy

The answer to this problem is pretty straightforward: your JLabel isn't being updated whenever the score changes! Maybe toss in a setText() method after your if, else-if, else statement in the compRandom() function to take care of that. :]

Best regards,
Colton

Straight flippin.
Offline Nicky-G-

Senior Newbie





« Reply #2 - Posted 2012-08-10 23:39:18 »

Hahaha oh man, thanks a ton for the speedy answer. Everything is working as it should. Once again, Thank you coltonoscopy!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline coltonoscopy

Junior Member


Medals: 2



« Reply #3 - Posted 2012-08-10 23:39:58 »

Hahaha oh man, thanks a ton for the speedy answer. Everything is working as it should. Once again, Thank you coltonoscopy!
My pleasure! :]

Colton

Straight flippin.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Pippogeek (35 views)
2014-09-24 16:13:29

Pippogeek (28 views)
2014-09-24 16:12:22

Pippogeek (17 views)
2014-09-24 16:12:06

Grunnt (40 views)
2014-09-23 14:38:19

radar3301 (23 views)
2014-09-21 23:33:17

BurntPizza (58 views)
2014-09-21 02:42:18

BurntPizza (29 views)
2014-09-21 01:30:30

moogie (34 views)
2014-09-21 00:26:15

UprightPath (47 views)
2014-09-20 20:14:06

BurntPizza (51 views)
2014-09-19 03:14:18
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!