网站开发的选择是什么,福州短视频seo方法,wordpress流量赚钱,深圳电商代运营公司排名文章目录 main方法登录界面游戏主界面User类图片素材获取方式 基于Java实现的拼图小游戏#xff0c;代码与图片素材来自b站up主黑马程序员的Java教学视频。
main方法
import ui.LoginJframe;public class App {public static void main(String[] args) {new LoginJframe(); … 文章目录 main方法登录界面游戏主界面User类图片素材获取方式 基于Java实现的拼图小游戏代码与图片素材来自b站up主黑马程序员的Java教学视频。
main方法
import ui.LoginJframe;public class App {public static void main(String[] args) {new LoginJframe(); //创建登陆界面}
} 登录界面
package ui;import javax.swing.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;//游戏登录界面
public class LoginJframe extends JFrame implements MouseListener {//创建全集一个用户集合用于登录判断static ArrayListUser listnew ArrayList();static String userName;//添加用户集合成员用于登录操作判断static{list.add(new User(wuhu,yahu));}//切换验证码按键JButton Switch_Captchanew JButton();//设置注册按钮JButton enrollJButtonnew JButton();//设置登录按钮JButton loginJButton new JButton();//getCaptcha为验证码获取方法传递的数据为需要返回的验证码的长度String str_CaptchagetCaptcha(5);/*登录与注册按钮的图片文件位置*/String Image_enrolluntitled/image/login/注册按钮.png;String Image_loginuntitled/image/login/登录按钮.png;//显示用户名输入框JTextField NameFramenew JTextField();//添加验证码文本框JTextField FrameCaptchanew JTextField();//密码输入显示框JTextField PasswordFramenew JTextField();//创建一个弹框对象JDialog Error;//登录界面的空参构造public LoginJframe(){initJFrame(); //界面初始化项目initView(); //界面元素添加//绑定监听enrollJButton.addMouseListener(this);loginJButton.addMouseListener(this);Switch_Captcha.addMouseListener(this);//将界面状态设置为显示this.setVisible(true);}//界面初始化类public void initView(){//清空界面this.getContentPane().removeAll();//显示用户名JLabel UsrtName new JLabel(new ImageIcon(untitled/image/login/用户名.png));UsrtName.setBounds(100,105,47,17);this.getContentPane().add(UsrtName);//配置用户名输入框NameFrame.setBounds(160,100,200,30);this.getContentPane().add(NameFrame);//显示密码JLabel UsrtPasswordnew JLabel(new ImageIcon(untitled/image/login/密码.png));UsrtPassword.setBounds(100,155,47,17);this.getContentPane().add(UsrtPassword);//配置密码框参数PasswordFrame.setBounds(160,150,200,30);this.getContentPane().add(PasswordFrame);//添加验证码JLabel CaptchaTxTnew JLabel(new ImageIcon(untitled/image/login/验证码.png));CaptchaTxT.setBounds(100,215,47,17);this.getContentPane().add(CaptchaTxT);//配置验证码文本框参数FrameCaptcha.setBounds(160,210,100,30);this.getContentPane().add(FrameCaptcha);//验证码显示System.out.println(str_Captcha);JLabel Captchanew JLabel(str_Captcha);Captcha.setBounds(280,215,50,20);this.getContentPane().add(Captcha);//设置验证码按钮Switch_Captcha.setBounds(275,215,50,20);Switch_Captcha.setBorderPainted(false);Switch_Captcha.setContentAreaFilled(false);this.getContentPane().add(Switch_Captcha);//配置登录按钮loginJButton.setIcon(new ImageIcon(Image_login));loginJButton.setBorderPainted(false); //去除按钮的默认边框loginJButton.setContentAreaFilled(false); //去除按钮的默认背景loginJButton.setBounds(123,310,128,47); //设置位置与大小this.getContentPane().add(loginJButton);//配置注册按钮enrollJButton.setIcon(new ImageIcon(Image_enroll));enrollJButton.setBorderPainted(false); //去除按钮的默认边框enrollJButton.setContentAreaFilled(false); //去除按钮的默认背景enrollJButton.setBounds(270,310,128,47); //设置位置与大小this.getContentPane().add(enrollJButton);// 添加背景图片//设置一个JLabel对象进行管理图片JLabel background_JLabelnew JLabel(new ImageIcon(untitled/image/register/background.png));//设置图片的位置与宽高background_JLabel.setBounds(0,0,470,390);//将容器添加到界面当中this.getContentPane().add(background_JLabel);//刷新界面this.getContentPane().repaint();}//获取验证码方法 n 为需要返回的验证码长度public static String getCaptcha(int len){Random rnew Random(); //设置随机方法种子StringBuilder strnew StringBuilder();for (int i 0; i len-1; i) {int numr.nextInt(58)65; //字母范围的数值//跳过ascii表中的其他字符if(num 90 num 97){num7;}str.append((char)num); //将数值强制转换为字母并添加到字符串中}//将数字添加到字符串中int numberr.nextInt(10);str.append(number);//转换为字符数组更好进行乱序的操作char []arr str.toString().toCharArray();//将其数组元素顺序打乱for (int i 0; i arr.length; i) {int indexr.nextInt(arr.length);char temparr[i];arr[i]arr[index];arr[index]temp;}//将完成的验证码转换为字符串将其返回return new String(arr);}//注册界面初始化方法public void initJFrame(){//设置界面的宽高this.setSize(488,430);//设置界面的标题this.setTitle( 登录界面 );//设置界面为图层顶置this.setAlwaysOnTop(true);//设置界面为初始化居中显示this.setLocationRelativeTo(null);//设置关闭模式 3 代表当前界面被关闭后程序结束this.setDefaultCloseOperation(3);}/*事件监听*///鼠标松开后执行Overridepublic void mouseClicked(MouseEvent e) {System.out.println(松开后);Object obje.getSource(); //获取点击是哪一个按键//登录按钮点击后的操作if(loginJButton obj){Image_login(untitled/image/login/登录按钮.png);initView();//界面显示方法//获取用户名、密码、验证码框内的内容String getNameNameFrame.getText();String getPasswodrPasswordFrame.getText();String getCaptchaFrameCaptcha.getText();//判断三个文本框内是否都有输入数据if((0 getName.length()) || (0 getPasswodr.length()) || (0 getCaptcha.length())){showJDialog(输入有误请检查是否已全部输入);return;}else if(!(str_Captcha.equals(getCaptcha))){showJDialog(验证码错误请正确输入);str_CaptchagetCaptcha(5);initView();//界面显示方法}/*judgmentUser方法作用为判断输入的用户名与密码是否为正确的* 返回值为 bool类型正确返回true错误返回false* */else if ( judgmentUser(getName,getPasswodr) ){showJDialog(登录成功欢迎进入游戏);this.dispose(); //关闭当前界面userNamegetName;new GameJframe(); //打开游戏界面}else {showJDialog(用户名或密码错误);return;}}//注册按钮点击后的操作else if (enrollJButton obj) {Image_enroll(untitled/image/login/注册按钮.png);showJDialog(功能未完善敬请期待。);}//验证码显示范围地区被点击后else if (Switch_Captcha obj) {str_CaptchagetCaptcha(5); //传入的形参为返回的长度initView();//界面显示方法}/*判断整个弹窗对象是否被点击*/else if (Error obj) {//将生成的弹窗关闭Error.dispose();} else {;}}//按键按着不松时Overridepublic void mousePressed(MouseEvent e) {/*主要作用为当按键松开后将按钮恢复为初始状态*/Object obje.getSource(); //获取点击是哪一个按键if(loginJButton obj){Image_login(untitled/image/login/登录按下.png);//界面显示方法initView();} else if (enrollJButton obj) {Image_enroll(untitled/image/login/注册按下.png);//界面显示方法initView();}else {;}}//鼠标点击后松开执行Overridepublic void mouseReleased(MouseEvent e) {}//鼠标滑入监听Overridepublic void mouseEntered(MouseEvent e) {}//鼠标滑出监听Overridepublic void mouseExited(MouseEvent e) {}//判断用户名与密码是否相等方法public boolean judgmentUser(String strName,String strPassword){for (int i 0; i list.size(); i) {if( list.get(i).getName().equals(strName) ){if( list.get(i).getPassword().equals(strPassword) ){return true;}return false;}}return false;}//弹框显示public void showJDialog(String display){//创建一个弹框的对象显示形参displayErrornew JDialog();//设置弹框的大小Error.setSize(200,150);JLabel datanew JLabel(display);data.setBounds(0,0,200,150);Error.getContentPane().add(data);Error.setTitle(提示);//将弹框设置为界面顶置Error.setAlwaysOnTop(true);//初始位置为居中显示Error.setLocationRelativeTo(null);//将弹框设置为不关闭弹框无法操作其他界面Error.setModal(true);//绑定监听Error.addMouseListener(this);//将弹框显示方式设置为显示Error.setVisible(true);}
} 游戏主界面
package ui;import javax.swing.*;
import javax.swing.border.BevelBorder;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;//游戏主界面
public class GameJframe extends JFrame implements KeyListener, ActionListener {int [][] PosArrnew int[4][4];int [][] winnew int[][]{{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,0}};String pathuntitled\\image\\animal\\animal3\\;int step0; //计步器//记录空白方块在二维数组中的位置int x0,y0;//条目JMenuItem accountItemnew JMenuItem(支持我们(手动狗头));JMenuItem promptnew JMenuItem(提示);JMenuItem replayItemnew JMenuItem(重新游戏);JMenuItem reLoginItemnew JMenuItem(重新登录);JMenuItem closeItemnew JMenuItem(关闭游戏);JMenuItem BeautyPinew JMenuItem(美女);JMenuItem AnimalPinew JMenuItem(动物);JMenuItem MotionPinew JMenuItem(运动);JMenuItem AboutUsernew JMenuItem(关于用户);//创建一个主界面public GameJframe(){//初始化界面initJFrame();//初始化菜单initJMenuBar();//初始化并打乱二维数组中的下标ArrRandom();//初始化图片initImage();//将界面状态设置为显示this.setVisible(true);}//初始化图片private void initImage() {//清空所有图片this.getContentPane().removeAll();JLabel stepJlbelnew JLabel(步数step);stepJlbel.setBounds(50,30,100,20);this.getContentPane().add(stepJlbel);//判断数组是否顺序相同相同则代表图片位置同意一样if(OrderJudgment()){JLabel winJlabelnew JLabel(new ImageIcon(untitled/image/win.png));winJlabel.setBounds(203,283,197,73);this.getContentPane().add(winJlabel);}for(int i0; i4; i){for(int j0; j4; j){int indexPosArr[i][j];ImageIcon icon new ImageIcon(path(index).jpg);JLabel jLabel new JLabel(icon);jLabel.setBounds(105*j83,105*i134,105,105);jLabel.setBorder(new BevelBorder(BevelBorder.LOWERED));this.getContentPane().add(jLabel);}}//添加背景ImageIcon backgroundnew ImageIcon(untitled\\image\\background.png);JLabel dataJlabelnew JLabel(background);dataJlabel.setBounds(40,40,508,560);this.getContentPane().add(dataJlabel);//刷新界面this.getContentPane().repaint();}//数组随机public void ArrRandom() {Random rnew Random();int []tmparr{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,0};for (int i 0; i tmparr.length; i) {int indexr.nextInt(tmparr.length);int temptmparr[index];tmparr[index]tmparr[i];tmparr[i]temp;}for (int i 0; i tmparr.length; i) {if(tmparr[i] 0){xi/4;yi%4;}PosArr[i/4][i%4]tmparr[i];}}//菜单设置private void initJMenuBar() {//初始化菜单JMenuBar jMenuBar new JMenuBar();JMenu functionJMenunew JMenu(功能);JMenu handoffPinew JMenu(切换图片);JMenu aboutJMenunew JMenu(关于);//添加functionJMenu.add(handoffPi);functionJMenu.add(replayItem);functionJMenu.add(reLoginItem);functionJMenu.add(closeItem);//将功能添加进切换图片这个项当中handoffPi.add(BeautyPi);handoffPi.add(AnimalPi);handoffPi.add(MotionPi);//关于此项添加aboutJMenu.add(accountItem);//给条目绑定事件//重新游戏replayItem.addActionListener(this);//重新登录reLoginItem.addActionListener(this);//关闭游戏closeItem.addActionListener(this);//关于我们accountItem.addActionListener(this);//提示prompt.addActionListener(this);//关于用户AboutUser.addActionListener(this);//更换图片功能绑定条目//美女、动物、运动BeautyPi.addActionListener(this);AnimalPi.addActionListener(this);MotionPi.addActionListener(this);//将其添加到大菜单当中jMenuBar.add(functionJMenu);jMenuBar.add(aboutJMenu);jMenuBar.add(prompt);jMenuBar.add(AboutUser);this.setJMenuBar(jMenuBar); //给界面设置菜单}//界面初始化private void initJFrame() {//设置界面长宽this.setSize(603,680);this.setTitle(拼图游戏单机版 V1.0);this.setAlwaysOnTop(true);this.setLocationRelativeTo(null);//设置关闭模式this.setDefaultCloseOperation(3);this.setLayout(null);//给整个界面添加键盘监听this.addKeyListener(this);}//判断两个数组数据是否相同public boolean OrderJudgment(){for (int i 0; i PosArr.length; i) {for (int j 0; j PosArr[i].length; j) {if(PosArr[i][j] ! win[i][j]){return false;}}}return true;}//无内容Overridepublic void keyTyped(KeyEvent e) {}//按键按着不松时执行Overridepublic void keyPressed(KeyEvent e) {//获取当前按键的键值int code e.getKeyCode();//判断是否 X 按键被按下 x的键盘值就是88if (code 88) {//清空图片this.getContentPane().removeAll();//显示完整图片ImageIcon Fullmapnew ImageIcon(path\\all.jpg);JLabel dataJlabelnew JLabel(Fullmap);dataJlabel.setBounds(83,134,420,420);this.getContentPane().add(dataJlabel);//添加背景JLabel allnew JLabel(new ImageIcon(untitled\\image\\background.png));all.setBounds(40,40,508,560);this.getContentPane().add(all);//刷新界面this.getContentPane().repaint();}}//按键松开时执行Overridepublic void keyReleased(KeyEvent e) {//对 上 下 左 右 的操作进行判断// 38 40 37 39int codee.getKeyCode(); //获取键盘按键的键值if(code 37 y ! 0){PosArr[x][y]PosArr[x][y-1];PosArr[x][y-1]0;y--;//初始化图片initImage();System.out.println(向左移动);step;}else if (code 38 x ! 0) {PosArr[x][y]PosArr[x-1][y];PosArr[x-1][y]0;x--;//初始化图片initImage();System.out.println(向上移动);step;}else if (code 39 y ! 3) {PosArr[x][y]PosArr[x][y1];PosArr[x][y1]0;y;//初始化图片initImage();System.out.println(向右移动);step;}else if (code 40 x! 3) {PosArr[x][y]PosArr[x1][y];PosArr[x1][y]0;x;//初始化图片initImage();System.out.println(向下移动);step;}//按下 X 显示全图else if (code 88) {//初始化图片initImage();}//按下 A 键则执行一键通关else if (code 65) {PosArr new int[][]{{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,0},};xy3; //将空白位置的坐标记录下来//初始化图片initImage();}//按下 C 键图片重新打乱重新游戏else if (code 67) {step0; //计步器归零//将二维数组重新打乱以达到重新将图片打乱的效果ArrRandom();//初始化图片initImage();System.out.println(重新游戏);}else{return;}}//鼠标动作监听Overridepublic void actionPerformed(ActionEvent e) {Object obje.getSource();//重新游戏if(obj replayItem){step0; //计步器归零//将二维数组重新打乱以达到重新将图片打乱的效果ArrRandom();//初始化图片initImage();System.out.println(重新游戏);}//重新登录else if (obj reLoginItem) {this.dispose(); //关闭当前界面new LoginJframe(); //打开登录界面System.out.println(重新登录);}//关闭游戏else if (obj closeItem) {//关闭虚拟机已到达退出游戏的效果System.exit(0);System.out.println(关闭游戏);}//收款码弹窗显示else if(obj accountItem){//创建一个弹框对象JDialog jDialog new JDialog();//创建一个图片对象并将其添加到管理容器当中JLabel jLabelnew JLabel(new ImageIcon(untitled\\image\\收款码.png));//设置图片的位置与宽高jLabel.setBounds(0,0,516,506);//将管理容器添加到弹框中jDialog.getContentPane().add(jLabel);//设置弹框的大小jDialog.setSize(600,600);//设置弹框为界面顶置jDialog.setAlwaysOnTop(true);//设置弹框为初始居中显示jDialog.setLocationRelativeTo(null);//将弹框设置为未关闭则无法操作其他界面jDialog.setModal(true);//将弹框设置为显示jDialog.setVisible(true);System.out.println(关于我们);}//美女、动物、与运动等图片的切换else if (obj BeautyPi) {System.out.println(美女图片);Random rnew Random();int indexr.nextInt(10)1;//随机图片下标pathnew String(untitled\\image\\girl\\girl(index)\\);step0; //计步器归零ArrRandom();//生成图片initImage();} else if (obj AnimalPi) {System.out.println(动物图片);Random rnew Random();int indexr.nextInt(8)1;//随机图片下标pathnew String(untitled/image/animal/animal(index)\\);step0; //计步器归零ArrRandom(); //数组乱序initImage(); //生成图片} else if (obj MotionPi) {System.out.println(运动图片);Random rnew Random();int indexr.nextInt(10)1; //随机图片下标pathnew String(untitled/image/sport/sport(index)\\);step0; //计步器归零ArrRandom(); //数组乱序initImage(); //生成图片}//提示弹框创建else if (obj prompt) {showJDialog(x 显示全图 c 重新游戏);}//关于用户动作监听else if (obj AboutUser) {showJDialog(那年LoginJframe.userName双手插兜, 被我们打的不知如何还手);}else {return;}}public void showJDialog(String display){//创建一个弹框的对象显示形参displayJDialog Errornew JDialog();//设置弹框的大小Error.setSize(300,300);JLabel datanew JLabel(display);data.setBounds(0,0,200,150);Error.getContentPane().add(data);Error.setTitle(提示);//将弹框设置为界面顶置Error.setAlwaysOnTop(true);//初始位置为居中显示Error.setLocationRelativeTo(null);//将弹框设置为不关闭弹框无法操作其他界面Error.setModal(true);//将弹框显示方式设置为显示Error.setVisible(true);}
} User类
package ui;//用户类
public class User {private String Name;private String password;public User() {}public User(String name, String password) {this.Name name;this.password password;}public String getName() {return Name;}public void setName(String name) {this.Name name;}public String getPassword() {return password;}public void setPassword(String password) {this.password password;}
} 图片素材获取方式
方式1访问黑马官网可下载www.itheima.com?bz 方式2关注黑马程序员公众号回复关键词:领取资源02