十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
1)把记录的东西存到一个ArrayList 里但是退出以后再进入就没了。
创新互联公司基于分布式IDC数据中心构建的平台为众多户提供多线服务器托管 四川大带宽租用 成都机柜租用 成都服务器租用。
2)把记录的东西存到一个txt文件中用特殊的方式存再用特殊的方式取
比如说存的时候用“,”隔开名字和分数,在取的时候用split方法去分割
名字和分数。
3)把名字和分数存到数据库要的时候调用就是了(这个是最好的方法)
至于排行的话重写compareTo方法然后用Collections.sort去排序。
import java.awt.*;
import java.util.*;
import java.io.*;
import javax.swing.*;
import javax.swing.Timer;
import java.awt.event.*;
import javax.swing.border.*;
/**
* pTitle:扫雷/p
*
* pDescription:学JAVA以来做的第一个游戏,程序中可能还有些BUG,希望大家提出来供一起探讨,
* 如果要测试记录文件,可以把雷的数量改的少一点,
* arithmetic中的while(landmintTally99), button_mouseClicked中的
* if((landmineNum-1)==0),有3处,表示还剩的雷数.completeGame中的
* for (int i=0; i99; i++)/p
* pCopyright: Copyright (c) 2006/p
*
* pCompany: private /p
*
* @author cqp
* @version demo
*/
public class shaolei extends JFrame {
/**类的属性和控件实例化*/
ImageIcon ButtonIcon; //按钮的图片;
HashMap map = new HashMap(); //雷和数字的状态,键为位置(0-479),值为状态,0-6为数字,7为雷;
HashMap flag_landmine = new HashMap(); //按钮上打的标记,如问号,对勾和取消,8为标记雷,9为问号,10为默认值空;
JMenuBar file = new JMenuBar(); //菜单栏;
JMenu game = new JMenu(); //菜单按钮;
JMenuItem start = new JMenuItem(); //菜单项;
JMenuItem record = new JMenuItem(); //菜单项;
JMenuItem quit = new JMenuItem(); //菜单项;
JMenuItem clearReocrd = new JMenuItem();//菜单项;
JMenu help = new JMenu(); //菜单按钮;
JButton[] cardsBtn = new JButton[480]; //480个按钮;
JButton beginBtn = new JButton(); //开始按钮;
JPanel pane = new JPanel(); //雷区面板;
JPanel paneTime = new JPanel(); //记数器所在的面板;
JOptionPane saveRecord = new JOptionPane(); //保存记录对话框;
JTextField landmineTally = new JTextField("99");//所剩雷的计数器;
JTextField timeTally = new JTextField("0"); //时间计数器;
GridLayout gridLayout1 = new GridLayout(); //网格布局;
Timer timer; //线程设施;
String[] landmine = new String[99]; //存放雷的位置,用来判断雷的位置是否重复;
slFrame_button_actionAdatper[] buttonClick =new slFrame_button_actionAdatper[480];//雷区按钮的事件类;
int mouseKey=0; //得到鼠标先按下的哪个键,用来判断鼠标是否同时按下了左右键;
int timeCount = 0; //时间计数器;
/**构造方法*/
public shaolei() {
try {
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}
/**界面设置*/
private void jbInit() throws Exception {
getContentPane().setLayout(null);
this.setJMenuBar(file);
game.setText("游戏");
start.setText("开局");
start.addActionListener(new slFrame_start_actionAdapter(this));
record.setText("排行榜");
record.addActionListener(new slFrame_record_actionAdapter(this));
quit.setText("退出");
quit.addActionListener(new slFrame_quit_actionAdapter(this));
help.setText("帮助");
clearReocrd.setText("清除记录");
clearReocrd.addActionListener(new slFrame_clearReocrd_actionAdapter(this));
landmineTally.setBounds(new Rectangle(5, 5, 40, 25));
landmineTally.setBackground(new Color(0,0,0));
landmineTally.setForeground(new Color(255,0,0));
landmineTally.setFont(new java.awt.Font("Times New Roman", Font.BOLD, 20));
landmineTally.setBorder(BorderFactory.createBevelBorder(1));
landmineTally.setEditable(false);
timeTally.setBounds(new Rectangle(520, 5, 50, 25));
timeTally.setBackground(new Color(0,0,0));
timeTally.setForeground(new Color(255,0,0));
timeTally.setHorizontalAlignment(4);
timeTally.setFont(new java.awt.Font("Times New Roman", Font.BOLD, 20));
timeTally.setBorder(BorderFactory.createBevelBorder(0));
timeTally.setEditable(false);
beginBtn.setBounds(new Rectangle(250, 5, 25, 25));
beginBtn.setBorder(BorderFactory.createBevelBorder(0));
beginBtn.addActionListener(new slFrame_beginBtn_actionAdatper(this));
beginBtn.setIcon(createImageIcon("images/laugh.jpg"));
paneTime.setBounds(new Rectangle(0, 0, 585, 35));
paneTime.setBorder(BorderFactory.createEtchedBorder());
paneTime.setLayout(null);
paneTime.add(landmineTally);
paneTime.add(timeTally);
paneTime.add(beginBtn);
pane.setBounds(new Rectangle(0, 35, 590, 320));
pane.setLayout(gridLayout1);
gridLayout1.setColumns(30);
gridLayout1.setRows(16);
file.add(game);
file.add(help);
game.add(start);
game.add(record);
game.add(quit);
help.add(clearReocrd);
this.getContentPane().add(pane);
this.getContentPane().add(paneTime);
ActionListener listener = new ActionListener(){ //自定义线程
public void actionPerformed(ActionEvent e){
timeCount++;
timeTally.setText(Integer.toString(timeCount));
}
};
timer = new Timer(1000, listener); //增加线程,并每1秒执行一次;
for (int i=0;i480;i++) //实例化480个小按钮加到面板pane中
{
cardsBtn[i] = new JButton();
cardsBtn[i].setText(""); //按钮上的文字去掉;
cardsBtn[i].setBorder(null); //按钮的边框去掉;
pane.add(cardsBtn[i]);
}
}
/**主方法*/
public static void main(String[] args) {
shaolei frame = new shaolei();
frame.setSize(580,410);
frame.setTitle("扫雷");
frame.show();
frame.setResizable(false); //不能修改窗体大小
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //点关闭按钮时直
}
/**自定义方法,用来给按钮增加图片*/
protected static ImageIcon createImageIcon(String path){
java.net.URL imgURL = shaolei.class.getResource(path);
if (imgURL != null) {
return new ImageIcon(imgURL);
} else {
System.err.println("Couldn't find file: " + path);
return null;
}
}
/**菜单按钮的事件,开始游戏*/
public void start_actionPerformed(ActionEvent e) {
start(); //初始化;
arithmetic(); //计算雷的位置;
calculate(); //计算雷的分布情况;
timer.start(); //时间线程开始;
}
/**开始游戏按钮的事件*/
public void beginBtn_mouseClicked(ActionEvent e){
start_actionPerformed(e); //直接调用菜单的事件;
}
/**自定义方法,游戏从这里开始,方法里对按钮的属性和状态进行初始化;*/
void start(){
timeCount=0; //时间从0开始;
landmineTally.setText("99");//所剩雷数为99;
for (int i = 0; i480; i++){
cardsBtn[i].setIcon(null); //清除按钮上的图片;
map.put( Integer.toString(i),Integer.toString(10)); //分布状态全为10,表示为空;
flag_landmine.put( Integer.toString(i),Integer.toString(10)); //标记状态全为10;
cardsBtn[i].removeMouseListener(buttonClick[i]); //去除雷区所有按钮的鼠标事件;
}
}
/**自定义方法,用来计算雷的分布位置*/
void arithmetic(){
Calendar time = Calendar.getInstance(); //日历类,得到当前时间;
int leed = time.get(Calendar.SECOND); //得到当前时间的秒;
Random rand = new Random(leed); //把秒数当个随机数的种子;
int tempRand; //临时随机数;
int landmintTally=0; //得到多少雷的计数器;
boolean flag=false; //标记是否重复;
int tempNum;
while(landmintTally 99){ //最多只能有99颗雷;
tempRand = (int)(rand.nextFloat()*480); //得随机数;
tempNum = Integer.parseInt(map.get(Integer.toString(tempRand)).toString());
if (tempNum == 7) continue; //如果重复执行一个数字;
landmine[landmintTally] = Integer.toString(tempRand); //把得到的位置放进字符串;
map.put(Integer.toString(tempRand),Integer.toString(7)); //把得到的位置放到map集合里,值为7,表示有雷;
landmintTally++; //计数器加1;
}
}
/**计算雷的分部情况,指一个按钮周围有多少雷;*/
void calculate()
{
int num; //按钮的状态;
int sum=0; //计数器,计算周围有几颗雷;
int leftUp, up, rightUp, left, right, leftDown, down, rightDown; //定义了8个位置
for (int i = 0; i480; i++)
{
leftUp = i-31;
up = i-30;
rightUp = i-29;
left = i-1;
right = i+1;
leftDown = i+29;
down = i+30;
rightDown= i+31;
cardsBtn[i].setBorder(BorderFactory.createBevelBorder(0)); //设置按钮的边框样式;
buttonClick[i] = new slFrame_button_actionAdatper(this,i); //实例化事件类;
cardsBtn[i].addMouseListener(buttonClick[i]); //给当前按钮添加鼠标事件;
num = Integer.parseInt(map.get(Integer.toString(i)).toString());//得到当前按钮的状态;
if (num == 7){
continue; //如果这个按钮的状态为雷,跳到下个按钮;
}
if (i == 0) { //左上角第一颗雷;
num = Integer.parseInt(map.get(Integer.toString(i)).toString());
if ( Integer.parseInt(map.get(Integer.toString(right)).toString()) == 7 ) sum++; //如果是雷计数器加1;
if ( Integer.parseInt(map.get(Integer.toString(down)).toString()) == 7 ) sum++;
if ( Integer.parseInt(map.get(Integer.toString(rightDown)).toString()) == 7 ) sum++;
map.put(Integer.toString(0),Integer.toString(sum)); //把得到的数字放到当前的位置;
sum=0; //计数器清零;
continue; //下个按钮;
}else if (i == 29) { //右上角第一颗雷;
if ( Integer.parseInt(map.get(Integer.toString(left)).toString()) == 7 ) sum++;
if ( Integer.parseInt(map.get(Integer.toString(leftDown)).toString()) == 7 ) sum++;
if ( Integer.parseInt(map.get(Integer.toString(down)).toString()) == 7 ) sum++;
map.put(Integer.toString(i),Integer.toString(sum));
sum=0;
continue;
}else if (i == 450) { //左下角第一颗雷;
if ( Integer.parseInt(map.get(Integer.toString(right)).toString()) == 7 ) sum++;
if ( Integer.parseInt(map.get(Integer.toString(up)).toString()) == 7 ) sum++;
if ( Integer.parseInt(map.get(Integer.toString(rightUp)).toString()) == 7 ) sum++;
map.put(Integer.toString(i),Integer.toString(sum));
sum=0;
continue;
}else if (i == 479) { //右下角第一颗雷;
if ( Integer.parseInt(map.get(Integer.toString(left)).toString()) == 7 ) sum++;
if ( Integer.parseInt(map.get(Integer.toString(leftUp)).toString()) == 7 ) sum++;
if ( Integer.parseInt(map.get(Integer.toString(up)).toString()) == 7 ) sum++;
map.put(Integer.toString(i),Integer.toString(sum));
sum=0;
return;
}else if (i29){ //第一行;
if ( Integer.parseInt(map.get(Integer.toString(left)).toString()) == 7 ) sum++;
if ( Integer.parseInt(map.get(Integer.toString(leftDown)).toString()) == 7 ) sum++;
if ( Integer.parseInt(map.get(Integer.toString(down)).toString()) == 7 ) sum++;
if ( Integer.parseInt(map.get(Integer.toString(right)).toString()) == 7 ) sum++;
if ( Integer.parseInt(map.get(Integer.toString(rightDown)).toString()) == 7 ) sum++;
map.put(Integer.toString(i),Integer.toString(sum));
sum=0;
continue;
}else if (i450){ //最后一行;
if ( Integer.parseInt(map.get(Integer.toString(leftUp)).toString()) == 7 ) sum++;
if ( Integer.parseInt(map.get(Integer.toString(up)).toString()) == 7 ) sum++;
if ( Integer.parseInt(map.get(Integer.toString(rightUp)).toString()) == 7 ) sum++;
if ( Integer.parseInt(map.get(Integer.toString(left)).toString()) == 7 ) sum++;
if ( Integer.parseInt(map.get(Integer.toString(right)).toString()) == 7 ) sum++;
map.put(Integer.toString(i),Integer.toString(sum));
sum=0;
continue;
}else if ( (i%30) == 0 ){ //第一列;
if ( Integer.parseInt(map.get(Integer.toString(up)).toString()) == 7 ) sum++;
if ( Integer.parseInt(map.get(Integer.toString(rightUp)).toString()) == 7 ) sum++;
if ( Integer.parseInt(map.get(Integer.toString(right)).toString()) == 7 ) sum++;
if ( Integer.parseInt(map.get(Integer.toString(down)).toString()) == 7 ) sum++;
if ( Integer.parseInt(map.get(Integer.toString(rightDown)).toString()) == 7 ) sum++;
map.put(Integer.toString(i),Integer.toString(sum));
sum=0;
continue;
}else if ( ((i+1)%30) == 0 ){ //最后一列;
if ( Integer.parseInt(map.get(Integer.toString(leftUp)).toString()) == 7 ) sum++;
if ( Integer.parseInt(map.get(Integer.toString(up)).toString()) == 7 ) sum++;
if ( Integer.parseInt(map.get(Integer.toString(left)).toString()) == 7 ) sum++;
if ( Integer.parseInt(map.get(Integer.toString(leftDown)).toString()) == 7 ) sum++;
if ( Integer.parseInt(map.get(Integer.toString(down)).toString()) == 7 ) sum++;
map.put(Integer.toString(i),Integer.toString(sum));
sum=0;
continue;
}else{ //除去四周剩下的;
if ( Integer.parseInt(map.get(Integer.toString(leftUp)).toString()) == 7 ) sum++;
if ( Integer.parseInt(map.get(Integer.toString(up)).toString()) == 7 ) sum++;
if ( Integer.parseInt(map.get(Integer.toString(rightUp)).toString()) == 7 ) sum++;
if ( Integer.parseInt(map.get(Integer.toString(left)).toString()) == 7 ) sum++;
if ( Integer.parseInt(map.get(Integer.toString(right)).toString()) == 7 ) sum++;
if ( Integer.parseInt(map.get(Integer.toString(leftDown)).toString()) == 7 ) sum++;
if ( Integer.parseInt(map.get(Integer.toString(down)).toString()) == 7 ) sum++;
if ( Integer.parseInt(map.get(Integer.toString(rightDown)).toString()) == 7 ) sum++;
map.put(Integer.toString(i),Integer.toString(sum));
sum=0;
continue;
}
}
}
/**鼠标点击事件,参数i为点击按钮的位置 */
public void button_mouseClicked(MouseEvent e,int i){
int mKey = e.getButton(); //点击的哪个键;
int landmineNum = Integer.parseInt(landmineTally.getText().toString()); //所剩的雷数;
int num = Integer.parseInt(map.get(Integer.toString(i)).toString()); //当前按钮的状态;
int flag = Integer.parseInt(flag_landmine.get(Integer.toString(i)).toString());//当前按钮的标记状态;
if ( (mKey == 3) ( cardsBtn[i].getBorder()!= null)){ //点击为鼠标右键,并且边框不为空(空的表示已按亮开的);
if (flag == 10){ //如果没有标记,则改为标记状态;
flag_landmine.put(Integer.toString(i),Integer.toString(8));
ButtonIcon = createImageIcon("images/8.jpg");
cardsBtn[i].setIcon(ButtonIcon);
landmineTally.setText( Integer.toString(landmineNum - 1) );
if ( (landmineNum-1) == 0) //如果标记的雷数为99;
completeGame(); //完成游戏;
}else if (flag == 8){ //如果为标记状态,则改为问号;
flag_landmine.put(Integer.toString(i),Integer.toString(9));
ButtonIcon = createImageIcon("images/9.jpg");
cardsBtn[i].setIcon(ButtonIcon);
landmineTally.setText( Integer.toString(landmineNum + 1) );
if ( (landmineNum+1) == 0) //如果标记的雷数为99;
completeGame(); //完成游戏;
}else if (flag == 9){ //如果为问号,则取消标记;
flag_landmine.put(Integer.toString(i),Integer.toString(10));
cardsBtn[i].setIcon(null);
}
}else if (mKey == 1){ //如果点击为鼠标左键;
flag_landmine.put(Integer.toString(i),Integer.toString(10)); //先清除所点击按钮的标记状态;
if ( (landmineNum+1) == 0) //如果标记的雷数为99;
completeGame(); //完成游戏;
if (num == 7){ //如果铵钮的状态为雷,则结束游戏;
overGame(i);
}else if (num == 0){ //如果雷数为空
if ( flag == 8 ){ //如果已经标记为雷,计数器加1;
landmineTally.setText( Integer.toString(landmineNum + 1) );
}
ButtonIcon = createImageIcon("images/0.jpg");
cardsBtn[i].setIcon(ButtonIcon);
cardsBtn[i].setBorder(null);
display(i); //亮开周围的按钮;
}else { //数字为1-6之间,亮开按钮,并显示数字所对应的图片;
if ( flag == 8 ){ //如果已经标记为雷,计数器加1;
landmineTally.setText( Integer.toString(landmineNum + 1) );
}
ButtonIcon = createImageIcon("images/"+num+".jpg");
cardsBtn[i].setIcon(ButtonIcon);
cardsBtn[i].setBorder(null);
}
}
if ( (mouseKey==1 mKey == 3) || (mouseKey==3 mKey == 1) ){ //鼠标左右键同时点按下;
open(i); //亮开周围的按钮(先判断);
}
mouseKey = 0;
}
/**自定义方法,用来判断是否要亮开周围的按钮*/
void open(int i){
int landmineAmount = 0; //实际的雷数;
int flagAmount=0; //标记的雷数;
int landmine_leftUp=0, landmine_up=0, landmine_rightUp=0, landmine_left=0, landmine_right=0,
landmine_leftDown=0, landmine_down=0, landmine_rightDown=0; //定义了实际雷的8个位置
int flag_leftUp=0, flag_up=0, flag_rightUp=0, flag_left=0, flag_right=0,
flag_leftDown=0, flag_down=0, flag_rightDown=0; //定义了标记雷的8个位置
//实际雷所在的8个位置和标记雷的8个位置,如果不加判断则hashMap集合会越界;
if (i 31) landmine_leftUp = Integer.parseInt(map.get(Integer.toString(i-31)).toString());
if (i 30) landmine_up = Integer.parseInt(map.get(Integer.toString(i-30)).toString());
if (i 29) landmine_rightUp = Integer.parseInt(map.get(Integer.toString(i-29)).toString());
if (i 1) landmine_left = Integer.parseInt(map.get(Integer.toString(i-1)).toString());
if (i 479) landmine_right = Integer.parseInt(map.get(Integer.toString(i+1)).toString());
if (i 450) landmine_leftDown = Integer.parseInt(map.get(Integer.toString(i+29)).toString());
if (i 449) landmine_down = Integer.parseInt(map.get(Integer.toString(i+30)).toString());
if (i 448) landmine_rightDown = Integer.parseInt(map.get(Integer.toString(i+31)).toString());
if (i 31) flag_leftUp = Integer.parseInt(flag_landmine.get(Integer.toString(i-31)).toString());
if (i 30) flag_up = Integer.parseInt(flag_landmine.get(Integer.toString(i-30)).toString());
if (i 29) flag_rightUp = Integer.parseInt(flag_landmine.get(Integer.toString(i-29)).toString());
if (i 1) flag_left = Integer.parseInt(flag_landmine.get(Integer.toString(i-1)).toString());
if (i 479) flag_right = Integer.parseInt(flag_landmine.get
太长了写不完,我把压缩包发给你吧,49905518注意查收
JOptionPane就可以了,不必自己再设计类了。
你运行一下我下面的代码段就知道了。
import javax.swing.JOptionPane;
import javax.swing.JDialog;
import javax.swing.JButton;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class MultiplyTable1{
public static void main(String[] args){
JOptionPane pane=new JOptionPane(String.format("初级:%1$3d秒%2$20s\n中级:%3$3d秒%4$20s\n高级:%5$3d秒%6$20s",999,"匿名",999,"匿名",999,"匿名"));
JButton btnRecount,btnOK;
btnRecount=new JButton("重新计分(R)");
btnRecount.setMnemonic('R');
btnOK=new JButton("确定");
pane.setOptions(new JButton[]{btnRecount,btnOK});
final JDialog dlgHeroList=pane.createDialog(null,"扫雷英雄榜");
btnRecount.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
dlgHeroList.dispose();
//重新计算
}
});
btnOK.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
dlgHeroList.dispose();
}
});
dlgHeroList.setVisible(true);
}
}