用Java创建主菜单

时间:2014-01-26 16:41:43

标签: java swing methods jframe jbutton

嘿伙计们,我有这个代码,我已经有一段时间了。它是一个游戏,当点击开始按钮时,一串按钮弹出到屏幕上,一个按钮突出显示,如果玩家点击突出显示的按钮,玩家获得分数,但如果玩家没有点击正确的按钮,则玩家失去分数。游戏工作正常,但当我试图使主菜单出错的地方。当我点击开始按钮时,它会打开游戏所在的窗口(它有正确的窗口名称,所以我知道它的工作原理),但是没有任何按钮。有人可以帮助,因为我已经坚持这个问题大约一个星期。

由于

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;

    }

}

1 个答案:

答案 0 :(得分:0)

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

How to Use CardLayout了解详情,您还可以看到使用主菜单的简单示例here