本人java初学者,现在正在做java扫雷小游戏,但被界面编写困扰了很久,求高手源码参考!
要求:
JMenu 游戏 关于
JMenuItem 难度 开始游戏 退出游戏 关于作者
最主要的是扫雷的主页面,求大神!!还有 计时器 //很久之前做的,忘了有什么功能了,lz将就下吧
import java.io.Serializable;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class FindMine implements ActionListener{
static Cube[][] cubeArray = new Cube[12][12];
static void setMine(){
int x = (int)(Math.random()*10 + 1);
int y = (int)(Math.random()*10 + 1);
if(cubeArray[x][y].isMine == false){
cubeArray[x][y].isMine = true;
}
else{setMine();}
}
static JButton[][] buttonArray = new JButton[10][10];
static int flag = 0;
public static void main(String[] args){
for(int i = 0;i < 12;i++){
for(int j = 0;j < 12;j++){
cubeArray[i][j] = new Cube();
cubeArray[i][j].X = i;
cubeArray[i][j].Y = j;
}
}
for(int i = 0;i < 10;i++){
setMine();
}
for(int i = 1;i < 11;i++){
for(int j = 1;j < 11;j++){
cubeArray[i][j].setHint();
}
}
for(int i = 1;i < 11;i++){
for(int j = 1;j < 11;j++){
System.out.print(cubeArray[i][j].hint);
}
System.out.print("\n");
}
JFrame jf;
JPanel jp1,jp2;
jf = new JFrame("FindMine");
jf.setSize(600,600);
jf.setVisible(true);
jf.setLayout(new CardLayout());
jp1 = new JPanel();
jp2 = new JPanel();
jp1.setLayout(new GridLayout(10,10));
jp2.setLayout(new GridLayout(10,10));
jf.getContentPane().add(jp1,"1");
jf.getContentPane().add(jp2,"2");
for(int i = 0;i < 10;i++){
for(int j = 0;j < 10;j++){
buttonArray[i][j] = new JB(i,j);
jp1.add(buttonArray[i][j]);
}
}
FindMine myHandler = new FindMine();
for(int i = 0;i < 10;i++){
for(int j = 0;j < 10;j++){
buttonArray[i][j].addActionListener(myHandler);
}
}
jf.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent e){
FindMine.cubeArray[((JB)e.getSource()).X + 1][((JB)e.getSource()).Y + 1].open();
}
}
class Cube implements Serializable{
int X,Y;
int hint = 0;
boolean isMine = false;
boolean isOpen = false;
boolean hasMine(){
if (this.isMine == true){return true;}
else {return false;}
}
void setHint(){
if(FindMine.cubeArray[this.X-1][this.Y-1].isMine == true){this.hint++;}
if(FindMine.cubeArray[this.X][this.Y-1].isMine == true){this.hint++;}
if(FindMine.cubeArray[this.X+1][this.Y-1].isMine == true){this.hint++;}