大家好,我有这段代码,并且我一直在努力一段时间。这是一种游戏,当单击“开始”按钮时,一堆按钮会弹出到屏幕上,并且高亮显示一个按钮,如果玩家单击突出显示的按钮,则该玩家将获得得分,但如果玩家未单击正确的按钮,则该玩家将失去得分。游戏运行正常,但是当我尝试使主菜单出现问题时。当我单击开始按钮时,它将打开应该在其中进行游戏的窗口(它具有正确的窗口名称,所以我知道它的工作原理),但是没有任何按钮。有人可以帮忙,因为我一直在这个问题上停留了大约一周。

谢谢

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);
            }

        }

    }

}


游戏代码为:

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;

    }

}

最佳答案

代替弹出一个新窗口(请参见The Use of Multiple JFrames, Good/Bad Practice?),更好的做法是使用CardLayout在视图之间交换。主菜单可以是面板视图,游戏可以是面板视图。

How to Use CardLayout处了解更多信息,您还可以看到一个使用主菜单的简单示例here

10-08 20:15