1

hey guys i have this code, and i have been working on it for a while. Its a game that when the start button is clicked a bunch of buttons pop onto the screen and one button is highlighted, if the player clicks the highlighted button the player gains score but if the player doesn't click the correct button the player loses score. The game works fine, but when i tried to make the main menu is where things went wrong. When i click the start button it opens the window where the game is supposed to be(it has the correct window name so i know its working), however there aren't any buttons. Can someone please help since i have been stuck on this problem for about a week.

Thanks

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;

import javax.swing.*;

public class ClickerMenu extends JFrame {
    private JButton start, settings, exit, about;


    public ClickerMenu() {
        super("Main Menu");
        setLayout(new FlowLayout());

        start = new JButton("          Start Game          ");
        settings = new JButton("    Settings    ");
        exit = new JButton("    Exit    ");
        about = new JButton("    About    ");
        add(start);
        add(about);
        add(settings);
        add(exit);

        HandlerClass handler = new HandlerClass();
        start.addActionListener(handler);
        about.addActionListener(handler);
        settings.addActionListener(handler);
        exit.addActionListener(handler);
    }

    public class HandlerClass implements ActionListener {
        public void actionPerformed(ActionEvent event) {
            if (event.getSource() == start) {
                ClickerMain main = new ClickerMain();
                main.main(null);
            }
            if (event.getSource() == settings) {

            }
            if (event.getSource() == about) {

            }
            if (event.getSource() == exit) {
                System.exit(0);
            }

        }

    }

}

and the game code is:

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;

import javax.swing.*;

public class Clicker extends JFrame {
    private int counter = 0, place = 0, score, timer, holder, number, index;
    private JButton button[] = new JButton[20];
    randomButtons rbutton = new randomButtons();


    public Clicker() {
        super("Clicker The Game");
        setLayout(new FlowLayout());

        while (counter < button.length) {
            button[counter] = new JButton("Clicker");
            add(button[counter]);
            counter++;
        }
        HandlerClass handler = new HandlerClass();
        while (place < button.length) {
            button[place].addActionListener(handler);
            place++;
        }

        number = rbutton.randomButton();

        button[number].setBackground(Color.RED);

        System.out.println("number: " + number);

    }

    public class HandlerClass implements ActionListener {
        public void actionPerformed(ActionEvent event) {

            if ((JButton) event.getSource() == button[number]) {
                ++score;
                button[number].setBackground(null);

            } else {
                --score;
                button[number].setBackground(null);
            }
            number = rbutton.randomButton();
            button[number].setBackground(Color.RED);


            System.out.println(score);

        }

    }
    public int Scoring(){
        return score;

    }

}
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
Voids
  • 19
  • 3

1 Answers1

0

Instead of popping up a new window ( see The Use of Multiple JFrames, Good/Bad Practice?), better practice would be to use a CardLayout to just swap between views. The main menu can be a panel view, and the game can be a panel view.

Read more at How to Use CardLayout, and also you can see a simple example here that uses a main menu

Community
  • 1
  • 1
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720