Search code examples
javaeclipsewindowbuilderswingbuilder

WindowBuilder how to use a different button to display a counter


I have a program that i am creating in Windowbuilder using eclipse. I want the program to generate a random number and the user would have to guess if it's even or odd. I already created the game based on the command line, but what i'm trying to do is do it with a GUI. The one problem I have is when I want to end the game and let the user see their score with a quit button. However, when I try to show the final score of how many times they got it correct and incorrect I cant because it's in a different method. So I want a separate button when clicked to display the total right and wrong. This is the code:

 import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JLabel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Random;
public class RandomNumber {

    private JFrame frame;
    private JTextField textFieldMe;
    private JTextField textFieldCpu;
    private JTextField textFieldScore;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    RandomNumber window = new RandomNumber();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public RandomNumber() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */

    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        textFieldMe = new JTextField();
        textFieldMe.setBounds(152, 62, 86, 20);
        frame.getContentPane().add(textFieldMe);
        textFieldMe.setColumns(10);

        textFieldCpu = new JTextField();
        textFieldCpu.setBounds(152, 118, 86, 20);
        frame.getContentPane().add(textFieldCpu);
        textFieldCpu.setColumns(10);

        textFieldScore = new JTextField();
        textFieldScore.setBounds(152, 217, 86, 20);
        frame.getContentPane().add(textFieldScore);
        textFieldScore.setColumns(10);

        JButton btnGo = new JButton("GO!");/* this is the start button*/
        btnGo.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Random rand = new Random();
                int correctCount = 0;
                int incorrectCount = 0;

                int ran = rand.nextInt(50) + 1;
                String me = textFieldMe.getText();
                if (ran % 2 == 0 && me.equals("e") || ran % 2 != 0 && me.equals("o")){
                    correctCount++;
                }else if (ran % 2 == 0 && me.equals("o") || ran % 2 != 0 && me.equals("e")){
                    incorrectCount++;
                }

                textFieldCpu.setText(Integer.toString(ran));

                int totalRight = correctCount;
                int totalWrong = incorrectCount;
                String right = Integer.toString(totalRight);
                String wrong = Integer.toString(totalWrong);


            }
        });
        btnGo.setBounds(149, 149, 89, 23);
        frame.getContentPane().add(btnGo);

        JButton btnQuit = new JButton("Quit"); /* i want this button to display the score*/
        btnQuit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                String right = Integer.toString(totalRight);
                String wrong = Integer.toString(totalWrong);

                textFieldScore.setText("Correct & wrong: " + right + wrong ); 
            }
        });
        btnQuit.setBounds(321, 216, 89, 23);
        frame.getContentPane().add(btnQuit);

        JLabel lblNewLabel = new JLabel("Your Number");
        lblNewLabel.setBounds(167, 36, 135, 14);
        frame.getContentPane().add(lblNewLabel);

        JLabel lblNewLabel_1 = new JLabel("CPU Guess");
        lblNewLabel_1.setBounds(162, 93, 71, 14);
        frame.getContentPane().add(lblNewLabel_1);

        JLabel lblScore = new JLabel("Score");
        lblScore.setBounds(179, 192, 46, 14);
        frame.getContentPane().add(lblScore);
    }
}

Solution

  • I would try to make right and wrong visible to every method by converting them to a variable in your class. The new class would look something like

    public class RandomNumber {
      ...
      private int right, wrong;
      ...
      private void initialize() {
        ...
        btnGo.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            Random rand = new Random();
            int ran = rand.nextInt(50) + 1;
    
            String me = textFieldMe.getText();
            if (ran % 2 == 0 && me.equals("e") || ran % 2 != 0 && me.equals("o")){
              ++right;
            }else if (ran % 2 == 0 && me.equals("o") || ran % 2 != 0 && me.equals("e")){
              ++wrong;
            }
    
            textFieldCpu.setText("" + ran);
          }
        }
        ...
        btnQuit.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            textFieldScore.setText("Correct : " + right + " Wrong: " wrong );
            //will automatically convert ints to String
          }
        }
        ...
      }
      ...
    }