java版数独游戏界面实现(二)
程序员文章站
2024-02-18 18:47:28
本文实例为大家分享了java版数独游戏界面实现的具体代码,供大家参考,具体内容如下
实现效果图:
这里写图片描述
主函数用于启动程序:
package hl...
本文实例为大家分享了java版数独游戏界面实现的具体代码,供大家参考,具体内容如下
实现效果图:
这里写图片描述
主函数用于启动程序:
package hlc.shudu.app; import hlc.shudu.src.shuduhelper; import hlc.shudu.ui.shudumainframe; public class appstart { public static void main(string[] args) { shudumainframe mainframe = new shudumainframe(); mainframe.setvisible(true); } }
主窗体类(包含消息区,时间区,游戏区):
package hlc.shudu.ui; import java.awt.borderlayout; import java.awt.color; import java.awt.font; import java.awt.gridlayout; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.sql.time; import java.text.simpledateformat; import javax.swing.icon; import javax.swing.imageicon; import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.jpanel; import javax.swing.jtextfield; import javax.swing.timer; import javax.swing.border.titledborder; /* * 数独主窗体 */ public class shudumainframe extends jframe { public static int pass = 1; // 关卡 public static jlabel lbpass; // 显示关卡的lable public static long usedtime = 0; // 玩家用时 private shuducanvers panelcanvers; // 主游戏区 public static timer usertimeaction; /* * 默认构造函数 */ public shudumainframe() { // 初始化方法 init(); // 添加组件 addcomponent(); // 添加主游戏区 addcanvers(); } /* * 添加主游戏区 */ private void addcanvers() { panelcanvers = new shuducanvers(); panelcanvers.setborder(new titledborder("游戏区")); // 将主游戏区添加到窗体中 this.add(panelcanvers, borderlayout.center); } /* * 添加组件区 */ private void addcomponent() { jpanel panelcomponent = new jpanel(); // 添加消息区 addpanelmsg(panelcomponent); // 添加时间区 addpaneltime(panelcomponent); // 将组件添加到窗体顶部 this.add(panelcomponent, borderlayout.north); } private void addpaneltime(jpanel panelcomponent) { jpanel paneltime = new jpanel(); paneltime.setborder(new titledborder("时间")); paneltime.setlayout(new gridlayout(2, 1)); final jlabel lbsystime = new jlabel(); final jlabel lbusertime = new jlabel(); paneltime.add(lbsystime, borderlayout.north); paneltime.add(lbusertime, borderlayout.south); // 设置系统时间定时器 timer systimeaction = new timer(500, new actionlistener() { @override public void actionperformed(actionevent e) { long timemillis = system.currenttimemillis(); simpledateformat df = new simpledateformat( "yyyy-mm-dd hh:mm:ss"); lbsystime.settext(" 系统时间: " + df.format(timemillis)); } }); systimeaction.start(); usertimeaction = new timer(1000, new actionlistener() { @override public void actionperformed(actionevent e) { lbusertime.settext(" 您已用时: " + (++usedtime)+ " sec."); } }); usertimeaction.start(); panelcomponent.add(paneltime, borderlayout.east); } /* * 添加消息区 */ private void addpanelmsg(jpanel panelcomponent) { // panelcomponent.setborder(new titledborder("消息区")); panelcomponent.setlayout(new gridlayout(1, 3)); font font14 = new font("", 4, 14); font font28 = new font("", 2, 28); jpanel panelmsg = new jpanel(); panelmsg.setborder(new titledborder("消息区")); jlabel lbpass1 = new jlabel("关卡:第"); lbpass1.setfont(font14); panelmsg.add(lbpass1); // 显示关卡数 lbpass = new jlabel("" + pass); lbpass.setforeground(color.red); lbpass.setfont(font28); panelmsg.add(lbpass); jlabel lbpass2 = new jlabel("关/总共10关"); lbpass2.setfont(font14); panelmsg.add(lbpass2); panelcomponent.add(panelmsg, borderlayout.center); } /* * 界面初始化 */ private void init() { imageicon image = new imageicon("icon/icon.png"); this.seticonimage(image.getimage()); // 设置窗口初始大小 this.setsize(515, 600); // 设置窗口初始位置 this.setlocation(500, 50); // 设置窗口标题 this.settitle("数独游戏(by:侯龙超)"); // 设置窗体不允许改变大小 this.setresizable(false); // 设置默认关闭操作 this.setdefaultcloseoperation(jframe.exit_on_close); } }
游戏区画布:
package hlc.shudu.ui; import hlc.shudu.src.shuduhelper; import java.awt.borderlayout; import java.awt.color; import java.awt.dialog.modalexclusiontype; import java.awt.event.inputevent; import java.awt.event.mouseevent; import java.awt.event.mouselistener; import javax.swing.jdialog; import javax.swing.joptionpane; import javax.swing.jpanel; import javax.swing.border.border; import javax.swing.border.titledborder; public class shuducanvers extends jpanel implements mouselistener { shuducell[][] cells; // 得到数独数组 int[][] maps = new int[9][9]; private selectnumframe selectnum; /* * 默认构造函数 */ public shuducanvers() { shudumainframe.usedtime = 0; maps = shuduhelper.getmap(); // 加载数独区 this.setlayout(null); cells = new shuducell[9][9]; for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { // this.remove(cells[i][j]); // 创建单元格 cells[i][j] = new shuducell(); // 设置位置 cells[i][j].setlocation(20 + i * 50 + (i / 3) * 5, 20 + j * 50 + (j / 3) * 5); if (passrole(shudumainframe.pass)) { cells[i][j].settext("" + maps[i][j]); // 设置背景颜色 cells[i][j].setbackground(getcolor(maps[i][j])); cells[i][j].setenabled(false); cells[i][j].setforeground(color.gray); } else { cells[i][j].addmouselistener(this); } this.add(cells[i][j]); } } checkfinish(); // reloadcanvers(); } /* * 检查是否完成 */ private void checkfinish() { for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { if (!check(i, j)) { return; } } } // 停止用户用时计时器 shudumainframe.usertimeaction.stop(); // 清除所有cell监听 clearalllistener(); // 闯关数加一 shudumainframe.pass += 1; if (shudumainframe.pass > 10) { int o = joptionpane .showconfirmdialog(this, "您已经通关了,是否重头开始?", "", 0); if (o == 1) { system.exit(0); } else { shudumainframe.pass = 1; } } else { joptionpane.showmessagedialog(this, "恭喜你通过本关!用时:" + shudumainframe.usedtime + "秒\n即将进入下一关!"); } // 更新关卡提示 shudumainframe.lbpass.settext("" + shudumainframe.pass); // 开始新的关卡 reloadcanvers(); // 打开用户用时计时器 shudumainframe.usertimeaction.start(); } /* * 检查指定坐标处的单元格 */ private boolean check(int i, int j) { if (cells[i][j].gettext().isempty()) { return false; } for (int k = 0; k < 9; k++) { if (cells[i][j].gettext().trim().equals(cells[i][k].gettext().trim()) && j!=k) { return false; } if (cells[i][j].gettext().trim().equals(cells[k][j].gettext().trim()) && i != k) { return false; } int ii = (i / 3) * 3 + k / 3; int jj = (j / 3) * 3 + k % 3; if (cells[i][j].gettext().trim().equals(cells[ii][jj].gettext().trim()) &&!(i == ii && j == jj)) { return false; } } return true; } /* * 重新加载数独区 */ public void reloadcanvers() { shudumainframe.usedtime = 0; maps = shuduhelper.getmap(); for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { this.remove(cells[i][j]); // 创建单元格 cells[i][j] = new shuducell(); // 设置位置 cells[i][j].setlocation(20 + i * 50 + (i / 3) * 5, 20 + j * 50 + (j / 3) * 5); if (passrole(shudumainframe.pass)) { cells[i][j].settext("" + maps[i][j]); // 设置背景颜色 cells[i][j].setbackground(getcolor(maps[i][j])); cells[i][j].setenabled(false); cells[i][j].setforeground(color.gray); } else { cells[i][j].addmouselistener(this); } this.add(cells[i][j]); } } this.repaint(); checkfinish(); } /* * 根据关卡随机产生该位置是否显示数字 */ private boolean passrole(int pass) { // todo auto-generated method stub return math.random() * 11 > pass; } /* * 根据数字获得颜色 */ private color getcolor(int i) { color color = color.pink; switch (i) { case 1: color = new color(255, 255, 204); break; case 2: color = new color(204, 255, 255); break; case 3: color = new color(255, 204, 204); break; case 4: color = new color(255, 204, 153); break; case 5: color = new color(204, 255, 153); break; case 6: color = new color(204, 204, 204); break; case 7: color = new color(255, 204, 204); break; case 8: color = new color(255, 255, 255); break; case 9: color = new color(153, 255, 153); break; default: break; } return color; } @override public void mouseclicked(mouseevent e) { // todo auto-generated method stub } @override public void mousepressed(mouseevent e) { int modes = e.getmodifiers(); if ((modes & inputevent.button3_mask) != 0) {// 点击鼠标右键 // 清空点击单元格上的内容 ((shuducell) e.getsource()).settext(""); } else if ((modes & inputevent.button1_mask) != 0) {// 点击鼠标左键 // 如果选择数字窗口存在则销毁 if (selectnum != null) { selectnum.dispose(); } // 新建一个选择窗口 selectnum = new selectnumframe(); // 设置成模态窗口 selectnum.setmodal(true); // 设置选择窗口在显示器上的位置 selectnum.setlocation(e.getlocationonscreen().x, e.getlocationonscreen().y); // 将点击的单元格传递给数字选择窗口 selectnum.setcell((shuducell) e.getsource()); // 显示数字选择窗口 selectnum.setvisible(true); } checkfinish(); } /* * 清除所有cell的点击监听 */ private void clearalllistener() { for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { cells[i][j].removemouselistener(this); } } } @override public void mousereleased(mouseevent e) { // todo auto-generated method stub } @override public void mouseentered(mouseevent e) { // todo auto-generated method stub } @override public void mouseexited(mouseevent e) { // todo auto-generated method stub } }
数独单元格:
package hlc.shudu.ui; import java.awt.color; import java.awt.font; import javax.swing.jbutton; public class shuducell extends jbutton { public shuducell(){ this.setsize(50,50); font font = new font("",2,24); this.setfont(font); this.setbackground(new color(255,153,102)); this.setforeground(color.blue); } }
数字选择框:
package hlc.shudu.ui; import java.awt.color; import java.awt.window; import java.awt.event.inputevent; import java.awt.event.mouseevent; import java.awt.event.mouselistener; import javax.swing.jbutton; import javax.swing.jdialog; import javax.swing.jframe; public class selectnumframe extends jdialog implements mouselistener { private shuducell cell; public void setcell(shuducell cell) { this.cell = cell; } public selectnumframe(){ //隐藏界面上面的工具栏 this.setundecorated(true); this.setsize(150, 150); this.setbackground(new color(255,204,153, 123)); this.setlayout(null); addnum(); } //添加数字1~9 private void addnum() { for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { jbutton btn = new jbutton(); btn.setsize(50, 50); btn.setlocation(i*50,j*50); btn.settext(""+(j*3+i+1)); btn.addmouselistener(this); this.add(btn); } } } @override public void mouseclicked(mouseevent e) { // todo auto-generated method stub } @override public void mousepressed(mouseevent e) { int modes = e.getmodifiers(); if ((modes & inputevent.button1_mask) != 0) { jbutton btn = (jbutton) e.getsource(); cell.settext(btn.gettext()); } this.dispose(); } @override public void mousereleased(mouseevent e) { // todo auto-generated method stub } @override public void mouseentered(mouseevent e) { // todo auto-generated method stub } @override public void mouseexited(mouseevent e) { // todo auto-generated method stub } }
完整程序包可在github上下载:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。