由于
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;
}
}
答案 0 :(得分:0)
不是弹出一个新窗口(参见The Use of Multiple JFrames, Good/Bad Practice?),更好的做法是使用CardLayout
只在视图之间切换。主菜单可以是面板视图,游戏可以是面板视图。
在How to Use CardLayout了解详情,您还可以看到使用主菜单的简单示例here