实例介绍
【实例截图】

【核心代码】
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.Timer;
public class Mine {
// private static JFrame frame;//此处的static会导致 GridLayout无法正常显示
//private static final TimeCtrl tc=new TimeCtrl();
private JFrame frame;
private JPanel p1,p2,p3;
private JMenu degree,about;
private JMenuBar menuBar1;
private JMenuItem item1,item2,item3,item4,item5,item6,item7;
public static JLabel label1;
private JLabel label2,label3;
private int row,col,sum;
private int [][] data;
private JButton start;
private static int t=0,t0=0,t1=0,t2=0;
private int[][] tag;
private JButton[][] button;
//TimeCtrl t;
private int mineNum,mineCount;//the number of Mines and the number has be fined
private boolean isOver,over;//End of game;
private static Timer timer;//=new Timer(1000,new Time());
private ImageIcon imageIcon1,imageIcon2;
private static Mine primary,intermediate,advanced;
// private static Mine primary=new Mine(10,10,12),
// intermediate=new Mine(15,15,30),advanced=new Mine(20,20,40),current;
//private JContainer content;
public Mine(){}
public Mine(int r,int c,int n) {
//row=10;col=10;mineNum=12;
row=r;col=c;mineNum=n;
frame=new JFrame("Mine");
imageIcon1=new ImageIcon("./res/success.gif");
imageIcon2=new ImageIcon("./res/failed.gif");
//p1=new JPanel();
p2=new JPanel();
p3=new JPanel();
menuBar1=new JMenuBar();
degree=new JMenu("等级");
about=new JMenu("关于");
item1=new JMenuItem("初级");
item2=new JMenuItem("中级");
item3=new JMenuItem("高级");
item4=new JMenuItem("关于");
item5=new JMenuItem("最高分");
item6=new JMenuItem("存档");
item7=new JMenuItem("载入");
label1=new JLabel("时间");
label2=new JLabel("剩余标记数");
start=new JButton("开始");
button =new JButton[row][col];
data=new int[row][col];
tag=new int[row][col];
timer=new Timer(1000,new Time());
//content = frame.getContentPane();
}
public void init() {
degree.add(item1);
degree.add(item2);
degree.add(item3);
about.add(item5);
about.add(item4);
about.add(item6);
about.add(item7);
menuBar1.add(degree);
menuBar1.add(about);
frame.setLayout(new BorderLayout());
item1.addActionListener(new item1ActionListener());
item2.addActionListener(new item2ActionListener());
item3.addActionListener(new item3ActionListener());
item4.addActionListener(new item4ActionListener());
item5.addActionListener(new item5ActionListener());
item6.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
item6ActionListener1 A6=new item6ActionListener1(data,tag);
}});
//p2.setLayout(new BoxLayout(p2,BoxLayout.LINE_AXIS));//这个不合适
p2.setLayout(new FlowLayout(FlowLayout.CENTER,10,0));//FlowLayout(int align,hgap.vgap)
//The value of the alignment argument must be one of
///FlowLayout.LEFT, FlowLayout.RIGHT, FlowLayout.CENTER, FlowLayout.LEADING=3, or
//FlowLayout.TRAILING=4.//后面的拖尾的int java.awt.FlowLayout.CENTER = 1 [0x1]
//p2.setLayout(new GridLayout(1,3,20,20));
//label1.setSize(40,40);
//label2.setSize(40,40);
//start.setSize(40,40);
p3.setLayout(new GridLayout(row,col,4,4));
p2.add(label1);//,BorderLayout.WEST);//后面的加不加没用
p2.add(start);
p2.add(label2);//,BorderLayout.WEST);
//p2.set(100,400);
//p2.setBounds(0,50,100,400);
//frame.add(label1);label1.setBounds(0,50,50,50);//<---这个有用
frame.add(menuBar1,BorderLayout.NORTH);
frame.add(p2,BorderLayout.CENTER);
frame.add(p3,BorderLayout.SOUTH);
start.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent a)
{
for (int i = 0; i < row; i ) {
for (int j = 0; j < col; j ) {
button[i][j].setText(" ");
button[i][j].setBackground(Color.WHITE);
data[i][j]=0;
p3.add(button[i][j]);
isOver=false;
}
}
Deploy d=new Deploy();
data=d.DeployMine(row,col,mineNum);
mineCount=0;
start.setIcon(null);
label1.setText("GO");//t0=t2=t1=0;
timer.start();t=0;
//tc.setInit();
//t0=t2=t1=0;
}
});
for (int i = 0; i < row; i ) {
for (int j = 0; j < col; j ) {
button[i][j]=new JButton(" ");
button[i][j].setName((i ":" j));
button[i][j].setBackground(Color.WHITE);
button[i][j].addMouseListener(new MousePressed());
//button[i][j].setSize(40,40);
//button[i][j].setPreferredSize(null);
p3.add(button[i][j]);
}
}
Deploy d=new Deploy();
data=d.DeployMine(row,col,mineNum);//tc.setInit();
timer.start();t=0;//t0=t2=t1=0;//
//button[9][9].setText();
}
public int [][] getData()
{
return data;
}
public int [][] getTag()
{
return tag;
}
public class MousePressed extends MouseAdapter {//监听button
//public void actionPerformed(ActionEvent arg0)
public void mousePressed(MouseEvent e) {
try {
if(isOver)
return;
if(e.getButton()==MouseEvent.BUTTON3)
{
JButton btn=(JButton)e.getSource();
//JOptionPane.showMessageDialog(null, btn.getBackground(), "警告!!!", JOptionPane.ERROR_MESSAGE);
if(btn.getBackground()==Color.YELLOW)//如果已经排除 的地方右击 无反应
{
return;
}
//Deploy d=new Deploy();
//d.mousePressR(btn);//当点击的是右键的时候
mousePressR(btn);//当点击的是右键的时候
}
else {
JButton btn=(JButton)e.getSource();
if(!btn.getText().equals("?"))// 当 已经被标记了之后就不可以在点击
mousePress(btn);
}
} catch (Exception e2) {
// TODO: handle exception
e2.printStackTrace();
}
}
}
private void Check(JButton btn)
{
//JOptionPane.showMessageDialog(null, btn.getText(), "警告!!!", JOptionPane.ERROR_MESSAGE);
String str[]=btn.getName().split(":");
int i=Integer.parseInt(str[0]);
int j=Integer.parseInt(str[1]);
if(data[i][j]==-1&&button[i][j].getText().equals("?"))
{
sum--;//当取消 真正雷上的标记时
//label1.setText("" sum);
//JOptionPane.showMessageDialog(null, "sum--", "警告!!!", JOptionPane.ERROR_MESSAGE);
return;
}
if(data[i][j]==-1)
{
sum ;
}
if(sum>=mineNum)
{
//label1.setText("Success");
//胜利了
timer.stop();
isOver=true;
start.setIcon(imageIcon1);
String nameTop=JOptionPane.showInputDialog(null,"请输入你的名字:","最高分录入",JOptionPane.PLAIN_MESSAGE);
System.out.print("\n\n" nameTop "Time:" t);
new Record(t,nameTop);
}
else {
//label1.setText(" " sum);
}
}
private void mousePressR(JButton btn)
{
//get the button object which has been marked
String str[]=btn.getName().split(":");
int i=Integer.parseInt(str[0]);
int j=Integer.parseInt(str[1]);
if(btn.getText().equals("?"))
{Check(btn);
btn.setText(" ");
tag[i][j]=0;
mineCount--;
label2.setText("剩余标记数" (mineNum-mineCount));
btn.setBackground(Color.WHITE);
//if()
}
else
{mineCount ;Check(btn);
if(mineCount<=mineNum)
{
btn.setText("?");
btn.setBackground(Color.BLUE);
tag[i][j]=2;
}
else
{
JOptionPane.showMessageDialog(null, "标记数不能大于地雷数", "警告!!!", JOptionPane.ERROR_MESSAGE);
mineCount--;
//label1.setText("标记数不能大于地雷数");
}
label2.setText("剩余标记数" (mineNum-mineCount));
}
}
private void mousePress(JButton btn) {
String str[]=btn.getName().split(":");
int i=Integer.parseInt(str[0]);
int j=Integer.parseInt(str[1]);
if(data[i][j]==-1)
{
gameOver(true);
return;
}
else {
tag[i][j]=1;
if(data[i][j]==0){btn.setText("");}
else {
btn.setText(data[i][j] "");}
btn.setBackground(Color.YELLOW);
if(data[i][j]==0)
{
for (int k = -1; k <=1; k ) {
for (int k2 = -1; k2 <=1; k2 ) {
if(i k>=0&&j k2>=0&&i k<row&&j k2<col)
if(button[i k][j k2].getText().equals(" "))
{mousePress(button[i k][j k2]);}
}
}
}
}
}
private void gameOver(boolean over)
{
if(over==true)
{
timer.stop();
//tc.Stop();
for (int i = 0; i < row; i ) {
for (int j = 0; j < col; j ) {
if(data[i][j]==-1)
{
button[i][j].setText("M");
button[i][j].setBackground(Color.RED);
}
}
}
start.setIcon(imageIcon2);
//TimeCtrl tim=new TimeCtrl();tim.Stop();
//t.Stop();
//label2.setIcon(imageIcon2);
//label1.setText(imageIcon1);
timer.stop();
//tc.Stop();
isOver=true;
return;
}
}
/* public class Time1 implements ActionListener {
int t0=0,t1=0,t2=0;
String s="";
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
t0 ;
if(t0>=60)
{
t1=t0/60;
t0=t0%60;
}
if(t1>=60)
{
t2=t1/60;
t1=t1%60;
}
if(t2>0)
s="时间:" t2 "小时" t1 "分" t0 "秒";
if(t1>0)
s="时间:" t1 "分" t0 "秒";
if(t0>=0)
s="时间:" t0 "秒";
//Mine c=new Mine();
//label1.setText(s);
}
}*/
public class item1ActionListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
timer.stop();
primary=new Mine(10,10,12);
primary.init();
primary.start(500,400);
//tc.setInit();
//primary.timer.stop();
//intermediate.timer.stop();
//advanced.timer.stop();
//t0=t2=t1=0;
//System.gc(advanced);
//primary.timer.start();
//intermediate=new Mine();
//primary.FrameClose(frame);
intermediate.FrameClose(frame);
advanced.FrameClose(frame);
//primary.init();
}
}
public class item2ActionListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
//timer.cancel();
timer.stop();
intermediate=new Mine(15,15,20);
intermediate.init();
intermediate.start(750,600);
//tc.setInit();
//t0=t2=t1=0;
primary.FrameClose(frame);
//intermediate.FrameClose(frame);
advanced.FrameClose(frame);
//primary.init();
}
}
public class item3ActionListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
timer.stop();
advanced=new Mine(20,20,40);
advanced.init();
advanced.start(1000,750);
//tc.setInit();
//t0=t2=t1=0;
primary.FrameClose(frame);
intermediate.FrameClose(frame);
//advanced.FrameClose(frame);
//primary.init();
}
}
public class Time implements ActionListener {
String s="";
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
t ;
t2=(int)t/360;
t1=(t/60)%60;
t0=t%60;
if(t2>0)
s="时间:" t2 "小时" t1 "分" t0 "秒";
else
if(t1>0)
s="时间:" t1 "分" t0 "秒";
else
if(t0>=0)
s="时间:" t0 "秒";
label1.setText(s);
}
}
public void FrameClose(JFrame frame) {
//if(b==true)
frame.setVisible(false);
//primary.setVisible(false);
//frame.exit(0);
// System.exit(0);
}
public void start(int x,int y)
{
frame.setSize(x,y);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args) {
primary=new Mine(10,10,12);
primary.init();
primary.start(500,450);
//TimeCtrl t=new TimeCtrl();//t.start();
//Mine.frame.setSize(500,400);
//Mine.frame.setVisible(true);
}
}
标签: 游戏
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


网友评论
我要评论