Rie Rie
Rie Rie

Reputation: 11

Why is my JLabel code not updating when I pressed the button?

I am trying to do a rock-paper-scissors Java code using Swing GUI. As you can see under every button action code, I included a line to change the compChoice label, but nothing appears. How to make the code run so that the compChoice label get updated? BTW, I'm using IntelliJ.

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Scanner;
import java.util.Random;

public class RPS implements ActionListener {

   JFrame frame;
   JPanel panel;
   JButton buttonR;
   JButton buttonP;
   JButton buttonS;
   JLabel title;
   JLabel comChoice;

   int userScore = 0;
   int comScore = 0;
   String userInput;



    JLabel ps;
    JLabel cs;


     public RPS() {
        frame = new JFrame();
        panel = new JPanel(new GridBagLayout());
        buttonR = new JButton("ROCK");
        buttonP = new JButton("PAPER");
        buttonS = new JButton("SCISSORS");
        title = new JLabel("ROCK vs PAPER vs SCISSORS");
        ps = new JLabel("Player Score : 0");
        cs = new JLabel("Computer Score : 0");
        comChoice = new JLabel("Computer choice : " );




        GridBagConstraints c = new GridBagConstraints();             //ADDING THE COMPONENTS
        c.insets = new Insets(30, 30, 30, 30);
        c.gridx = 3;
        c.gridy = 0;
        panel.add(title, c);
        c.insets = new Insets(10, 10, 10, 10);
        c.gridx = 1;
        c.gridy = 2;
        panel.add(buttonR, c);
        c.gridx = 3;
        c.gridy = 2;
        panel.add(buttonP, c);
        c.gridx = 5;
        c.gridy = 2;
        panel.add(buttonS, c);
        c.insets = new Insets(15, 15, 15, 15);
        c.gridx = 2;
        c.gridy = 3;
        panel.add(ps, c);
        c.gridx = 4;
        c.gridy = 3;
        panel.add(cs, c);
        c.insets = new Insets(15, 15, 15, 15);
        c.gridx = 3;
        c.gridy = 3;
        panel.add(comChoice, c);




        buttonR.addActionListener(this);
        buttonP.addActionListener(this);
        buttonS.addActionListener(this);


        frame.add(panel, BorderLayout.CENTER);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setTitle("Rock Paper Scissors");
        frame.pack();
        frame.setVisible(true);

    }


    public static void main(String[] args) {
        new RPS();


    }


    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == buttonR) {
            userInput = "ROCK";
            Random rand = new Random();
            String compGuess[] = {"ROCK", "PAPER", "SCISSORS"};
            Scanner input = new Scanner(System.in);
            String rand1 = compGuess[rand.nextInt(3)];
            comChoice = new JLabel("Computer choice : " + rand1);
        }
        if (e.getSource() == buttonP) {
            userInput = "PAPER";
            Random rand = new Random();
            String compGuess[] = {"ROCK", "PAPER", "SCISSORS"};
            Scanner input = new Scanner(System.in);
            String rand1 = compGuess[rand.nextInt(3)];
            comChoice = new JLabel("Computer choice : " + rand1);
        }
        if (e.getSource() == buttonS) {

            userInput = "SCISSORS";
            Random rand = new Random();
            String compGuess[] = {"ROCK", "PAPER", "SCISSORS"};
            Scanner input = new Scanner(System.in);
            String rand1 = compGuess[rand.nextInt(3)];
            comChoice = new JLabel("Computer choice : " + rand1);






        }



    }

}

I want the GUI to be updated after I click the button, but nothing happens. Is there something that I am missing?

Upvotes: 0

Views: 40

Answers (1)

Thomas Kläger
Thomas Kläger

Reputation: 21435

Your code creates new labels with the computer choice:

comChoice = new JLabel("Computer choice : " + rand1);

but these labels are never added to the UI and therefore never show up.

Instead, you should update the text on the existing label:

comChoice.setText("Computer choice : " + rand1);

Additionally, the code in the if statements contains this block every time:

        Random rand = new Random();
        String compGuess[] = {"ROCK", "PAPER", "SCISSORS"};
        Scanner input = new Scanner(System.in);
        String rand1 = compGuess[rand.nextInt(3)];
        comChoice = new JLabel("Computer choice : " + rand1);

One thing is the Scanner: you never use it, why do you create one? Remove that line!

And you could move that code repeated code into a method and call it when the user makes his choice:

    private void computerChoice() {
        Random rand = new Random();
        String compGuess[] = {"ROCK", "PAPER", "SCISSORS"};
        String rand1 = compGuess[rand.nextInt(3)];
        comChoice.setText("Computer choice : " + rand1);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == buttonR) {
            userInput = "ROCK";
            computerChoice();
        }
        if (e.getSource() == buttonP) {
            userInput = "PAPER";
            computerChoice();
        }
        if (e.getSource() == buttonS) {
            userInput = "SCISSORS";
            computerChoice();
        }
    }

Upvotes: 2

Related Questions