Java综合项目:拼图游戏
零、项目描述
1. 用户登录界面
- 包括用户名、密码、验证码。
- 点击验证码刷新。
- 用户名或密码不正确有提示。
2. 注册界面
- 包括用户名、密码、再次输入密码。
- 两次密码输入不正确有提示。
- 用户名不能重复注册。
3. 游戏界面
- 包括菜单栏、记步、拼图。
- 上下左右键移动拼图。
- 快捷键A查看原图。
- 快捷键W快速通过。
4. 素材
百度云盘链接: 素材下载 提取码: nws6
一、创建主界面
- 主界面:宽603px,高680px;
package day15.ui;
import javax.swing.*;
public class GameJFrame extends JFrame {
// 初始化
public GameJFrame() {
// 设置宽高
this.setSize(603, 680);
// 设置界面标题
this.setTitle("拼图单机版 v1.0");
}
}
- 登陆界面:宽488px,高430px;
package day15.ui;
import javax.swing.*;
public class LoginJFrame extends JFrame {
// 初始化
public LoginJFrame() {
this.setSize(488, 430);
this.setVisible(true);
}
}
- 注册界面:宽488px,高500px。
package day15.ui;
import javax.swing.*;
public class RegisterJFrame extends JFrame {
// 初始化
public RegisterJFrame() {
this.setSize(488, 500);
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);
}
三、制作菜单栏
private void initJMenuBar() {
// 创建整个菜单对象
JMenuBar jMenuBar = new JMenuBar();
// 创建菜单选项
JMenu functionJMenu = new JMenu("功能");
JMenu aboutJMenu = new JMenu("关于我们");
// 创建菜单选项条目
JMenuItem replayItem = new JMenuItem("重新游戏");
JMenuItem reLoginItem = new JMenuItem("重新登录");
JMenuItem closeItem = new JMenuItem("关闭游戏");
JMenuItem accountItem = new JMenuItem("公众号");
// 把条目添加到选项下面
functionJMenu.add(replayItem);
functionJMenu.add(reLoginItem);
functionJMenu.add(closeItem);
aboutJMenu.add(accountItem);
// 把选项添加到菜单栏中
jMenuBar.add(functionJMenu);
jMenuBar.add(aboutJMenu);
// 给界面设置菜单
this.setJMenuBar(jMenuBar);
}
四、添加图片
private void initImage() {
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
ImageIcon icon = new ImageIcon("/Users/fulling/Desktop/Java/src/day15/image/animal/animal3/" + date[i][j] + ".jpg");
// 创建JLabel对象(管理容器)
JLabel jLabel = new JLabel(icon);
// 指定位置
jLabel.setBounds(105 * j, 105 * i, 105, 105);
// 把管理容器添加到界面中
this.getContentPane().add(jLabel);
}
}
}
五、打乱图片
int[][] date = new int[4][4];
private void initDate() {
Random random = new Random();
int[] tempArr = new int[16];
for (int i = 0; i < tempArr.length; i++) {
tempArr[i] = i;
}
// 打乱
for (int i = 0; i < tempArr.length; i++) {
int index = random.nextInt(tempArr.length);
int temp = tempArr[i];
tempArr[i] = tempArr[index];
tempArr[index] = temp;
}
// 放入数组
for (int i = 0; i < tempArr.length; i++) {
date[i / 4][i % 4] = tempArr[i];
}
}
六、美化界面
private void initImage() {
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
ImageIcon icon = new ImageIcon("/Users/fulling/Desktop/Java/src/day15/image/animal/animal3/" + date[i][j] + ".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("/Users/fulling/Desktop/Java/src/day15/image/background.png"));
background.setBounds(40, 40, 508, 560);
this.getContentPane().add(background);
}
七、移动
@Override
public void keyReleased(KeyEvent e) {
int code = e.getKeyCode();
switch (code) {
case 37:
System.out.println("zuo");
if (y == 3) {
return;
}
date[x][y] = date[x][y + 1];
date[x][y + 1] = 0;
y++;
initImage();
break;
case 38:
System.out.println("shang");
if (x == 3) {
return;
}
date[x][y] = date[x + 1][y];
date[x + 1][y] = 0;
x++;
initImage();
break;
case 39:
System.out.println("you");
if (y == 0) {
return;
}
date[x][y] = date[x][y - 1];
date[x][y - 1] = 0;
y--;
initImage();
break;
case 40:
System.out.println("xia");
if (x == 0) {
return;
}
date[x][y] = date[x - 1][y];
date[x - 1][y] = 0;
x--;
initImage();
break;
default:
break;
}
}
八、查看完整图片
@Override
public void keyPressed(KeyEvent e) {
int code = e.getKeyCode();
if (code == 65) {
this.getContentPane().removeAll();
JLabel all = new JLabel(new ImageIcon(path + "all.jpg"));
all.setBounds(83, 134, 420, 420);
this.getContentPane().add(all);
// 加载背景图片
JLabel background = new JLabel(new ImageIcon("/Users/fulling/Desktop/Java/src/day15/image/background.png"));
background.setBounds(40, 40, 508, 560);
this.getContentPane().add(background);
// 刷新界面
this.getContentPane().repaint();
}
}
九、判断胜利
// 判断是否胜利
public boolean victory(){
for (int i = 0; i < date.length; i++) {
for (int j = 0; j < date[i].length; j++) {
if (date[i][j] != win[i][j]) {
return false;
}
}
}
return true;
}
十、源码
1. 用户类
package day15.ui;
import java.util.ArrayList;
public class User {
private String username;
private String pd;
public User() {
}
public User(String username, String pd) {
this.username = username;
this.pd = pd;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPd() {
return pd;
}
public void setPd(String pd) {
this.pd = pd;
}
public String toString() {
return "User{username = " + username + ", pd = " + pd + "}";
}
public static int getIndex(ArrayList<User> userArr, String username) {
for (int i = 0; i < userArr.size(); i++) {
if (userArr.get(i).getUsername().equals(username)) {
return i;
}
}
return -1;
}
}
2. 验证码工具类
package day15.ui;
import java.util.Random;
public class VerifyUtil {
public static String makeVerify() {
Random random = new Random();
StringBuilder sb = new StringBuilder();
for (char i = 'a'; i <= 'z'; i++) {
sb.append(i);
}
for (char i = 'A'; i <= 'Z'; i++) {
sb.append(i);
}
char[] verify = new char[5];
for (int i = 0; i < verify.length; i++) {
verify[i] = sb.charAt(random.nextInt(sb.length()));
}
verify[random.nextInt(verify.length)] = (char) (random.nextInt(10) + 48);
return new String(verify);
}
}
3. 登录类
package day15.ui;
import day15.App;
import javax.swing.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
public class LoginJFrame extends JFrame implements MouseListener {
JTextField usernameText = new JTextField();
JTextField passwordText = new JPasswordField();
JTextField verifyText = new JTextField();
String verify = VerifyUtil.makeVerify();
JLabel verifyJLabel = new JLabel(this.verify);
JLabel loginButton = new JLabel();
JLabel registerButton = new JLabel();
ArrayList<User> userArr = new ArrayList<>();
// 初始化
public LoginJFrame() {
initJFrame();
initImage();
// 设置界面显示
this.setVisible(true);
}
public LoginJFrame(ArrayList<User> userArr) {
this.userArr = userArr;
initJFrame();
initImage();
// 设置界面显示
this.setVisible(true);
}
private void initImage() {
// 基础布局
JLabel usernameLabel = new JLabel(new ImageIcon("src/day15/image/login/用户名.png"));
JLabel passwordLabel = new JLabel(new ImageIcon("src/day15/image/login/密码.png"));
JLabel verifyLabel = new JLabel(new ImageIcon("src/day15/image/login/验证码.png"));
usernameLabel.setBounds(120, 140, 47, 17);
passwordLabel.setBounds(120, 200, 32, 16);
verifyLabel.setBounds(120, 260, 56, 21);
usernameText.setBounds(180, 135, 180, 30);
passwordText.setBounds(180, 195, 180, 30);
verifyText.setBounds(180, 255, 100, 30);
this.getContentPane().add(usernameLabel);
this.getContentPane().add(passwordLabel);
this.getContentPane().add(verifyLabel);
this.getContentPane().add(usernameText);
this.getContentPane().add(passwordText);
this.getContentPane().add(verifyText);
// 验证码
verifyJLabel.setBounds(290, 255, 80, 30);
verifyJLabel.addMouseListener(this);
this.getContentPane().add(verifyJLabel);
// 按钮
this.loginButton.setIcon(new ImageIcon("src/day15/image/login/登录按钮.png"));
this.registerButton.setIcon(new ImageIcon("src/day15/image/login/注册按钮.png"));
this.loginButton.setBounds(110, 310, 128, 47);
this.registerButton.setBounds(250, 310, 128, 47);
this.getContentPane().add(this.loginButton);
this.getContentPane().add(this.registerButton);
this.loginButton.addMouseListener(this);
this.registerButton.addMouseListener(this);
// 背景
JLabel background = new JLabel(new ImageIcon("src/day15/image/login/background.png"));
background.setBounds(0, 0, 470, 390);
this.getContentPane().add(background);
// 刷新界面
this.getContentPane().repaint();
}
private void initJFrame() {
this.setSize(470, 418);
// 设置界面标题
this.setTitle("拼图单机版 v1.0 登录");
// 设置界面置顶
this.setAlwaysOnTop(true);
// 设置界面居中
this.setLocationRelativeTo(null);
// 设置关闭模式
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
// 取消默认的居中放置,只有取消了才会按照xy轴的形式添加组件
this.setLayout(null);
}
@Override
public void mouseClicked(MouseEvent e) {
Object source = e.getSource();
if (source == verifyJLabel) {
this.verify = VerifyUtil.makeVerify();
this.verifyJLabel.setText(this.verify);
}
}
@Override
public void mousePressed(MouseEvent e) {
Object source = e.getSource();
if (source == this.loginButton) {
System.out.println("登录按下");
this.loginButton.setIcon(new ImageIcon("src/day15/image/login/登录按下.png"));
} else if (source == registerButton) {
System.out.println("注册");
this.registerButton.setIcon(new ImageIcon("src/day15/image/login/注册按下.png"));
}
}
@Override
public void mouseReleased(MouseEvent e) {
Object source = e.getSource();
if (source == this.loginButton) {
login();
this.loginButton.setIcon(new ImageIcon("src/day15/image/login/登录按钮.png"));
} else if (source == registerButton) {
System.out.println("注册松开");
this.registerButton.setIcon(new ImageIcon("src/day15/image/login/注册按钮.png"));
this.setVisible(false);
new RegisterJFrame(userArr);
}
}
private void login() {
System.out.println("登录松开");
String username = usernameText.getText();
String pd = passwordText.getText();
String verifyInput = verifyText.getText();
int index = User.getIndex(userArr, username);
if (!verifyInput.equalsIgnoreCase(verify)) {
App.showJDialog("验证码不正确,请重新输入");
return;
}
if (index < 0) {
App.showJDialog("用户名" + username + "未注册");
return;
}
String rightPd = userArr.get(index).getPd();
if (!pd.equals(rightPd)) {
System.out.println("密码不正确,请重新输入");
return;
}
App.showJDialog("登录成功");
this.setVisible(false);
new GameJFrame();
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
}
4. 注册类
package day15.ui;
import day15.App;
import javax.swing.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
public class RegisterJFrame extends JFrame implements MouseListener {
JTextField usernameText = new JTextField();
JTextField passwordText = new JPasswordField();
JTextField againText = new JPasswordField();
JLabel registerButton = new JLabel();
JLabel resetButton = new JLabel();
JLabel loginButton = new JLabel();
ArrayList<User> userArr = new ArrayList<>();
// 初始化
public RegisterJFrame() {
initJFrame();
initImage();
// 设置界面显示
this.setVisible(true);
}
public RegisterJFrame(ArrayList<User> userArr) {
this.userArr = userArr;
initJFrame();
initImage();
// 设置界面显示
this.setVisible(true);
}
private void initImage() {
// 基础布局
JLabel usernameLabel = new JLabel(new ImageIcon("src/day15/image/register/注册用户名.png"));
JLabel passwordLabel = new JLabel(new ImageIcon("src/day15/image/register/注册密码.png"));
JLabel againLabel = new JLabel(new ImageIcon("src/day15/image/register/再次输入密码.png"));
usernameLabel.setBounds(90, 140, 79, 17);
passwordLabel.setBounds(90, 200, 64, 16);
againLabel.setBounds(90, 260, 96, 17);
usernameText.setBounds(200, 135, 180, 30);
passwordText.setBounds(200, 195, 180, 30);
againText.setBounds(200, 255, 180, 30);
this.getContentPane().add(usernameLabel);
this.getContentPane().add(passwordLabel);
this.getContentPane().add(againLabel);
this.getContentPane().add(usernameText);
this.getContentPane().add(passwordText);
this.getContentPane().add(againText);
// 按钮
this.registerButton.setIcon(new ImageIcon("src/day15/image/register/注册按钮.png"));
this.resetButton.setIcon(new ImageIcon("src/day15/image/register/重置按钮.png"));
this.loginButton.setIcon(new ImageIcon("src/day15/image/register/登录按钮.png"));
this.registerButton.setBounds(35, 310, 128, 47);
this.resetButton.setBounds(175, 310, 128, 47);
this.loginButton.setBounds(315, 310, 128, 47);
this.getContentPane().add(this.registerButton);
this.getContentPane().add(this.resetButton);
this.getContentPane().add(this.loginButton);
this.registerButton.addMouseListener(this);
this.resetButton.addMouseListener(this);
this.loginButton.addMouseListener(this);
// 背景
JLabel background = new JLabel(new ImageIcon("src/day15/image/register/background.png"));
background.setBounds(0, 0, 470, 390);
this.getContentPane().add(background);
// 刷新界面
this.getContentPane().repaint();
}
private void initJFrame() {
this.setSize(470, 418);
// 设置界面标题
this.setTitle("拼图单机版 v1.0 注册");
// 设置界面置顶
this.setAlwaysOnTop(true);
// 设置界面居中
this.setLocationRelativeTo(null);
// 设置关闭模式
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
// 取消默认的居中放置,只有取消了才会按照xy轴的形式添加组件
this.setLayout(null);
}
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
Object source = e.getSource();
if (source == this.registerButton) {
this.registerButton.setIcon(new ImageIcon("src/day15/image/register/注册按下.png"));
} else if (source == resetButton) {
this.resetButton.setIcon(new ImageIcon("src/day15/image/register/重置按下.png"));
} else if (source == loginButton) {
this.loginButton.setIcon(new ImageIcon("src/day15/image/register/登录按下.png"));
}
}
@Override
public void mouseReleased(MouseEvent e) {
Object source = e.getSource();
if (source == this.registerButton) {
register();
} else if (source == resetButton) {
System.out.println("重置");
usernameText.setText("");
passwordText.setText("");
againText.setText("");
this.resetButton.setIcon(new ImageIcon("src/day15/image/register/重置按钮.png"));
} else if (source == loginButton) {
this.setVisible(false);
new LoginJFrame(userArr);
}
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
public void register() {
System.out.println("注册松开");
String username = usernameText.getText();
String pd = passwordText.getText();
String again = againText.getText();
// 查重
if (User.getIndex(userArr, username) >= 0) {
App.showJDialog("用户名已注册");
} else {
if (pd.equals(again)) {
App.showJDialog("用户名" + username + "注册成功");
userArr.add(new User(username, pd));
this.setVisible(false);
new LoginJFrame(userArr);
} else {
App.showJDialog("两次密码输入不一致");
}
}
this.registerButton.setIcon(new ImageIcon("src/day15/image/register/注册按钮.png"));
}
}
5. 游戏类
package day15.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[][] date = new int[4][4];
// 正确的数组
int[][] win = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 0}};
// 空白方块的位置
int x = 0;
int y = 0;
// 记录当前图片的路径
String path = "src/day15/image/";
String chooseImage = "";
// 记录步数
int step = 0;
JMenuItem replayItem = new JMenuItem("重新游戏");
JMenuItem reLoginItem = new JMenuItem("重新登录");
JMenuItem closeItem = new JMenuItem("关闭游戏");
JMenuItem accountItem = new JMenuItem("公众号");
JMenuItem girlItem = new JMenuItem("美女");
JMenuItem animalItem = new JMenuItem("动物");
JMenuItem sportItem = new JMenuItem("运动");
// 初始化
public GameJFrame() {
// 初始化界面
initJFrame();
// 初始化菜单
initJMenuBar();
// 初始化数据
initDate();
this.animalItem.doClick();
// 初始化图片
initImage();
// 设置界面显示
this.setVisible(true);
}
private void initDate() {
Random random = new Random();
int[] tempArr = new int[16];
for (int i = 0; i < tempArr.length; i++) {
tempArr[i] = i;
}
// 打乱
for (int i = 0; i < tempArr.length; i++) {
int index = random.nextInt(tempArr.length);
int temp = tempArr[i];
tempArr[i] = tempArr[index];
tempArr[index] = temp;
}
// 放入数组
for (int i = 0; i < tempArr.length; i++) {
if (tempArr[i] == 0) {
x = i / 4;
y = i % 4;
}
date[i / 4][i % 4] = tempArr[i];
}
}
private void initImage() {
// 清空原本已经出现的图片
this.getContentPane().removeAll();
if (victory()) {
JLabel winJLabel = new JLabel(new ImageIcon("src/day15/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++) {
ImageIcon icon = new ImageIcon(path + chooseImage + date[i][j] + ".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("/Users/fulling/Desktop/Java/src/day15/image/background.png"));
background.setBounds(40, 40, 508, 560);
this.getContentPane().add(background);
// 刷新界面
this.getContentPane().repaint();
}
private void initJMenuBar() {
// 创建整个菜单对象
JMenuBar jMenuBar = new JMenuBar();
// 创建菜单选项
JMenu functionJMenu = new JMenu("功能");
JMenu aboutJMenu = new JMenu("关于我们");
JMenu changeJMenu = new JMenu("更改图片");
// 创建菜单选项条目
replayItem.addActionListener(this);
reLoginItem.addActionListener(this);
closeItem.addActionListener(this);
accountItem.addActionListener(this);
girlItem.addActionListener(this);
animalItem.addActionListener(this);
sportItem.addActionListener(this);
// 把条目添加到选项下面
functionJMenu.add(changeJMenu);
functionJMenu.add(replayItem);
functionJMenu.add(reLoginItem);
functionJMenu.add(closeItem);
aboutJMenu.add(accountItem);
changeJMenu.add(girlItem);
changeJMenu.add(animalItem);
changeJMenu.add(sportItem);
// 把选项添加到菜单栏中
jMenuBar.add(functionJMenu);
jMenuBar.add(aboutJMenu);
// 给界面设置菜单
this.setJMenuBar(jMenuBar);
}
private void initJFrame() {
// 设置宽高
this.setSize(603, 680);
// 设置界面标题
this.setTitle("拼图单机版 v1.0");
// 设置界面置顶
this.setAlwaysOnTop(true);
// 设置界面居中
this.setLocationRelativeTo(null);
// 设置关闭模式
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
// 取消默认的居中放置,只有取消了才会按照xy轴的形式添加组件
this.setLayout(null);
// 给整个界面添加键盘事件
this.addKeyListener(this);
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
if (victory()) {
return;
}
int code = e.getKeyCode();
if (code == 65) {
this.getContentPane().removeAll();
JLabel all = new JLabel(new ImageIcon(path + chooseImage + "all.jpg"));
all.setBounds(83, 134, 420, 420);
this.getContentPane().add(all);
// 加载背景图片
JLabel background = new JLabel(new ImageIcon("/Users/fulling/Desktop/Java/src/day15/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();
switch (code) {
case 37:
System.out.println("zuo");
if (y == 3) {
return;
}
date[x][y] = date[x][y + 1];
date[x][y + 1] = 0;
y++;
step++;
initImage();
break;
case 38:
System.out.println("shang");
if (x == 3) {
return;
}
date[x][y] = date[x + 1][y];
date[x + 1][y] = 0;
x++;
step++;
initImage();
break;
case 39:
System.out.println("you");
if (y == 0) {
return;
}
date[x][y] = date[x][y - 1];
date[x][y - 1] = 0;
y--;
step++;
initImage();
break;
case 40:
System.out.println("xia");
if (x == 0) {
return;
}
date[x][y] = date[x - 1][y];
date[x - 1][y] = 0;
x--;
step++;
initImage();
break;
case 65:
initImage();
break;
case 87:
date = new int[][]{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 0}};
initImage();
break;
default:
break;
}
}
// 判断是否胜利
public boolean victory() {
for (int i = 0; i < date.length; i++) {
for (int j = 0; j < date[i].length; j++) {
if (date[i][j] != win[i][j]) {
return false;
}
}
}
return true;
}
@Override
public void actionPerformed(ActionEvent e) {
Random random = new Random();
Object obj = e.getSource();
if (obj == replayItem) {
step = 0;
initDate();
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("src/day15/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 == girlItem) {
int i = random.nextInt(13);
chooseImage = "girl/girl" + i + "/";
step = 0;
initDate();
initImage();
} else if (obj == animalItem) {
int i = random.nextInt(8);
chooseImage = "animal/animal" + i + "/";
step = 0;
initDate();
initImage();
} else if (obj == sportItem) {
int i = random.nextInt(10);
chooseImage = "sport/sport" + i + "/";
step = 0;
initDate();
initImage();
}
}
}
6. App主入口
package day15;
import day15.ui.LoginJFrame;
import javax.swing.*;
public class App {
public static void main(String[] args) {
new LoginJFrame();
}
public static void showJDialog(String content) {
JDialog jDialog = new JDialog();
jDialog.setSize(200, 150);
jDialog.setAlwaysOnTop(true);
jDialog.setLocationRelativeTo(null);
jDialog.setModal(true);
JLabel warning = new JLabel(content);
warning.setBounds(0, 0, 200, 150);
jDialog.getContentPane().add(warning);
jDialog.setVisible(true);
}
}