每一步都做过测试了,创建好类与对象复制进去即可
一、对象
游戏部分
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[][] data = new int[4][4];
int x = 0;
int y = 0;
int[][] win = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15, 0}
};
String path = "puzzlegame\\image\\";
String pathPlus = "animal\\animal3\\";
int step = 0;
// 创建菜单JMenuBar的对象
JMenuBar jMenuBar = new JMenuBar();
// 创建菜单上面的两个选项JMenu
JMenu functionJMenu = new JMenu("功能");
JMenu aboutJMenu = new JMenu("关于我们");
// 创建更换图片
JMenu changeImage = new JMenu("更换图片");
// 创建JMenuItem的对象
JMenuItem girl = new JMenuItem("美女");
JMenuItem animal = new JMenuItem("动物");
JMenuItem sport = new JMenuItem("运动");
JMenuItem replayItem = new JMenuItem("重新游戏");
JMenuItem reLoginItem = new JMenuItem("重新登录");
JMenuItem closeItem = new JMenuItem("关闭游戏");
JMenuItem accountItem = new JMenuItem("公众号");
// 游戏主界面
public GameJFrame() {
// 初始化界面
initJFrame();
// 初始化菜单
initJMenuBar();
// 初始化数据
initData();
// 初始化图片
initImage();
// 控制界面显隐,建议放在最后
this.setVisible(true);
}
// 初始化界面
private void initJFrame() {
// 设置界面宽高
this.setSize(603, 680);
// 设置标题
this.setTitle("拼图 v1.0");
// 界面持续置顶
this.setAlwaysOnTop(true);
// 界面居中
this.setLocationRelativeTo(null);
// 设置关闭模式
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
// 设置布局,取消居中
this.setLayout(null);
// 添加事件
this.addKeyListener(this);
}
// 初始化菜单
private void initJMenuBar() {
// 把美女,动物,运动添加到更换图片当中
changeImage.add(girl);
changeImage.add(animal);
changeImage.add(sport);
// 把更换图片,重新游戏,重新登录,关闭游戏添加到功能当中
functionJMenu.add(changeImage);
functionJMenu.add(replayItem);
functionJMenu.add(reLoginItem);
functionJMenu.add(closeItem);
// 把公众号添加到关于我们当中
aboutJMenu.add(accountItem);
// 把功能,关于我们添加到JMenuBar当中
jMenuBar.add(functionJMenu);
jMenuBar.add(aboutJMenu);
// 把整个菜单JMenuBar添加到整个界面当中
this.setJMenuBar(jMenuBar);
replayItem.addActionListener(this);
reLoginItem.addActionListener(this);
closeItem.addActionListener(this);
accountItem.addActionListener(this);
animal.addActionListener(this);
girl.addActionListener(this);
sport.addActionListener(this);
}
// 初始化数据
private void initData() {
Random r = new Random();
int[] tempArr = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
for (int i = 0; i < tempArr.length; i++) {
int temp = tempArr[i];
int number = r.nextInt(16);
tempArr[i] = tempArr[number];
tempArr[number] = temp;
}
for (int i = 0; i < tempArr.length; i++) {
if (tempArr[i] == 0) {
x = i / 4;
y = i % 4;
} else {
data[i / 4][i % 4] = tempArr[i];
}
}
}
// 初始化图片
private void initImage() {
this.getContentPane().removeAll();
if (victory()) {
JLabel winJLabel = new JLabel(new ImageIcon("puzzlegame/image/win.png"));
winJLabel.setBounds(203, 283, 197, 73);
this.getContentPane().add(winJLabel);
}
JLabel stepCount = new JLabel("步数" + step);
stepCount.setBounds(50, 30, 100, 20);
this.getContentPane().add(stepCount);
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
int num = data[i][j];
// 创建一个图片ImageIcon的对象
ImageIcon icon = new ImageIcon(path + pathPlus + num + ".jpg");
// 创建一个JLabel的对象(管理容器)
JLabel jLabel = new JLabel(icon);
// 指定图片位置
jLabel.setBounds(105 * j + 83, 105 * i + 134, 105, 105);
// 给图片加边框
jLabel.setBorder(new BevelBorder(BevelBorder.LOWERED));
// 把管理容器添加到界面中
this.getContentPane().add(jLabel);
}
}
JLabel background = new JLabel(new ImageIcon("puzzlegame\\image\\background.png"));
background.setBounds(40, 40, 508, 560);
this.getContentPane().add(background);
this.getContentPane().repaint();
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
int code = e.getKeyCode();
if (code == 65) {
this.getContentPane().removeAll();
JLabel all = new JLabel(new ImageIcon(path + pathPlus + "all.jpg"));
all.setBounds(83, 134, 420, 420);
this.getContentPane().add(all);
JLabel background = new JLabel(new ImageIcon("puzzlegame\\image\\background.png"));
background.setBounds(40, 40, 508, 560);
this.getContentPane().add(background);
this.getContentPane().repaint();
}
}
@Override
public void keyReleased(KeyEvent e) {
if (victory()) {
return;
}
int code = e.getKeyCode();
if (code == 37) {
if (y == 3) {
return;
}
data[x][y] = data[x][y + 1];
data[x][y + 1] = 0;
y++;
step++;
initImage();
} else if (code == 38) {
if (x == 3) {
return;
}
data[x][y] = data[x + 1][y];
data[x + 1][y] = 0;
x++;
step++;
initImage();
} else if (code == 39) {
if (y == 0) {
return;
}
data[x][y] = data[x][y - 1];
data[x][y - 1] = 0;
y--;
step++;
initImage();
} else if (code == 40) {
if (x == 0) {
return;
}
data[x][y] = data[x - 1][y];
data[x - 1][y] = 0;
x--;
step++;
initImage();
} else if (code == 65) {
initImage();
} else if (code == 87) {
data = new int[][]{
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15, 0}
};
initImage();
}
}
public boolean victory() {
for (int i = 0; i < data.length; i++) {
for (int j = 0; j < data[i].length; j++) {
if (data[i][j] != win[i][j]) {
return false;
}
}
}
return true;
}
@Override
public void actionPerformed(ActionEvent e) {
Random r = new Random();
Object obj = e.getSource();
if (obj == replayItem) {
step = 0;
data = new int[4][4];
initData();
initImage();
} else if (obj == reLoginItem) {
this.setVisible(false);
new LoginJFrame();
} else if (obj == closeItem) {
System.exit(0);
} else if (obj == accountItem) {
JDialog jDialog = new JDialog();
JLabel jLabel = new JLabel(new ImageIcon("puzzlegame/image/about.png"));
jLabel.setBounds(0, 0, 258, 258);
jDialog.getContentPane().add(jLabel);
jDialog.setSize(344, 344);
jDialog.setAlwaysOnTop(true);
jDialog.setLocationRelativeTo(null);
jDialog.setModal(true);
jDialog.setVisible(true);
} else if (obj == animal) {
this.getContentPane().removeAll();
step = 0;
pathPlus = "animal\\animal" + (r.nextInt(8) + 1) + "\\";
data = new int[4][4];
initData();
initImage();
this.getContentPane().repaint();
} else if (obj == girl) {
this.getContentPane().removeAll();
step = 0;
pathPlus = "girl\\girl" + (r.nextInt(13) + 1) + "\\";
data = new int[4][4];
initData();
initImage();
this.getContentPane().repaint();
} else if (obj == sport) {
this.getContentPane().removeAll();
step = 0;
pathPlus = "sport\\sport" + (r.nextInt(10) + 1) + "\\";
data = new int[4][4];
initData();
initImage();
this.getContentPane().repaint();
}
}
}
登录部分
import javax.swing.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
public class LoginJFrame extends JFrame implements MouseListener {
//创建一个集合存储正确的用户名和密码
static ArrayList<User> list = new ArrayList<>();
static {
list.add(new User("zhangsan", "123"));
list.add(new User("lisi", "1234"));
}
JButton login = new JButton();
JButton register = new JButton();
JTextField username = new JTextField();
JTextField password = new JTextField();
JTextField code = new JTextField();
String codeStr = CodeUtil.getCode();
JLabel rightCode = new JLabel();
public LoginJFrame() {
//初始化界面
initJFrame();
//在这个界面中添加内容
initView();
//让当前界面显示出来
this.setVisible(true);
}
public void initView() {
//1. 添加用户名文字
JLabel usernameText = new JLabel(new ImageIcon("puzzlegame\\image\\login\\用户名.png"));
usernameText.setBounds(116, 135, 47, 17);
this.getContentPane().add(usernameText);
//2.添加用户名输入框
username.setBounds(195, 134, 200, 30);
this.getContentPane().add(username);
//3.添加密码文字
JLabel passwordText = new JLabel(new ImageIcon("puzzlegame\\image\\login\\密码.png"));
passwordText.setBounds(130, 195, 32, 16);
this.getContentPane().add(passwordText);
//4.密码输入框
password.setBounds(195, 195, 200, 30);
this.getContentPane().add(password);
//验证码提示
JLabel codeText = new JLabel(new ImageIcon("puzzlegame\\image\\login\\验证码.png"));
codeText.setBounds(133, 256, 50, 30);
this.getContentPane().add(codeText);
//验证码的输入框
code.setBounds(195, 256, 100, 30);
this.getContentPane().add(code);
//设置内容
rightCode.setText(codeStr);
//位置和宽高
rightCode.setBounds(300, 256, 50, 30);
//添加到界面
this.getContentPane().add(rightCode);
//5.添加登录按钮
login.setBounds(123, 310, 128, 47);
login.setIcon(new ImageIcon("puzzlegame\\image\\login\\登录按钮.png"));
//去除按钮的默认边框
login.setBorderPainted(false);
//去除按钮的默认背景
login.setContentAreaFilled(false);
login.addMouseListener(this);
this.getContentPane().add(login);
//6.添加注册按钮
register.setBounds(256, 310, 128, 47);
register.setIcon(new ImageIcon("puzzlegame\\image\\login\\注册按钮.png"));
//去除按钮的默认边框
register.setBorderPainted(false);
//去除按钮的默认背景
register.setContentAreaFilled(false);
register.addMouseListener(this);
this.getContentPane().add(register);
//7.添加背景图片
JLabel background = new JLabel(new ImageIcon("puzzlegame\\image\\login\\background.png"));
background.setBounds(0, 0, 470, 390);
this.getContentPane().add(background);
}
public void initJFrame() {
this.setSize(488, 430);//设置宽高
this.setTitle("拼图游戏 V1.0登录");//设置标题
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//设置关闭模式
this.setLocationRelativeTo(null);//居中
this.setAlwaysOnTop(true);//置顶
this.setLayout(null);//取消内部默认布局
}
//要展示弹框
public void showJDialog(String content) {
//创建一个弹框对象
JDialog jDialog = new JDialog();
//给弹框设置大小
jDialog.setSize(200, 150);
//让弹框置顶
jDialog.setAlwaysOnTop(true);
//让弹框居中
jDialog.setLocationRelativeTo(null);
//弹框不关闭永远无法操作下面的界面
jDialog.setModal(true);
//创建Jlabel对象管理文字并添加到弹框当中
JLabel warning = new JLabel(content);
warning.setBounds(0, 0, 200, 150);
jDialog.getContentPane().add(warning);
//让弹框展示出来
jDialog.setVisible(true);
}
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
Object obj = e.getSource();
if (obj == login) {
ImageIcon Icon = new ImageIcon("puzzlegame/image/login/登录按下.png");
login.setIcon((Icon));
}
}
@Override
public void mouseReleased(MouseEvent e) {
Object obj = e.getSource();
if (obj == login) {
ImageIcon Icon = new ImageIcon("puzzlegame/image/login/登录按钮.png");
login.setIcon((Icon));
if (username.getText().length() == 0 || password.getText().length() == 0) {
showJDialog("用户名或密码不能为空");
} else {
for (int i = 0; i < list.size(); i++) {
if (list.get(i).getName().equals(username.getText())) {
if (list.get(i).getPassword().equals(password.getText())) {
if (codeStr.equals(code.getText())) {
this.setVisible(false);
new GameJFrame();
} else {
showJDialog("验证码错误");
codeStr = CodeUtil.getCode();
rightCode.setText(codeStr);
}
} else {
showJDialog("用户名或密码错误");
}
break;
}
if (!list.get(list.size() - 1).getName().equals(username.getText())) {
showJDialog("用户名或密码错误");
break;
}
}
}
} else if (obj == register) {
System.out.println("注册");
}
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
}
用户类
public class User {
private String name;
private String password;
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;
}
}
验证码
import java.util.Random;
public class CodeUtil {
public static String getCode() {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 6; i++) {
sb.append(number());
}
return sb.toString();
}
public static char number() {
Random r = new Random();
char a = (char)(r.nextInt(26) + 65);
char b = (char)(r.nextInt(26) + 97);
char c = (char)(r.nextInt(10) + 48);
char number = 0;
int chance = r.nextInt(3) + 1;
switch (chance) {
case 1 -> number = a;
case 2 -> number = b;
case 3 -> number = c;
}
return number;
}
}
二、测试
App
import com.cookies.ui.LoginJFrame;
public class App {
public static void main(String[] args) {
// 程序入口
new LoginJFrame();
}
}