大二电子信息工程小白一枚,想自学后端毕业去找工作,为了提高学习效率,想记录总结一下每天学习的内容,若有志同道合的小伙伴可以一起打卡,有大佬的话也希望能提一些建议,感谢!
5.6
转眼就五月六号了,五一假期期间完全没学,假期就要好好休息!不过回来之后要好好加油。
今天接着之前的进度,学了内部类,不过我感觉不是很重要就没有记笔记,后面用到再说吧。
然后开始写下一个项目了,一个拼图游戏,包含登陆注册的功能。主要是跟着视频敲代码然后自己理解一遍。先搭建了游戏和菜单的界面,用的swing模块,然后把图片加到界面上,因为是拼图游戏所以要打乱,采取的思路是把一维数组的数据用random模块打乱,得到一个打乱后的一维数组后按索引加到二维数组中,这样就可以代表每一个图片的位置,
这是目前完成的一些部分,test是用来测试的,然后ui下面的三个类分别是游戏界面,登录界面和注册界面
以下是代码实现
游戏界面,里面包含了打乱图片的方法
package com.itheima.ui;
import java.util.Random;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
public class GameJFrame extends JFrame{
//JFrame 界面 窗体
//创建二维数组
//用来管理数据
int[][] data = new int[4][4];
public GameJFrame(){
//初始化界面
initJFrame();
//初始化菜单
initJMenuBar();
//初始化数据(打乱)
initDate();
//初始化图片()
initImage();
//让界面显示出来
this.setVisible(true);
}
private void initDate() {
//需求:
//把一个一维数组中的数据(0-16)打乱顺序
//然后再按照4个一组的方式添加到二维数组中
//1.定义一维数组
int[] tempArr = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
//2.打乱
//遍历数组,得到每一个元素,拿着每一个元素跟随机索引上的数据进行交换
Random r = new Random();
for (int i = 0; i < tempArr.length; i++) {
//获取随机索引
int index = r.nextInt(tempArr.length);
int temp = tempArr[i];
tempArr[i] = tempArr[index];
tempArr[index] = temp;
}
//4.创建二维数组
// int[][] data = new int[4][4];
//5.给二维数组添加数据
//解法一:
//遍历一维数组tempArr得到每一个元素,把每一个元素依次添加到二维数组中
for (int i = 0; i < tempArr.length; i++) {
data[i/4][i%4] = tempArr[i];
}
}
private void initJMenuBar() {
//初始化菜单
//创建整个菜单对象
JMenuBar jMenuBar = new JMenuBar();
//创建菜单上面的两个选项的对象(功能)(关于我们)
JMenu functionJmenu = new JMenu("功能");
JMenu aboutJMenu = new JMenu("关于我们");
//创建选项下面的条目对象
JMenuItem replyItem = new JMenuItem("重新登录");
JMenuItem reLoginItem = new JMenuItem("重新登陆");
JMenuItem closeItem = new JMenuItem("关闭游戏");
JMenuItem accountItem = new JMenuItem("公众号");
//将每一个选项下面的条目添加到选项当中
functionJmenu.add(replyItem);
functionJmenu.add(reLoginItem);
functionJmenu.add(closeItem);
aboutJMenu.add(accountItem);
//将菜单里面的两个选项添加到菜单当中
jMenuBar.add(functionJmenu);
jMenuBar.add(aboutJMenu);
//给整个界面设置菜单
this.setJMenuBar(jMenuBar);
//将界面显示出来,建议写在最后
this.setVisible(true);
}
private void initJFrame() {
this.setSize(603,680);
this.setTitle("拼图单机版");
this.setAlwaysOnTop(true);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(3);
}
private void initImage(){
// //创建一个图片ImageIcon对象
// ImageIcon icon = new ImageIcon("D:\\java projects\\.vscode\\puzzlegame\\image\\animal\\animal3\\3.jpg");
// //创建一个JLable对象
// JLabel jLabel = new JLabel(icon);
// //指定图片位置
// jLabel.setBounds(0,0,105,105);
// //把管理容器添加到界面中
// // this.add(jLabel);
// this.getContentPane().add(jLabel);
for (int j = 0; j < 4; j++) {
for (int i = 0; i < 4; i++) {
//获取当前要加载的图片的序号
int num = data[i][j];
//创建一个图片ImageIcon对象
JLabel jLabel = new JLabel(new ImageIcon("D:\\java projects\\.vscode\\puzzlegame\\image\\animal\\animal3\\"+num+".jpg"));
//指定图片位置
jLabel.setBounds(105*i,105*j,105,105);
//把管理容器添加到界面中
// this.add(jLabel);
this.getContentPane().add(jLabel);
}
}
}
}
登陆界面,具体的登录逻辑还没写
package com.itheima.ui;
import javax.swing.JFrame;
public class LoginJFrame extends JFrame{
public LoginJFrame(){
this.setSize(488,430);
this.setVisible(true);
}
}
注册界面,具体的逻辑也还没写
package com.itheima.ui;
import javax.swing.JFrame;
public class RegisterJFrame extends JFrame{
public RegisterJFrame(){
this.setSize(488,500);
this.setVisible(true);
}
}