import javax.swing.*;
import javax.swing.border.BevelBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class LoginFrame extends JFrame implements ActionListener, LoginField, KeyListener {
User hrc = new User("hrc", "123");
//隐藏容器
Container container = getContentPane();
//验证码
String code = Util.code();
//密码
String visiblePassword = "";
boolean showPassword;
//初始化对象
public LoginFrame() {
initFrame();
initWord();
initButton();
setVisible(true);
// 添加背景图片
background.setBounds(40, 40, 508, 560);
container.add(background);
}
//初始化按钮
public void initButton() {
//添加按钮
//登录
loginJButton.setBounds(100, 500, 120, 47);
loginJButton.setBorder(new BevelBorder(7));
loginJButton.addActionListener(this);
container.add(loginJButton);
loginJButton.setVisible(true);
//注册
registerJButton.setBounds(350, 500, 120, 47);
registerJButton.setBorder(new BevelBorder(7));
registerJButton.addActionListener(this);
container.add(registerJButton);
registerJButton.setVisible(true);
//验证码
codeJButton.setText(code);
codeJButton.setBounds(300, 350, 100, 50);
codeJButton.addActionListener(this);
codeJButton.setVisible(true);
container.add(codeJButton);
//显示密码
showPasswordJButton.setBounds(400, 250, 50, 50);
showPasswordJButton.addActionListener(this);
showPasswordJButton.setVisible(true);
container.add(showPasswordJButton);
}
//初始化文本框和文字
public void initWord() {
//添加文本框和文字
//用户名
userNameJLabel.setBounds(100, 150, 200, 50);
container.add(userNameJLabel);
userNameJtextField.setBounds(200, 150, 200, 50);
userNameJtextField.addKeyListener(this);
container.add(userNameJtextField);
userNameJtextField.setVisible(true);
//密码
passwordJLabel.setBounds(100, 250, 200, 50);
container.add(passwordJLabel);
passwordJTextField.setBounds(200, 250, 200, 50);
passwordJTextField.addKeyListener(this);
container.add(passwordJTextField);
passwordJTextField.setVisible(true);
//验证码
codeJLabel.setBounds(100, 350, 200, 50);
container.add(codeJLabel);
codeJTextField.setBounds(200, 350, 100, 50);
codeJTextField.addKeyListener(this);
container.add(codeJTextField);
codeJTextField.setVisible(true);
}
//初始化窗口
public void initFrame() {
setSize(600, 700);
setLocationRelativeTo(null);
setTitle("登录");
//取消中间放置布局
setLayout(null);
addKeyListener(this);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
//初始化弹窗
private static void initDialog(JLabel jLabel) {
JDialog dialog = new JDialog();
dialog.setSize(100, 100);
dialog.getContentPane().add(jLabel);
dialog.setLocationRelativeTo(null);
dialog.setModal(true);
dialog.setVisible(true);
}
//隐藏密码
public void hidePassword() {
String hide = "";
for (int i = 0; i < visiblePassword.length(); i++) {
hide += "*";
}
passwordJTextField.setText(hide);
}
//点击事件
@Override
public void actionPerformed(ActionEvent e) {
Object obj = e.getSource();
//登录
if (obj == loginJButton) {
login();
}
//注册
if (obj == registerJButton) {
new RegisterFrame();
setVisible(false);
}
//验证码
if (obj == codeJButton) {
code = Util.code();
codeJButton.setText(code);
}
//显示密码
if (obj == showPasswordJButton) {
if (!showPassword) {
passwordJTextField.setText(visiblePassword);
showPassword = true;
} else {
hidePassword();
showPassword = false;
}
}
}
//初始化登录按钮
private void login() {
if (userNameJtextField.getText().equals("")) {
initDialog(new JLabel("用户名不能为空"));
} else if (!hrc.getUserName().equals(userNameJtextField.getText())) {
initDialog(new JLabel("用户名不存在"));
} else if (!hrc.getPassword().equals(visiblePassword)) {
initDialog(new JLabel("密码错误"));
} else if (!codeJTextField.getText().equalsIgnoreCase(code)) {
initDialog(new JLabel("验证码错误"));
} else {
new GameFrame();
setVisible(false);
}
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
Object obj = e.getSource();
//密码区
if (obj == passwordJTextField) {
//密码删除
if (keyCode == 8) {
if (passwordJTextField.getText() == null) {
visiblePassword = "";
return;
}
if (visiblePassword.length() - 1 == 0) {
return;
}
if (visiblePassword.length() >= 2) {
String str = visiblePassword.substring(0, visiblePassword.length() - 1);
visiblePassword = str;
}
}
}
}
@Override
public void keyReleased(KeyEvent e) {
int keyCode = e.getKeyCode();
System.out.println(keyCode);
Object obj = e.getSource();
//用户名区
if(obj==userNameJtextField){
if (keyCode == 10)
login();
}
//验证码区
if (obj == codeJTextField) {
if (keyCode == 10)
login();
}
//密码区
if (obj == passwordJTextField) {
if (keyCode == 10)
login();
//禁用键
int[] forbidKey = {67, 17, 16, 192, 20, 18, 524, 37,10, 38,39,40};
for (int x : forbidKey) {
if (x == keyCode)
return;
}
//DEL键
if (keyCode == 8) {
if (passwordJTextField.getText() == null) {
visiblePassword = "";
return;
}
if (visiblePassword.length() - 1 == 0) {
visiblePassword = "";
return;
}
if (visiblePassword.length() >= 2) {
String str = visiblePassword.substring(0, visiblePassword.length() - 1);
visiblePassword = str;
}
return;
}
//获取用户输入的字符
visiblePassword += e.getKeyChar();
//点击显示密码时不在隐藏密码
if (!showPassword)
hidePassword();
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
Java版PuzzleGame拼图游戏(含源码和思维导图)
共573个文件
jpg:527个
png:21个
class:9个
需积分: 0 0 下载量 131 浏览量
2023-04-12
10:42:33
上传
评论 2
收藏 10.62MB ZIP 举报
温馨提示
1.内容概括: 使用JAVA编写的拼图游戏 2.适合人群:学完JAVA基础的同学 3.作用:可以巩固JAVA基础知识 4.阅读建议:可以先看我的思维导图,然后再进行编写代码
资源推荐
资源详情
资源评论
收起资源包目录
Java版PuzzleGame拼图游戏(含源码和思维导图) (573个子文件)
GameFrame.class 6KB
LoginFrame.class 6KB
RegisterFrame.class 6KB
Util.class 2KB
User.class 1KB
GameField.class 1KB
LoginField.class 1KB
RegisterField.class 1KB
App.class 385B
.gitignore 184B
PuzzleGame.iml 433B
LoginFrame.java 8KB
GameFrame.java 7KB
RegisterFrame.java 7KB
Util.java 1KB
LoginField.java 964B
User.java 929B
GameField.java 898B
RegisterField.java 854B
App.java 184B
all.jpg 399KB
all.jpg 328KB
all.jpg 319KB
all.jpg 253KB
all.jpg 240KB
all.jpg 238KB
all.jpg 231KB
all.jpg 211KB
all.jpg 203KB
all.jpg 200KB
all.jpg 200KB
all.jpg 197KB
all.jpg 193KB
all.jpg 173KB
all.jpg 167KB
all.jpg 152KB
all.jpg 149KB
all.jpg 148KB
all.jpg 143KB
all.jpg 139KB
all.jpg 134KB
all.jpg 133KB
all.jpg 127KB
all.jpg 126KB
all.jpg 125KB
all.jpg 123KB
all.jpg 122KB
all.jpg 120KB
all.jpg 118KB
all.jpg 113KB
all.jpg 103KB
10.jpg 23KB
7.jpg 23KB
12.jpg 23KB
11.jpg 23KB
16.jpg 23KB
7.jpg 23KB
14.jpg 23KB
8.jpg 23KB
9.jpg 23KB
10.jpg 23KB
4.jpg 22KB
13.jpg 22KB
9.jpg 22KB
6.jpg 22KB
5.jpg 22KB
1.jpg 22KB
12.jpg 22KB
11.jpg 22KB
11.jpg 22KB
15.jpg 21KB
3.jpg 21KB
15.jpg 21KB
2.jpg 21KB
3.jpg 21KB
16.jpg 21KB
6.jpg 20KB
15.jpg 20KB
10.jpg 20KB
2.jpg 19KB
16.jpg 19KB
14.jpg 19KB
10.jpg 19KB
5.jpg 19KB
11.jpg 19KB
4.jpg 19KB
14.jpg 18KB
6.jpg 18KB
12.jpg 18KB
8.jpg 18KB
6.jpg 18KB
10.jpg 18KB
10.jpg 17KB
1.jpg 17KB
13.jpg 17KB
2.jpg 17KB
15.jpg 17KB
11.jpg 17KB
11.jpg 17KB
7.jpg 17KB
共 573 条
- 1
- 2
- 3
- 4
- 5
- 6
资源评论
双非人想要进大厂
- 粉丝: 28
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功