java 初学者联系代码.docx
《java 初学者联系代码.docx》由会员分享,可在线阅读,更多相关《java 初学者联系代码.docx(21页珍藏版)》请在冰豆网上搜索。
java初学者联系代码
packagemySelf;
importjava.awt.Color;
importjava.awt.Container;
importjava.awt.Dimension;
importjava.awt.GridLayout;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.awt.event.MouseAdapter;
importjava.awt.event.MouseEvent;
importjavax.swing.JButton;
importjavax.swing.JFrame;
importjavax.swing.JLabel;
importjavax.swing.JOptionPane;
importjavax.swing.JPanel;
/**
*练习代码规范
*
*@author王毅臣
*
*/
publicclassCheckMineSweeping{
privateintnumber=40;//设置地雷的个数
privateint[]num;//存放地雷的位置
privateintintx=10,inty=10;//显示多少行列
privatechar[]ch;//存放标签中所有要显示的字符
privateinttemp=0;//临时变量
privateJFramejf=null;
privateContainercon=null;
privateJPaneljp=null;
privateJButton[]jbs;
privateJLabel[]jls;
privateJPanel[]jps;
publicCheckMineSweeping(){
ch=newchar[intx*inty];
num=newint[number];
jbs=newJButton[intx*inty];
jls=newJLabel[intx*inty];
jps=newJPanel[intx*inty];
//首先初始化数组
intj=0;
while(true){
if(j==number){//循环结束条件
break;
}
temp=(int)(Math.random()*(intx*inty));//随即生成正整数
booleanflag=false;//判断是否重复的变量
for(inti=0;iif(num[i]==temp){
flag=true;
break;
}
}
if(!
flag){//说明生成的变量重复重新再生成一次
num[j++]=temp;
}
}
for(inti=0;ich[num[i]]='⊙';
}
for(inti=0;iif(ch[i]!
='⊙'){//说不是地雷那就是他周边的数据
if(i==0){//如果是第一个方格子
temp=0;
temp+=ch[i+1]=='⊙'?
1:
0;//右边
temp+=ch[i+intx]=='⊙'?
1:
0;//下边
temp+=ch[i+1+intx]=='⊙'?
1:
0;//右下边
if(temp==0){
ch[i]='';
}else{
ch[i]=this.getChar(temp);
}
}elseif(i==intx){//说明是右上角的格子
temp=0;
temp+=ch[i-1]=='⊙'?
1:
0;//左边
temp+=ch[i+intx]=='⊙'?
1:
0;//下边
temp+=ch[i+intx+1]=='⊙'?
1:
0;//左下边
if(temp==0){
ch[i]='';
}else{
ch[i]=this.getChar(temp);
}
}elseif(i==(intx-1)*inty){//左下面
temp=0;
temp+=ch[i+1]=='⊙'?
1:
0;//右边
temp+=ch[i-intx]=='⊙'?
1:
0;//上边
temp+=ch[i+1-intx]=='⊙'?
1:
0;//右上边
if(temp==0){
ch[i]='';
}else{
ch[i]=this.getChar(temp);
}
}elseif(i==intx*inty-1){//右下面
temp=0;
temp+=ch[i-1]=='⊙'?
1:
0;//左边
temp+=ch[i-intx]=='⊙'?
1:
0;//上边
temp+=ch[i-intx-1]=='⊙'?
1:
0;//右上边
if(temp==0){
ch[i]='';
}else{
ch[i]=this.getChar(temp);
}
}elseif(itemp=0;
temp+=ch[i-1]=='⊙'?
1:
0;//左边
temp+=ch[i+1]=='⊙'?
1:
0;//右边
temp+=ch[i+intx]=='⊙'?
1:
0;//下边
temp+=ch[i+intx-1]=='⊙'?
1:
0;//左下边
temp+=ch[i+intx+1]=='⊙'?
1:
0;//右下边
if(temp==0){
ch[i]='';
}else{
ch[i]=this.getChar(temp);
}
}elseif((intx-1)*inty
temp=0;
temp+=ch[i+1]=='⊙'?
1:
0;//右边
temp+=ch[i-1]=='⊙'?
1:
0;//左边
temp+=ch[i-intx]=='⊙'?
1:
0;//上边
temp+=ch[i-intx+1]=='⊙'?
1:
0;//右上边
temp+=ch[i-intx-1]=='⊙'?
1:
0;//左上边
if(temp==0){
ch[i]='';
}else{
ch[i]=this.getChar(temp);
}
}elseif(i%intx==0){
temp=0;
temp+=ch[i+1]=='⊙'?
1:
0;//右边
temp+=ch[i-intx]=='⊙'?
1:
0;//上边
temp+=ch[i+intx]=='⊙'?
1:
0;//下边
temp+=ch[i-intx+1]=='⊙'?
1:
0;//右上边
temp+=ch[i+intx+1]=='⊙'?
1:
0;//右下边
if(temp==0){
ch[i]='';
}else{
ch[i]=this.getChar(temp);
}
}elseif(i%intx==(intx-1)){
temp=0;
temp+=ch[i-1]=='⊙'?
1:
0;//左边
temp+=ch[i-intx]=='⊙'?
1:
0;//上边
temp+=ch[i+intx]=='⊙'?
1:
0;//下边
temp+=ch[i-intx-1]=='⊙'?
1:
0;//左上边
temp+=ch[i+intx-1]=='⊙'?
1:
0;//左下边
if(temp==0){
ch[i]='';
}else{
ch[i]=this.getChar(temp);
}
}else{//剩下的部分就是中间普通的部分
temp=0;
temp+=ch[i+1]=='⊙'?
1:
0;//右边
temp+=ch[i-1]=='⊙'?
1:
0;//右边
temp+=ch[i-intx]=='⊙'?
1:
0;//上边
temp+=ch[i+intx]=='⊙'?
1:
0;//下边
temp+=ch[i-intx+1]=='⊙'?
1:
0;//右上边
temp+=ch[i+intx+1]=='⊙'?
1:
0;//右下边
temp+=ch[i-intx-1]=='⊙'?
1:
0;//左上边
temp+=ch[i+intx-1]=='⊙'?
1:
0;//左下边
if(temp==0){
ch[i]='';
}else{
ch[i]=this.getChar(temp);
}
}
}
}
jf=newJFrame("扫雷游戏");
jf.setBounds(350,250,150,150);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
con=jf.getContentPane();
jp=newJPanel();
jp.setBackground(Color.GRAY);
jp.setLayout(newGridLayout(intx,inty,2,2));
con.add(jp);
JButtonjb;
JLabeljl;
JPaneljpanel;
for(inti=0;ijb=newJButton();
jb.setPreferredSize(newDimension(25,25));
jb.setActionCommand(""+i+"");
jbs[i]=jb;
jl=newJLabel(""+ch[i]+"");
jl.setHorizontalAlignment(JLabel.CENTER);
jls[i]=jl;
jps[i]=newJPanel();
jps[i].add(jb);
jps[i].setLayout(newGridLayout(1,1));
jp.add(jps[i]);
jb.addActionListener(newClickLeftListener(this));//添加左键监听事件
jb.addMouseListener(newClickRightListener(this));//添加右键监听事件
}
jf.pack();
jf.validate();
}
/*
*添加左键点击监听事件
*/
classClickLeftListenerimplementsActionListener{
privateCheckMineSweepingcms;
publicClickLeftListener(){}
publicClickLeftListener(CheckMineSweepingcms){
this.cms=cms;
}
@Override
publicvoidactionPerformed(ActionEvente){
//TODOAuto-generatedmethodstub
action(Integer.parseInt(e.getActionCommand()));
isSuccess();
}
/*
*主导方法
*/
publicvoidaction(intn){
if(cms.jbs[n].getBackground()!
=Color.PINK
&&cms.jps[n].getComponent(0).getClass().toString().equals("classjavax.swing.JButton")){
if(cms.ch[n]=='⊙'){//说明踩中地雷了
this.peckButtonAndChangeColor(n);
cms.jf.setTitle("很遗憾,中雷了!
");
endingWork();
}elseif(Character.isDigit(ch[n])){//说明是数字
this.peckButtonAndChangeColor(n);
}else{//说明是空格
this.dealSpace(n);
}
}
}
/*
*判断是否成功
*/
publicvoidisSuccess(){
intnum=0;
for(inti=0;iif(cms.ch[i]=='⊙'&&cms.jbs[i].getBackground()==Color.PINK){
if(++num==cms.number){
cms.jf.setTitle("恭喜你,扫雷英雄!
");
for(intj=0;jendingWork();
}
break;
}
}
}
}
/*
*收尾工作
*/
publicvoidendingWork(){
for(inti=0;iif(cms.jbs[i].getBackground()==Color.PINK&&cms.ch[i]!
='⊙'){//说是你错了
cms.jls[i].setText("※");
}elseif(cms.jbs[i].getBackground()==Color.PINK&&cms.ch[i]=='⊙'){
cms.jls[i].setText("√");
}elseif(cms.ch[i]!
='⊙'&&cms.jps[i].getComponent(0).getClass().toString()
.equals("classjavax.swing.JButton")){
cms.jls[i].setText("");
}
this.peckButtonAndChangeColor(i);
}
}
/*
*去掉面板中的按钮而且要改变标签中字体的颜色
*/
publicvoidpeckButtonAndChangeColor(intn){
cms.jps[n].remove(cms.jbs[n]);
switch(cms.ch[n]){
case'1':
cms.jls[n].setForeground(Color.BLUE);
break;
case'2':
cms.jls[n].setForeground(Color.GREEN);
break;
case'3':
cms.jls[n].setForeground(Color.RED);
break;
case'4':
cms.jls[n].setForeground(Color.MAGENTA);
break;
case'5':
cms.jls[n].setForeground(Color.CYAN);
break;
case'6':
cms.jls[n].setForeground(Color.ORANGE);
break;
case'7':
cms.jls[n].setForeground(Color.PINK);
break;
case'8':
cms.jls[n].setForeground(Color.DARK_GRAY);
break;
}
cms.jps[n].add(cms.jls[n]);
cms.jps[n].validate();
}
/*
*处理空格连片问题
*/
publicvoiddealSpace(intn){
this.peckButtonAndChangeColor(n);
if(n==0){
this.action(n+1);
this.action(n+intx+1);
this.action(n+intx);
}elseif(n==intx-1){
this.action(n-1);
this.action(n+intx-1);
this.action(n+intx);
}elseif(n==(inty-1)*intx){
this.action(n+1);
this.action(n-intx);
this.action(n-intx+1);
}elseif(n==intx*inty-1){
this.action(n-1);
this.action(n-intx);
this.action(n-intx-1);
}elseif(nthis.action(n-1);
this.actio