📄 mainframe.java
字号:
void fileExit_ActionPerformed(ActionEvent e)
{
System.exit(0);
}
void helpAbout_ActionPerformed(ActionEvent e)
{
JOptionPane.setDefaultLocale(Locale.CHINESE);
JOptionPane.showMessageDialog(this, new MainFrame_AboutBoxPanel1(), "About", JOptionPane.PLAIN_MESSAGE);
}
void onNew(ActionEvent e)
{
// this.remove(jPanel1);
buttonOpen.setIcon(imageNew);
if(isRestart)
{
isRestart = false;
try
{
theTimeThread.join();
}
catch(InterruptedException ex)
{
JOptionPane.showMessageDialog(null,"InterruptedException occurred here: " + ex);
}
}
jPanel1.removeAll();
// jPanel1.add(jLabelTestImage,null);
for(int i = 0;i < this.sumOfButton;i ++)
for(int j = 0;j < this.sumOfButton;j ++)
{
mineButton[i][j] = new MyButton();
mineButton[i][j].setBounds(new Rectangle(j*16,i*16,16,16));
jPanel1.add(mineButton[i][j],null);
// mineButton[i][j].addActionListener(new ClickButton(this,i,j));
mineButton[i][j].addMouseListener(new ClickButton(this,i,j));
jLabelTest[i][j] = new JLabel();
jLabelTest[i][j].addMouseListener(new ClickLabel(this,i,j));
jLabelTest[i][j].setBackground(new Color(63,109,160));
jLabelTest[i][j].setForeground(new Color(61,65,155));
// testlabel.setFont(new Font("华文中宋",1,12));
jLabelTest[i][j].setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
jLabelTest[i][j].setBounds(j*16,i*16,16,16);
}
this.setSize(1000,1000);
if(level.startsWith("primer"))
{
this.setSize(167,237);
// this.sumOfButton=10;
this.sumOfMine = 10;
this.sumOfNotMine = 90;
this.jLabel1.setText(" ");
this.jLabel2.setText(" ");
}
else
if(level.startsWith("middle"))
{
this.setSize(249,320);
// this.sumOfButton=15;
this.jLabel1.setText(" ");
this.jLabel2.setText(" ");
this.sumOfMine = 40;
this.sumOfNotMine = 185;
}
else
if(level.startsWith("high"))
{
this.setSize(329,400);
this.jLabel1.setText(" ");
this.jLabel2.setText(" ");
// this.sumOfButton=20;
this.sumOfMine = 99;
this.sumOfNotMine = 301;
}
jLabelTime.setText("0000");
jLabelMineNum.setText("00" + this.sumOfMine);
Random myrandom = new Random();
//随机的分配雷的位置
int tempNum = sumOfMine;
// int tempFlag = 0;
while(tempNum > 0)
{
for(int iflag = 0;iflag < this.sumOfButton;iflag ++)
for(int jflag = 0;jflag < this.sumOfButton;jflag++)
{
if(tempNum > 0)
{
if(myrandom.nextInt(25)==0)
{
if(mineButton[iflag][jflag].getFlag())
break;
else
{
mineButton[iflag][jflag].setFlag(true);
tempNum--;
}
}
}
else
break;
}
}
//end of distribute mine
repaint();
//beginning of counting number of mine around everybutton
for(int icount = 0; icount < this.sumOfButton; icount++)
for(int jcount = 0; jcount < this.sumOfButton; jcount++)
{
if(icount > 0)
{
if(mineButton[icount-1][jcount].getFlag())
mineButton[icount][jcount].addNumOfMine();
if((jcount > 0)&&(mineButton[icount-1][jcount-1].getFlag()))
mineButton[icount][jcount].addNumOfMine();
if((jcount < this.sumOfButton-1)&&(mineButton[icount-1][jcount+1].getFlag()))
mineButton[icount][jcount].addNumOfMine();
}
if(icount < this.sumOfButton-1)
{
if(mineButton[icount+1][jcount].getFlag())
mineButton[icount][jcount].addNumOfMine();
if((jcount > 0)&&(mineButton[icount+1][jcount-1].getFlag()))
mineButton[icount][jcount].addNumOfMine();
if((jcount < this.sumOfButton-1)&&(mineButton[icount+1][jcount+1].getFlag()))
mineButton[icount][jcount].addNumOfMine();
}
if((jcount > 0)&&(mineButton[icount][jcount-1]).getFlag())
mineButton[icount][jcount].addNumOfMine();
if((jcount < this.sumOfButton-1)&&(mineButton[icount][jcount+1]).getFlag())
mineButton[icount][jcount].addNumOfMine();
}
}
void clickButton(int i,int j)
{
if(!mineButton[i][j].getEnable())
return ;
if(!isRestart)
{
isRestart = true;
theTimeThread = new TimeThread(this);
theTimeThread.start();
}
if(!mineButton[i][j].getFlag())
{
this.sumOfNotMine--;
jPanel1.remove(mineButton[i][j]);
mineButton[i][j].setEnable(false);
// JLabel testlabel = new JLabel();
if(mineButton[i][j].getNumofMine()== 0)
{
jLabelTest[i][j].setIcon(imageCurv);
//beginning process button around mineButton[i][j]
if((i > 0) &&(!mineButton[i-1][j].getFlag())&&(mineButton[i-1][j].getEnable())&&((mineButton[i-1][j].getNumOfClick()%3)!=1))
clickButton(i-1,j);
if((i < this.sumOfButton-1) &&(!mineButton[i+1][j].getFlag())&&(mineButton[i+1][j].getEnable())&&((mineButton[i+1][j].getNumOfClick()%3)!=1))
clickButton(i+1,j);
if((j > 0) &&(!mineButton[i][j-1].getFlag())&&(mineButton[i][j-1].getEnable())&&((mineButton[i][j-1].getNumOfClick()%3)!=1))
clickButton(i,j-1);
if((j < this.sumOfButton-1) &&(!mineButton[i][j+1].getFlag())&&(mineButton[i][j+1].getEnable())&&((mineButton[i][j+1].getNumOfClick()%3)!=1))
clickButton(i,j+1);
if((i > 0)&&(j > 0)&&(!mineButton[i-1][j-1].getFlag())&&(mineButton[i-1][j-1].getEnable())&&((mineButton[i-1][j-1].getNumOfClick()%3)!=1)&&(!mineButton[i-1][j].getEnable()||!mineButton[i][j-1].getEnable()))
clickButton(i-1,j-1);
if((i > 0)&&(j < this.sumOfButton-1)&&(!mineButton[i-1][j+1].getFlag())&&(mineButton[i-1][j+1].getEnable())&&((mineButton[i-1][j+1].getNumOfClick()%3)!=1)&&(!mineButton[i-1][j].getEnable()||!mineButton[i][j+1].getEnable()))
clickButton(i-1,j+1);
if((i < this.sumOfButton-1)&&(j > 0)&&(!mineButton[i+1][j-1].getFlag())&&(mineButton[i+1][j-1].getEnable())&&((mineButton[i+1][j-1].getNumOfClick()%3)!=1)&&(!mineButton[i][j-1].getEnable()||!mineButton[i+1][j].getEnable()))
clickButton(i+1,j-1);
if((i < this.sumOfButton-1)&&(j < this.sumOfButton-1)&&(!mineButton[i+1][j+1].getFlag())&&(mineButton[i+1][j+1].getEnable())&&((mineButton[i+1][j+1].getNumOfClick()%3)!=1)&&(!mineButton[i+1][j].getEnable()||!mineButton[i][j+1].getEnable()))
clickButton(i+1,j+1);
}
else
{
String strCount = " "+mineButton[i][j].getNumofMine();
jLabelTest[i][j].setText(strCount);
}
jPanel1.add(jLabelTest[i][j],null);
if(sumOfNotMine==0)
{
isRestart = false;
try
{
theTimeThread.join();
}
catch(InterruptedException ex)
{
JOptionPane.showMessageDialog(null,"InterruptedException occurred here: "+ex);
}
for(int iflag = 0;iflag < sumOfButton ; iflag++)
for(int jflag = 0; jflag < sumOfButton ; jflag++)
{
if(mineButton[iflag][jflag].getEnable())
{
if((mineButton[iflag][jflag].getFlag())&&(mineButton[iflag][jflag].getNumOfClick()%3!=1))
{
mineButton[iflag][jflag].setIcon(imageMine);
/*
sumOfMine--;
if(sumOfMine > 0)
{
if(sumOfMine < 10)
jLabelMineNum.setText("000"+sumOfMine);
else
jLabelMineNum.setText("00"+sumOfMine);
}
else
jLabelMineNum.setText(""+sumOfMine);*/
}
mineButton[iflag][jflag].setEnable(false);
}
}
sumOfMine=0;
jLabelMineNum.setText("0000");
buttonOpen.setIcon(imageWinner);
this.repaint();
JOptionPane.setDefaultLocale(Locale.CHINESE);
JOptionPane.showMessageDialog(null,"You Win!!!");
String newrec = ""+this.theTimeThread.numOfSecond;
if((newrec.length() < this.curRecord.length())||((newrec.length() == this.curRecord.length())&&(newrec.compareTo(curRecord) < 0)))
{
String newName = JOptionPane.showInputDialog(null,"please input your name");
int tempvar = newName.length();
int tempint = newrec.length();
while(newName.length() < 20)
newName = newName + ' ';
while(newrec.length() < 4)
newrec = newrec + ' ';
try
{
this.accessFile.seek(0);
if(this.level.startsWith("primer"))
{
this.accessFile.readLine();
this.accessFile.readLine();
this.accessFile.readLine();
}
else if(this.level.startsWith("middle"))
{
this.accessFile.readLine();
this.accessFile.readLine();
this.accessFile.readLine();
this.accessFile.readLine();
this.accessFile.readLine();
this.accessFile.readLine();
}
else if(this.level.startsWith("high"))
{
this.accessFile.readLine();
this.accessFile.readLine();
this.accessFile.readLine();
this.accessFile.readLine();
this.accessFile.readLine();
this.accessFile.readLine();
this.accessFile.readLine();
this.accessFile.readLine();
this.accessFile.readLine();
}
//write file
accessFile.writeBytes(newName + "\n");
accessFile.writeBytes(newrec + "\n");
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(null,"Exception occurred here: " + ex);
}
}
}
}
else
{
//mineButton[i][j].setIcon(imageMineNotFound);
jPanel1.remove(mineButton[i][j]);
mineButton[i][j].setEnable(false);
jLabelTest[i][j].setIcon(imageBoom);
jPanel1.add(jLabelTest[i][j],null);
isRestart = false;
try
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -