前言
因为一些工作暂缓了进度,现在开始恢复更新
博客仅记录个人学习进度和一些查缺补漏。
学习内容:BV17F411T7Ao
一、主界面分析
JFrame作为一个JavaBean,描述的是一个界面,其属性有长宽以及可见度等。
在写具体界面时,因为具体界面有各自的逻辑代码,而其本体属性都相同,故继承JFrame。
菜单结构也是一种JavaBean,在主界面的实例对象中作为内部成员被同时实例化。
使用JLabel控件来承载图片,图片作为该空间的属性被展示出来,而我们的操控主要是操作这个控件。
控件的位置由左上角点确定,坐标轴由左上角展开。
JFrame界面里有一个隐藏的ContentPane容器,直接设置JLabel时,需要更改布局时要手动解除默认布局。 该隐藏容器是初始化JFrame时被虚拟机初始化的。
二、逻辑代码
首先需要 将拼图看成4*4的宫格,将16个数字放置其中,并认定1~16为完成形态。
打乱数字顺序就完成了拼图 的乱序。
关于打乱第一个想法是:直接使用全排列,将所有的排列情况输出,最后返回排列数量作为随机数的范围。使用一个随机数来挑选其中一个排列情况。然而这种思想的时间复杂度过高,在算法中很容易超时。
第二个思路是:保证时间复杂度为N的情况下,仅遍历一次数组,将数组中当前指向的数字和随机索引上的数字交换一次,也可以实现随机的打乱。
**但是,以上的打乱都是随机打乱,有可能造成拼图根本无法还原。【是的,就是因为完全玩不回去发现的,有的情况下根本无法还原】
所以优化之后的思路时先把116放进4*4的数组,然后用上下左右随机替换来打散矩阵。其逻辑就是先选横纵,再选14第几列或者第几行,掏出来作为一维数组,再遍历并随机替换。**代码如下:
对于按钮JButton有监听事件的功能,其中动作监听只能监听空格敲击以及鼠标单击。
jButton.addActionListener();
表示为button添加监听。
ActionListener为一个接口,实现函数为actionPerformed,需要使用实现类并重写实现函数,当ActionListener作为传入参数被传入时,会自动监听(监听点击和空格事件),在事件被触发后会执行被重写过(自定义的)actionPerformed函数(这个函数仅对应单击和空格)。
对于MouseListener,也是一个监听接口,与ActionListener类似,但是前者的监听事件比后者更丰富。同样也需要实现类来自定义重写触发函数(更丰富的触发条件也有着更多的对应的触发函数)
相似的,KeyListener对应着更丰富的监听方式与对应的方法。
IDEA2024中相对路径需要省略项目文件夹名
三、游戏逻辑
其实只用将0和上下左右去交换,直至矩阵变成指定形状。
此时需要添加键盘监听事件,这个监听应该写在界面的初始化中,作为大界面的一种属性(开监听),因为玩游戏的时候鼠标焦点应该在最大的游戏界面上,此时进行监听的应该是JFrame对象,即只要打开该界面就开始进行监听。
按A键查看原图,有两种方法:
方法一:就和视频中一样,可以直接将all.jpg渲染到容器上(先将容器release,再添加all),松开后将该容器重新初始化(此时作为全局变量的data数组并没有改变),直接初始化就是最后一次data数组表示的图片分格。但是缺陷在于选择不同的图片的时候需要更改all.jpg的路径,需要额外定义一个通用路径。
方法二:直接使用另一个完成版的二维数组(1-16)来渲染此时分割的图片,而data数据并没有改变,在松开时还原data数据,就不用每次都要更换all的路径了。
在完成时需要显示成功图片,而图片的显示需要放到刚刚写的清除全部图像的后面。
四、一些细节的实现
原视频中跳转网页用的是visible的true和false,每次跳转都要重新创建一个JFrame,每次关闭虚拟机才会把这些新建的无用的窗口全部关掉,所以我将原窗口本身直接传递给下一个窗口,跳转回去的时候直接关闭当前窗口并将上一层窗口的visible设置为true,以节省空间。
验证部分由于还没掌握正则和组件的一些规则,都是拿最原始的比较完成的。
而由于没有数据库,考虑到程序要识别到当前的用户列表,只能在创建login窗口的时候附加一个静态列表,用于存储所有的用户,设计成静态成员是因为总是在new新的Frame(有时也当作刷新)。
注册页面应该有两个传参,一个是所有用户列表,防止重名以及登录的识别。一个是登录页面,防止多次new窗口内存溢出。
因为需要每次进入的时候随机图片,所以需要在新建GameFrame的时候新建图片
因为要根据用户名选择不同的图片组,所以需要传入用户名
以及根据不同的Username分配对应的图片。
**
代码如下
**
package game.jigsawgame.ui;
import java.util.Random;
public class CheckCode {
public static String verify() {
char[] verChar = new char[5];
for(int i = 0; i < 5; i++) {
Random radmChar = new Random();
int j = radmChar.nextInt(2);
if(j == 0){
verChar[i] = (char)('a' +radmChar.nextInt(26 ));
} else {
verChar[i] = (char)('A' +radmChar.nextInt(26 ));
}
}
Random radmChar = new Random();
verChar[radmChar.nextInt(5)] = (char)(radmChar.nextInt(10) + '0');
String str = new String(verChar);
System.out.println(str);
return str;
}
}
package game.jigsawgame.ui;
import javax.swing.*;
import javax.swing.border.BevelBorder;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Random;
public class GameJFrame extends JFrame implements KeyListener {
//继承自JFrame的界面属性
//自己又要显示这个游戏的主界面
//所有游戏相关逻辑都在该界面代码中,游戏逻辑代码是在该界面上显示的,与该界面息息相关。
public GameJFrame (User user) {
// 确认用户
this.user_now = user;
// 初始化JFrame
initJFrame();
// 初始化菜单
initJMenuBar();
// 初始化图片
initImage(data, inirName, initNum);
// 一般来说最后才会显示
this.setVisible(true);
}
// 当前0号图片的位置
int step;
int x;
int y;
int[][] data;
int[][] end_data = new int[4][4];
String inirName = randmStr();
int initNum = randmNum(inirName);
User user_now;
private void initImage(int[][] tempDouArr, String str, int num) {
// 删除目前加载的所有图片
this.getContentPane().removeAll();
// 如果此时已完成,就结束游戏
if(victoryCheck()) {
// 显示游戏结束
showVictory();
}
JLabel stepCount = new JLabel("步数:" + step);
stepCount.setBounds(50,30, 100, 20);
this.getContentPane().add(stepCount);
JLabel UserName = new JLabel("当前用户:" + user_now.getUserName());
UserName.setBounds(50,10, 200, 20);
this.getContentPane().add(UserName);
// 构建二次矩阵
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
// 构建一个图片对象
// 当 k = 0 时,查找不到图片故只有一个空的控件
ImageIcon imageIcon = new ImageIcon("image\\"+ str +"\\"+ str + num +"\\" + tempDouArr[i][j] + ".jpg");
// 创建一个JLabel对象,用于展示图片对象
JLabel jLabel = new JLabel(imageIcon);
// 指定JFrame的位置,需要取消内部容器的默认布局才能使用坐标指定具体位置。
jLabel.setBounds(105 * j + 83, 105 * i + 134, 105, 105);
// 给图片添加边框
// BevelBorder是Border类的
jLabel.setBorder(new BevelBorder(1));
// 把创建好的JLabel添加到游戏界面中
//this.add(jLabel);
this.getContentPane().add(jLabel);
}
}
// 先加载的图片在上方,后加载的图片在下方
// 添加背景图片
JLabel jLabel_background = new JLabel(new ImageIcon("image\\background.png"));
// 初始化位置
jLabel_background.setBounds(40, 40, 508, 560);
//初始化到容器上
this.getContentPane().add(jLabel_background);
// 刷新容器
this.getContentPane().repaint();
}
private int[][] initData () {
// 打乱4*4矩阵
int[] tempArr = new int[16];
for (int i = 0; i < 16; i++) {
tempArr[i] = i + 1;
}
int[][] tempDouArr = new int[4][4];
// 初始化end_data和tempDouArr,即1~16
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
end_data[i][j] = tempArr[4 * i + j];
tempDouArr[i][j] = tempArr[4 * i + j];
}
}
Random random = new Random();
// 先设置替换次数,暂且定义为16次
for (int i = 0; i < 16; i++) {
if (random.nextInt(2) == 0) {
int k = random.nextInt(4);
for (int j = 0; j < 4; j++) {
int temp = tempDouArr[k][j];
int t = random.nextInt(4);
tempDouArr[k][j] = tempDouArr[k][t];
tempDouArr[k][t] = temp;
}
}else {
int k = random.nextInt(4);
for (int j = 0; j < 4; j++) {
int temp = tempDouArr[j][k];
int t = random.nextInt(4);
tempDouArr[j][k] = tempDouArr[t][k];
tempDouArr[t][k] = temp;
}
}
}
//找16号图片(空格)
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if(tempDouArr[i][j] == 16) {
x = i;
y = j;
}
}
}
step = 0;
return tempDouArr;
}
private void initJFrame () {
// 设置JFrame大小
this.setSize(603, 680);
// 设置界面标题
this.setTitle("拼图单机版 v1.0");
// 设置界面置顶
this.setAlwaysOnTop(true);
// 设置界面居中
this.setLocationRelativeTo(null);
// 设置关闭模式
// 0:不关闭 1:默认隐藏 2:最后一个关闭结束程序 3:立即关闭并结束程序
this.setDefaultCloseOperation(3);
// 取消默认的居中放置,null表示手动管理布局
this.setLayout(null);
// 给整个界面添加键盘监听事件
this.addKeyListener(this);
data = initData();
}
private void setNotVisible (){
this.setVisible(false);
}
private void initJMenuBar() {
// 创建菜单对象 JMenuBar 即承载JMenu的容器
JMenuBar jMenuBar = new JMenuBar();
// 创建菜单上的选项
JMenu functionJMenu = new JMenu("功能");
JMenu aboutJMenu = new JMenu("关于我们");
// 创建选项下面的条目对象,但只是增加item还没指定添加到哪个一选项中
JMenuItem replayItem = new JMenuItem("重新游戏");
JMenu changePicture = new JMenu("更换图片");
JMenuItem reLoginItem = new JMenuItem("重新登录");
JMenuItem closeItem = new JMenuItem("关闭游戏");
//更换图片的子菜单
JMenuItem animal = new JMenuItem("动物");
JMenuItem girl = new JMenuItem("人物");
JMenuItem sport = new JMenuItem("运动");
// 实现重玩功能
replayItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 初始化data数组
data = initData();
initImage(data, inirName, initNum);
}
});
functionJMenu.add(replayItem);
girl.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
inirName = new String("girl");
initNum = randmNum(inirName);
initImage(initData(), inirName, initNum);
}
});
sport.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
inirName = new String("sport");
initNum = randmNum(inirName);
initImage(initData(), inirName, initNum);
}
});
animal.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
inirName = new String("animal");
initNum = randmNum(inirName);
initImage(initData(), inirName, initNum);
}
});
functionJMenu.add(changePicture);
changePicture.add(animal);
changePicture.add(girl);
changePicture.add(sport);
// 实现重新登陆
reLoginItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 返回登陆界面
setNotVisible();
// 打开登陆界面
new LoginJFrame();
}
});
functionJMenu.add(reLoginItem);
// 实现关闭游戏
closeItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 关闭游戏
System.exit(0);
}
});
functionJMenu.add(closeItem);
// 添加二维码图片
aboutJMenu.addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {
JDialog jDialog = new JDialog();
JLabel jLabel = new JLabel(new ImageIcon("image\\about.png"));
// 设置jLabel的,适合图片
jLabel.setBounds(0,0,258,258);
// 弹出框的容器加载jLabel
jDialog.getContentPane().add(jLabel);
// 设置弹出框的大小,弹出框置顶以及居中
jDialog.setSize(344,344);
jDialog.setAlwaysOnTop(true);
jDialog.setLocationRelativeTo(null);
// 最高处理优先级
jDialog.setModal(true);
// 弹框显示
jDialog.setVisible(true);
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
});
// 将菜单里面的两个选项添加到菜单中
jMenuBar.add(functionJMenu);
jMenuBar.add(aboutJMenu);
// 给整个界面设置菜单
this.setJMenuBar(jMenuBar);
}
private boolean victoryCheck(){
boolean flag = true;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if(data[i][j] != end_data[i][j]) {
flag = false;
};
}
}
return flag;
}
private void showVictory() {
ImageIcon imageIcon = new ImageIcon("image\\win.png");
JLabel jLabel = new JLabel(imageIcon);
jLabel.setBounds(203, 253, 197, 73);
this.getContentPane().add(jLabel);
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
//a: 65 w:87
// 按A查看原图片形状
if(e.getKeyCode() == 65) {
initImage(end_data, inirName, initNum);
}
}
@Override
public void keyReleased(KeyEvent e) {
// 判断游戏是否结束
if(victoryCheck()){
System.out.println("已完成");
}else {
System.out.println(e.getKeyCode());
// 左:37 上:38 右:39 下:40
// 没有结束则继续游戏
int code = e.getKeyCode();
switch (code) {
case 37:
if(y < 3) {
step++;
System.out.println("向左移动");
data[x][y] = data[x][y + 1];
data[x][y + 1] = 0;
y++;
initImage(data, inirName, initNum);
} else {
System.out.println("无法移动");
}
break;
case 38:
if(x < 3) {
step++;
System.out.println("向上移动");
data[x][y] = data[x + 1][y];
data[x + 1][y] = 0;
x++;
initImage(data, inirName, initNum);
} else {
System.out.println("无法移动");
}
break;
case 39:
if(y > 0) {
step++;
System.out.println("向右移动");
data[x][y] = data[x][y - 1];
data[x][y - 1] = 0;
y--;
initImage(data, inirName, initNum);
} else {
System.out.println("无法移动");
}
break;
case 40:
if(x > 0) {
step++;
System.out.println("向下移动");
data[x][y] = data[x - 1][y];
data[x - 1][y] = 0;
x--;
initImage(data, inirName, initNum);
} else {
System.out.println("无法移动");
}
break;
// 重置图片于变化前的样子
case 65:
initImage(data, inirName, initNum);
break;
// 完成图片
case 87:
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
data[i][j] = end_data[i][j];
if(data[i][j] == 16) {
x = i;
y = j;
}
}
}
initImage(data, inirName, initNum);
break;
default:
break;
}
}
}
public String randmStr() {
ArrayList<String> str = new ArrayList<>();
str.add("animal");
str.add("girl");
str.add("sport");
return str.get(new Random().nextInt(str.size()));
}
public int randmNum(String str) {
if(str.equals("animal")){
return new Random().nextInt(8) + 1;
}else if(str.equals("girl")){
if(user_now.getUserName().equals("vip")){
return new Random().nextInt(2) + 12;
}else {
return new Random().nextInt(11) + 1;
}
}else if(str.equals("sport")){
return new Random().nextInt(10) + 1;
}
return 0;
}
}
package game.jigsawgame.ui;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
public class LoginJFrame extends JFrame {
// 开启注册界面时才会弹出,所以在点击注册后才开始构造。
static ArrayList<User> userArrayList = new ArrayList<>();
public LoginJFrame() {
initFrame();
initImage();
// 一般来说最后才会显示
this.setVisible(true);
}
public void initFrame(){
this.setSize(485, 430);
this.setTitle("登录");
this.setAlwaysOnTop(true);
this.setLocationRelativeTo(null);
this.setLayout(null);
this.setDefaultCloseOperation(3);
}
public void initImage(){
// 删除目前加载的所有图片
this.getContentPane().removeAll();
JLabel jLabel_username = new JLabel("用户名:");
jLabel_username.setBounds(100, 150, 80, 30);
JLabel jLabel_password = new JLabel("密码:");
jLabel_password.setBounds(100, 200, 80, 30);
JTextField jTextField_username = new JTextField();
jTextField_username.setBounds(180, 150, 150, 30);
JPasswordField jPasswordField_password = new JPasswordField();
jPasswordField_password.setBounds(180, 200, 150, 30);
jPasswordField_password.setEchoChar('●');
JButton jButton_register = new JButton("注册");
JButton jButton_login = new JButton("登录");
JButton jButton_password = new JButton("显示密码");
jButton_login.setBounds(150, 300, 80, 30);
jButton_register.setBounds(250, 300, 80, 30);
jButton_password.setBounds(340, 200, 100, 30);
// 传入对象需要是ActionListener对象,但是ActionListener是一个接口类,需要手动继承然后实例化,并自定义实现函数。
// 匿名内部类对象
jButton_login.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String username = jTextField_username.getText();
String password = new String(jPasswordField_password.getPassword());
if(userArrayList.isEmpty()){
dialog("请先注册");
new LoginJFrame();
disposetThisFrame();
}else {
boolean flag = true;
for (User i : userArrayList) {
if(i.getUserName().equals(username)){
flag = false;
if(i.getPassWord().equals(password)){
System.out.println("登陆成功");
new GameJFrame(i);
disposetThisFrame();
}else {
dialog("密码错误");
new LoginJFrame();
disposetThisFrame();
}
break;
}
}
if(flag) {
dialog("没有该用户,请重新登录");
new LoginJFrame();
disposetThisFrame();
}
}
}
});
jButton_password.addMouseListener(new MouseAdapter() {
// 按住显示密码
@Override
public void mousePressed(MouseEvent e) {
super.mousePressed(e);
jPasswordField_password.setEchoChar((char) 0);
}
// 松开隐藏密码
@Override
public void mouseReleased(MouseEvent e) {
super.mouseReleased(e);
jPasswordField_password.setEchoChar('●');
}
});
jButton_register.addActionListener(new ActionListener() {
// 打开注册界面
@Override
public void actionPerformed(ActionEvent e) {
setNotVisible();
new RegisterJFrame(userArrayList, self());
}
});
// 添加按钮
this.getContentPane().add(jButton_login);
this.getContentPane().add(jButton_register);
this.getContentPane().add(jButton_password);
this.getContentPane().add(jLabel_username);
this.getContentPane().add(jLabel_password);
this.getContentPane().add(jTextField_username);
this.getContentPane().add(jPasswordField_password);
// 添加背景图片
JLabel jLabel_background = new JLabel(new ImageIcon("D:\\java_test_code\\JigsawGame\\image\\login\\background.png"));
// 初始化位置
jLabel_background.setBounds(0, 0, 470, 390);
// 初始化到容器上
this.getContentPane().add(jLabel_background);
// 刷新容器
this.getContentPane().repaint();
}
private void setNotVisible() {
this.setVisible(false);
}
private LoginJFrame self() {
return this;
}
private void dialog(String text) {
JDialog jDialog = new JDialog();
// 弹窗左右布局
jDialog.setLayout(new FlowLayout());
JLabel jLabel = new JLabel(new ImageIcon("image\\damie.jpg"));
JLabel jLabel_text = new JLabel(text);
jDialog.setSize(220,150);
jDialog.getContentPane().add(jLabel);
jDialog.getContentPane().add(jLabel_text);
jDialog.setAlwaysOnTop(true);
//屏幕居中
jDialog.setLocationRelativeTo(null);
// 最高处理优先级
jDialog.setModal(true);
// 弹框显示
jDialog.setVisible(true);
}
private void disposetThisFrame() {
this.dispose();
}
}
package game.jigsawgame.ui;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
public class RegisterJFrame extends JFrame {
public RegisterJFrame(ArrayList<User> userArrayList, LoginJFrame loginJFrame) {
this.setSize(488, 500);
this.setTitle("注册");
this.setAlwaysOnTop(true);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(3);
this.setLayout(null);
JLabel jLabel_username = new JLabel("新建用户名:");
jLabel_username.setBounds(50,100,100,30);
JTextField jTextField_username = new JTextField();
jTextField_username.setBounds(150,100, 200, 30);
JLabel jLabel_password = new JLabel("密码:");
jLabel_password.setBounds(50,150,100,30);
JPasswordField JPasswordField_password = new JPasswordField();
JPasswordField_password.setBounds(150,150, 200, 30);
JLabel jLabel_repassword = new JLabel("再次输入密码:");
jLabel_repassword.setBounds(50,200,100,30);
JPasswordField JPasswordField_repassword = new JPasswordField();
JPasswordField_repassword.setBounds(150,200, 200, 30);
JLabel jLabel_checkcode = new JLabel("验证码:");
jLabel_checkcode.setBounds(50, 250, 80, 30);
JLabel jLabel_checkText = new JLabel(CheckCode.verify());
jLabel_checkText.setBounds(130, 250, 80, 30);
JButton jButton_changeCheckCode = new JButton("换一个");
jButton_changeCheckCode.setBounds(300, 250, 80, 30);
jButton_changeCheckCode.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 更换验证码
jLabel_checkText.setText(CheckCode.verify());
}
});
JLabel jLabel_check = new JLabel("请输入上方验证码:");
jLabel_check.setBounds(50,320,150,30);
JTextField jTextField_check = new JTextField();
jTextField_check.setBounds(180,320, 100, 30);
JButton jButton_register = new JButton("确认");
jButton_register.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 确认注册
// 先检验密码输入是否一致
String userName = new String(jTextField_username.getText());
System.out.println(userName);
if(userName.isEmpty()){
System.out.println("用户名为空");
dialog("用户名为空");
new RegisterJFrame(userArrayList, loginJFrame);
disposetThisFrame();
}else {
String passWord = new String(JPasswordField_password.getPassword());
String rePassWord = new String(JPasswordField_repassword.getPassword());
if (!passWord.isEmpty()){
if(passWord.equals(rePassWord)){
String checkCode_1 = jLabel_checkText.getText();
String checkCode_2 = jTextField_check.getText();
if(checkCode_2.equals(checkCode_1)){
System.out.println("验证码正确");
System.out.println("当前用户人数为:" + userArrayList.size());
boolean flag = false;
if(!userArrayList.isEmpty()){
for(User i:userArrayList) {
if (i.getUserName().equals(userName)){
flag = true;
}
}
}
if(flag){
System.out.println("用户名重复");
dialog("用户名重复");
new RegisterJFrame(userArrayList, loginJFrame);
disposetThisFrame();
} else {
User user = new User(userName, passWord);
userArrayList.add(user);
loginJFrame.setVisible(true);
disposetThisFrame();
}
}else {
System.out.println("验证码错误");
dialog("验证码错误");
new RegisterJFrame(userArrayList, loginJFrame);
disposetThisFrame();
}
}else {
System.out.println("密码不一致");
dialog("密码不一致");
new RegisterJFrame(userArrayList, loginJFrame);
disposetThisFrame();
}
}else {
System.out.println("密码不能为空");
dialog("密码不能为空");
new RegisterJFrame(userArrayList, loginJFrame);
disposetThisFrame();
}
}
}
});
jButton_register.setBounds(300, 320, 60, 30);
JButton jButton_back = new JButton("返回");
jButton_back.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
loginJFrame.setVisible(true);
disposetThisFrame();
}
});
jButton_back.setBounds(300, 380, 60, 30);
this.getContentPane().add(jLabel_checkcode);
this.getContentPane().add(jLabel_username);
this.getContentPane().add(jLabel_password);
this.getContentPane().add(jLabel_repassword);
this.getContentPane().add(jLabel_check);
this.getContentPane().add(jTextField_username);
this.getContentPane().add(JPasswordField_password);
this.getContentPane().add(JPasswordField_repassword);
this.getContentPane().add(jTextField_check);
this.getContentPane().add(jButton_changeCheckCode);
this.getContentPane().add(jButton_register);
this.getContentPane().add(jButton_back);
this.getContentPane().add(jLabel_checkText);
// 一般来说最后才会显示
this.setVisible(true);
}
private void disposetThisFrame() {
this.dispose();
}
private void dialog(String text) {
JDialog jDialog = new JDialog();
// 弹窗左右布局
jDialog.setLayout(new FlowLayout());
JLabel jLabel = new JLabel(new ImageIcon("image\\damie.jpg"));
JLabel jLabel_text = new JLabel(text);
jDialog.setSize(220,150);
jDialog.getContentPane().add(jLabel);
jDialog.getContentPane().add(jLabel_text);
jDialog.setAlwaysOnTop(true);
//屏幕居中
jDialog.setLocationRelativeTo(null);
// 最高处理优先级
jDialog.setModal(true);
// 弹框显示
jDialog.setVisible(true);
}
}
package game.jigsawgame.ui;
public class User {
private String userName;
private String passWord;
public User(String userName, String passWord) {
this.userName = userName;
this.passWord = passWord;
}
public User() {}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassWord() {
return passWord;
}
public void setPassWord(String passWord) {
this.passWord = passWord;
}
}
import game.jigsawgame.ui.LoginJFrame;
public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
//程序启动入口
new LoginJFrame();
}
}
素材下载:
https://pan.baidu.com/s/1z2nnkoJZ2Sn6MxTETkuapw?pwd=9987
总结
鸽了好久,因为最近太忙了,又是补习又是考试,这几天重新回去开始学习,重新捡了一下代码,感觉还是得持续学习。
内部类和实体化接口的使用是真的很重要,继承和接口在学习过后确实觉得总在使用,代码的规范化有待学习提升,逻辑方面感觉没什么问题。之后继续努力。