《Java扫雷游戏源代码(10页).doc》由会员分享,可在线阅读,更多相关《Java扫雷游戏源代码(10页).doc(10页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、-Java扫雷游戏源代码-第 10 页import java.awt.BorderLayout;import java.awt.Container;import java.awt.Font;import java.awt.GridLayout;import java.awt.Insets;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import javax.swi
2、ng.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JMenu;import javax.swing.JMenuBar;import javax.swing.JMenuItem;import javax.swing.JPanel;import javax.swing.Timer;public class ScanLei1 extends JFrame implements ActionListenerprivate static final long serialVersionUID
3、 = 1L;private Container contentPane;private JButton btn;private JButton btns;private JLabel b1;private JLabel b2;private JLabel b3;private Timer timer;private int row=9;private int col=9;private int bon=10;private int a;private int b;private int a1;private JPanel p,p1,p2,p3;public ScanLei1(String ti
4、tle)super(title);contentPane=getContentPane();setSize(297,377);this.setBounds(400, 100, 400, 500);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);timer =new Timer(1000,(ActionListener) this);a = new introw+2col+2;initGUI();public void initGUI() p3=new JPanel();b=bon;JMenuBar menuBar=new JMenuBar();JM
5、enu menu1=new JMenu(游戏);JMenu menu2=new JMenu(帮助);JMenuItem mi1=new JMenuItem(初级);JMenuItem mi2 = new JMenuItem(中级);JMenuItem mi3 =new JMenuItem(高级);mi1.addActionListener(this);menu1.add(mi1);mi2.addActionListener(this);menu1.add(mi2);ddActionListener(this);menu1.add(mi3);menuBar.add(menu1);menuBar.
6、add(menu2);p3.add(menuBar);b1=new JLabel(bon+);a1=new intbon;btn =new JButton(开始);btn.addActionListener(this);b2=new JLabel(0);b3=new JLabel();btns=new JButtonrow*col; p=new JPanel();p.setLayout(new BorderLayout();contentPane.add(p);p.add(p3,BorderLayout.NORTH);/combo=new JComboBox(new Object初级,中级,高
7、级 );/加监听/*combo.addItemListener(new ItemListener() p1=new JPanel();/在那个位置/( FlowLayout)p1.getLayout().setAlignment( FlowLayout.RIGHT);p1.add(b1);p1.add(btn);p1.add(b2);p1.add(b3);p.add(p3,BorderLayout.NORTH);p.add(p1,BorderLayout.CENTER); p2=new JPanel();p2.setLayout(new GridLayout(row,col,0,0);for(
8、int i=0;irow*col;i+)btnsi=new JButton();btnsi.setMargin(new Insets(0,0,0,0);btnsi.setFont(new Font(null,Font.BOLD,25);btnsi.addActionListener(this);btnsi.addMouseListener(new NormoreMouseEvent();p2.add(btnsi);contentPane.add(p,BorderLayout.NORTH);contentPane.add(p2,BorderLayout.CENTER);public void g
9、o()setVisible(true);public static void main(String args)new ScanLei1(扫雷).go();public void out(int a,JButton btns,ActionEvent e,int i,int x,int y) int p=1; if(axy=0) axy=10; btnsi.setEnabled(false);/33 for(int l=y-1;l-1&n-1&mrow) for(int q=0;qrow; if(n+col*q)=(m*col)&(n+col*q)-1&n-1&mcol) for(int q=0
10、;q=(m*col)&(n+col*q)-1&n-1&mcol) for(int q=0;q=(m*col)&(n+col*q)-1&n-1&mcol) for(int q=0;q=(m*col)&(n+col*q)(m+1)*col) if(axy+1!=0&axy+1!=10) btnsn+col*q.setText(axy+1+); axy+1=10; btnsn+col*q.setEnabled(false); else if(axy+1=0) out(a,btns,e,n+col*q,x,y+1); axy+1=10; btnsn+col*q.setEnabled(false); p
11、=0;public void actionPerformed(ActionEvent e) if(e.getActionCommand()=初级)row=9;col=9;bon=10;a1=new intbon;b=bon;/setSize(297,377);a = new introw+2col+2;this.remove(p2);timer.stop();b1.setText(10);b2.setText(0);b3.setText();btns=new JButtonrow*col; p2=new JPanel();p2.setLayout(new GridLayout(row,col,
12、0,0);for(int i=0;irow*col;i+)btnsi=new JButton( );btnsi.setMargin(new Insets(0,0,0,0);btnsi.setFont(new Font(null,Font.BOLD,25);btnsi.addActionListener(this);btnsi.addMouseListener(new NormoreMouseEvent();p2.add(btnsi);contentPane.add(p2,BorderLayout.CENTER);/setSize(297,377);this.pack();for(int i=0
13、;irow*col;i+)btnsi.setText( );btnsi.setEnabled(true);for(int i=0;irow+2;i+)for(int j=0;jcol+2;j+)aij=0;else if(e.getActionCommand()=中级)row=16;col=16;bon=40;/setSize(33*col,33*row+80);a1=new intbon;a = new introw+2col+2; b=bon;this.remove(p2);timer.stop();b1.setText(40);b2.setText(0);b3.setText();btn
14、s=new JButtonrow*col; p2=new JPanel();p2.setLayout(new GridLayout(row,col,0,0);for(int i=0;irow*col;i+)btnsi=new JButton( );btnsi.setMargin(new Insets(0,0,0,0);btnsi.setFont(new Font(null,Font.BOLD,25);btnsi.addActionListener(this);btnsi.addMouseListener(new NormoreMouseEvent();p2.add(btnsi);content
15、Pane.add(p2,BorderLayout.CENTER);this.pack();/setSize(33*col,33*row+80);for(int i=0;irow*col;i+)btnsi.setText();btnsi.setEnabled(true);for(int i=0;irow+2;i+)for(int j=0;jcol+2;j+)aij=0;else if(e.getActionCommand()=高级)row=16;col=32;bon=99;setSize(33*col,33*row+80);a1=new intbon;a = new introw+2col+2;
16、b=bon;this.remove(p2);timer.stop();b1.setText(99);b2.setText(0);b3.setText();btns=new JButtonrow*col; p2=new JPanel();p2.setLayout(new GridLayout(row,col,0,0);for(int i=0;irow*col;i+)btnsi=new JButton( );btnsi.setMargin(new Insets(0,0,0,0);btnsi.setFont(new Font(null,Font.BOLD,25);btnsi.addActionLis
17、tener(this);btnsi.addMouseListener(new NormoreMouseEvent();p2.add(btnsi);contentPane.add(p2,BorderLayout.CENTER);/setSize(33*col,33*row+80);this.pack();for(int i=0;irow*col;i+)btnsi.setText();btnsi.setEnabled(true);for(int i=0;irow+2;i+)for(int j=0;jcol+2;j+)aij=0;if(e.getSource()=btn)timer.start();
18、b=bon;b3.setText();/System.out.println(bon);/清空for(int i=0;irow*col;i+)btnsi.setText();btnsi.setEnabled(true);for(int i=0;irow+2;i+)for(int j=0;jcol+2;j+)aij=0;/产生随机数for(int i=0;ibon;i+) int p=1;int m=(int)(Math.random()*row*col);while(p=1) int l=1; int j;for( j=0;ji&l=1;j+) if(a1j=m) m=(int)(Math.r
19、andom()*row*col); l=0;if(j=i)a1i=m;p=0;b1.setText(bon+);b2.setText(0);/布雷 for(int i=0;ibon;i+) int x=(a1i/col+1); int y=(a1i%col+1);axy=100; for(int i=0;irow+2;i+) for(int j=0;jcol+2;j+) if(i=0|j=0|i=row+1|j=col+1) aij=0; for(int i=1;i=row;i+) for(int j=1;j=col;j+) if(aij!=100) for(int l=j-1;l=600)t
20、imer.stop();elset+;b2.setText(t+);for(int i=0;icol*row;i+) if(btnsi.getText()!=) int x=i/col+1; int y=i%col+1; if(e.getSource()=btnsi&axy=100) btnsi.setText(); btnsi.setEnabled(false); axy=10; for(int k=0;kcol*row;k+) int m1=k/col+1; int n1=k%col+1; if(am1n1!=10&btnsk.getText()=) btnsk.setText(*o*);
21、 for(int j=0;jcol*row;j+) int m=j/col+1; int n=j%col+1; if(amn=100) btnsj.setText(); btnsj.setEnabled(false); b3.setText(你输了 !); btnsj.setEnabled(false); amn=10; timer.stop(); else if(e.getSource()=btnsi) if(axy=0) out(a,btns,e,i,x,y); axy=10; btnsi.setEnabled(false); if(axy!=0&axy!=10) btnsi.setTex
22、t(axy+); btnsi.setEnabled(false); axy=10; else if(btnsi.getText()=)class NormoreMouseEvent extends MouseAdapterpublic void mouseClicked(MouseEvent e) System.out.println(b);for(int i=0;icol*row;i+) int x1=i/col+1; int y1=i%col+1;if(e.getSource()=btnsi&btnsi.getText()!=&ax1y1!=10) if(e.getButton()=MouseEvent.BUTTON3) btnsi.setText(); b-; if(b=0) int flag=0; for(int j=0;jbon) b1.setText(bon+); else b1.setText(b+); btnsi.setEnabled(true);