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 ???
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.
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();
}
});
}
}