本内容包含:登录页面+注册页面+猜数字小游戏页面
登录页面:手机号+密码+校验码+随机校验码+登录按钮+注册
注册页面:手机号+密码+密码确认+注册按钮
游戏页面:标题(提示框)+输入框+猜一下+重置
包含校验:
手机号:非空+11位+首位非0整数+整数
密 码:非空+6-18位整数+英文字母组合
校验码:非空+整数+英文字母
数 字:非空+非数字+非范围
游戏界面:
登录游戏自动生成1-100的随机整数, 输入1-100的任意一个整数
输入不等于随机数,提示输入范围,直到输入整数等于随机数。
如果在输入期间输入空,非数字,非范围,则会提示正确的输入范围。
点击猜一下或者回车进行猜测,点击重置,重新开始游戏。
public class GuessNumberGame extends JFrame {
private int small = 1;
private int big = 100;
Random r = new Random();
private int num = r.nextInt(100)+1;
public GuessNumberGame(){
init();
this.setVisible(true);
}
private void init() {
this.setTitle("猜数字小游戏");
this.setSize(600,400);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setResizable(false);
this.setLayout(null);
//标题
JLabel jl = new JLabel("欢迎来到猜数字小游戏");
jl.setSize(500,40);
jl.setLocation(100,60);
jl.setFont(new Font("华文行楷", Font.PLAIN, 40));
this.add(jl);
//文本框
JTextField jtf = new JTextField();
jtf.setSize(200,40);
jtf.setLocation(200,140);
jtf.setFont(new Font("华文行楷", Font.PLAIN, 40));
this.add(jtf);
//"猜一下"按钮
JButton jb = new JButton("猜一下");
jb.setSize(160, 40);
jb.setLocation(130,220);
jb.setFont(new Font("华文行楷", Font.PLAIN, 40));
this.add(jb);
//"重置哈"按钮
JButton reset = new JButton("重置哈");
reset.setSize(160,40);
reset.setLocation(320,220);
reset.setFont(new Font("华文行楷", Font.PLAIN, 40));
this.add(reset);
//给文本框装上doClick
jtf.addActionListener(e -> jb.doClick());
//给"猜一下"按钮装上监听
jb.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String text = jtf.getText();
//校验null和空
if (text.equals("") || text.equals(null)){
jl.setText("无内容,请输入数字内容");
}
//校验只能是数字
for (int i = 0; i < text.length(); i++) {
char c = text.charAt(i);
if (c < '0' || c > '9'){
jl.setText("输入的内容包含非法内容");
return;
}
}
int a = Integer.parseInt(text);
//校验范围
if (a>big || a < small){
jl.setText("请输入范围内数字"+"("+small+"-"+big+")");
return;
}
//逻辑
if (a>num){
big=a;
jl.setText("猜大了,应该在"+ small + "到" + big + "之间");
jtf.setText("");
}else if(a<num){
small=a;
jl.setText("猜大了,应该在"+ small + "到" + big + "之间");
jtf.setText("");
}else{
jl.setText("恭喜您,猜对了!!!");
}
}
});
//给"重置哈"装上监听
reset.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
small=1;
big=100;
num = r.nextInt(100)+1;
jtf.setText("");
jl.setText("欢迎来到猜数字小游戏");
}
});
}
}
登录页面:
用户输入用户手机号,密码,校验码,点击登录,
注册,提示用户未注册, 已注册,进行,密码
和验证码校验,无误,登录到游戏界面。
package test1;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.ArrayList;
/**
* @author WJJ
* @create 2022-05-21
*/
public class LoginAndRegisterTab extends JFrame {
private JLabel registLable;
private JPanel loginPanle;
private JTabbedPane tabContianer;
public LoginAndRegisterTab(){
init();
this.setVisible(true);
}
private void init() {
this.setSize(600,400);
this.setTitle("欢迎来到猜数字小游戏");
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setResizable(false);
tabContianer = new JTabbedPane();
initLoginTab();
tabContianer.addTab("登录", loginPanle);
this.add(tabContianer);
}
//登录界面
private void initLoginTab() {
loginPanle = new JPanel();
loginPanle.setLayout(null);
//手机号JLabel
JLabel phoneNumberLabel = new JLabel("手机号");
phoneNumberLabel.setSize(70, 40);
phoneNumberLabel.setLocation(100,40);
phoneNumberLabel.setFont(new Font("华文行楷", Font.PLAIN, 20));
//手机号输入框JTextField
JTextField phoneNumberTextField = new JTextField();
phoneNumberTextField.setSize(300,40);
phoneNumberTextField.setLocation(200,40);
phoneNumberTextField.setFont(new Font("华文行楷", Font.PLAIN, 20));
//密码JLabel
JLabel passwordLabel= new JLabel("密 码");
passwordLabel.setSize(70, 40);
passwordLabel.setLocation(100,110);
passwordLabel.setFont(new Font("华文行楷", Font.PLAIN, 20));
//密码输入框JTextField
JPasswordField passwordTextField = new JPasswordField();
passwordTextField.setSize(300,40);
passwordTextField.setLocation(200,110);
//校验码JLabel
JLabel verifyCodeLabel = new JLabel("校验码");
verifyCodeLabel.setSize(70, 40);
verifyCodeLabel.setLocation(100,180);
verifyCodeLabel.setFont(new Font("华文行楷", Font.PLAIN, 20));
//校验码输入JTextField
JTextField verifyCodeTextField = new JTextField();
verifyCodeTextField.setSize(150,40);
verifyCodeTextField.setLocation(200,180);
verifyCodeTextField.setFont(new Font("华文行楷", Font.PLAIN, 20));
//校验码显示区JLabel
JLabel verifyCodeDisplayLabel = new JLabel(StringUtils.getVerifyCode());
verifyCodeDisplayLabel.setSize(150, 40);
verifyCodeDisplayLabel.setLocation(400,180);
verifyCodeDisplayLabel.setFont(new Font("华文行楷", Font.PLAIN, 20));
verifyCodeDisplayLabel.setForeground(Color.RED);
//校验码显示监听
verifyCodeDisplayLabel.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
verifyCodeDisplayLabel.setText(StringUtils.getVerifyCode());
}
});
//登录按钮 JButton
JButton loginButton = new JButton("登录");
loginButton.setSize(100, 40);
loginButton.setLocation(200,240);
loginButton.setFont(new Font("华文行楷", Font.PLAIN, 20));
loginButton.addActionListener(e -> {
// 获取 手机号 密码 验证码
String phoneNumber = phoneNumberTextField.getText();
String password = passwordTextField.getText();
String verifyCode = verifyCodeTextField.getText();
//校验手机格式
String[] messages = StringUtils.verifyPhoneNumber(phoneNumber);
if (messages[1].equals("RED")) {
JOptionPane.showMessageDialog(LoginAndRegisterTab.this, messages[0]);
phoneNumberTextField.requestFocus();
return;
}
//2: 判断手机号是否存在
ArrayList<String> allUsers = IoUtils.allUser();
int index = StringUtils.isExist(phoneNumber, allUsers);
if (index == -1) {
JOptionPane.showMessageDialog(LoginAndRegisterTab.this, "用户名不存在");
phoneNumberTextField.requestFocus();
return;
}
//3: 比对密码
// password
String phoneNumberAndPassword = allUsers.get(index);
String pwd = phoneNumberAndPassword.substring(12);
if (!password.equals(pwd)) {
JOptionPane.showMessageDialog(LoginAndRegisterTab.this, "密码错误");
passwordTextField.requestFocus();
return;
}
//4:比对 验证码,
String text = verifyCodeDisplayLabel.getText(); //验证码显示区显示的内容
if (!verifyCode.equalsIgnoreCase(text)) {
JOptionPane.showMessageDialog(LoginAndRegisterTab.this, "验证码错误");
verifyCodeTextField.requestFocus();
return;
}
//登录成功,关闭当前登陆窗口,跳转到猜数字窗口
JOptionPane.showMessageDialog(LoginAndRegisterTab.this, "登录成功!!!");
LoginAndRegisterTab.this.dispose();
new GuessNumberGame();
});
//注册
registLable = new JLabel("注册");
registLable.setSize(70, 20);
registLable.setLocation(303,263);
registLable.setFont(new Font("华文行楷", Font.PLAIN, 13));
registLable.setForeground(Color.BLUE);
//给注册装监听
registLable.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
initRegistTab();
registLable.setVisible(false);
}
});
//添加组件
loginPanle.add(phoneNumberLabel);
loginPanle.add(phoneNumberTextField);
loginPanle.add(passwordLabel);
loginPanle.add(passwordTextField);
loginPanle.add(verifyCodeLabel);
loginPanle.add(verifyCodeTextField);
loginPanle.add(verifyCodeDisplayLabel);
loginPanle.add(loginButton);
loginPanle.add(registLable);
}
注册页面:上接登录页面
用户未注册,点击注册来到注册页面
输入要注册的手机号,未注册则不能进行下一步
已注册,提示已注册,输入密码,确认密码
点击注册,无误注册界面关闭转登录页面
//注册界面
private void initRegistTab() {
JPanel rigistPanel = new JPanel();
rigistPanel.setLayout(null);
//手机号JLabel
JLabel phoneNumberLabel = new JLabel("手机号");
phoneNumberLabel.setSize(70, 40);
phoneNumberLabel.setLocation(100,40);
phoneNumberLabel.setFont(new Font("华文行楷", Font.PLAIN, 20));
//手机号输入框JTextField
JTextField phoneNumberTextField = new JTextField();
phoneNumberTextField.setSize(300,40);
phoneNumberTextField.setLocation(200,40);
phoneNumberTextField.setFont(new Font("华文行楷", Font.PLAIN, 20));
//手机号输入提示JLabel
JLabel phoneNumberTip = new JLabel();
phoneNumberTip.setSize(300,10);
phoneNumberTip.setLocation(200,83);
phoneNumberTip.setFont(new Font("华文行楷", Font.PLAIN, 9));
//对手机号输入框监听
phoneNumberTextField.addFocusListener(new FocusAdapter() {
@Override
public void focusLost(FocusEvent e) {
String phoneNumber = phoneNumberTextField.getText();
String[] messages = StringUtils.verifyPhoneNumber(phoneNumber);
phoneNumberTip.setText(messages[0]);
if (messages[1].equals("RED")){
phoneNumberTip.setForeground(Color.RED);
phoneNumberTextField.requestFocus();
}else if(messages[1].equals("BLUE")){
ArrayList<String> allUsers = IoUtils.allUser();
int index = StringUtils.isExist(phoneNumber, allUsers);
if (index != -1){
phoneNumberTip.setText("手机号已经被注册了");
phoneNumberTip.setForeground(Color.RED);
phoneNumberTextField.requestFocus();
return;
}
phoneNumberTip.setForeground(Color.BLUE);
}
}
});
//密码JLabel
JLabel passwordLabel= new JLabel("密 码");
passwordLabel.setSize(70, 40);
passwordLabel.setLocation(100,110);
passwordLabel.setFont(new Font("华文行楷", Font.PLAIN, 20));
//密码输入框JTextField
JPasswordField passwordTextField = new JPasswordField();
passwordTextField.setSize(300,40);
passwordTextField.setLocation(200,110);
//密码输入提示JLabel
JLabel passwordTip = new JLabel();
passwordTip.setSize(300,10);
passwordTip.setLocation(200,153);
passwordTip.setFont(new Font("华文行楷", Font.PLAIN, 9));
//密码提示监听
passwordTextField.addFocusListener(new FocusAdapter() {
@Override
public void focusLost(FocusEvent e) {
String password = passwordTextField.getText();
String[] messages = StringUtils.verifyPassword(password);
passwordTip.setText(messages[0]);
if (messages[1].equals("RED")){
passwordTip.setForeground(Color.RED);
passwordTip.requestFocus();
}else if(messages[1].equals("BLUE")){
passwordTip.setForeground(Color.BLUE);
}
}
});
//确认密码Label
JLabel confirmPasswordLabel = new JLabel("确认密码");
confirmPasswordLabel.setSize(80, 40);
confirmPasswordLabel.setLocation(100,180);
confirmPasswordLabel.setFont(new Font("华文行楷", Font.PLAIN, 20));
// 确认密码输入 JTextField
JPasswordField confirmPasswordTextField = new JPasswordField();
confirmPasswordTextField.setSize(300,40);
confirmPasswordTextField.setLocation(200,180);
//确认密码提示JLabel
JLabel confirmPasswordTip = new JLabel();
confirmPasswordTip.setSize(300,10);
confirmPasswordTip.setLocation(200,223);
confirmPasswordTip.setFont(new Font("华文行楷", Font.PLAIN, 9));
//确认密码输入监听
confirmPasswordTextField.addFocusListener(new FocusAdapter() {
@Override
public void focusLost(FocusEvent e) {
String confirmPassword = confirmPasswordTextField.getText();
String password = passwordTextField.getText();
if (confirmPassword.equals(password)){
confirmPasswordTip.setText("输入密码一致");
confirmPasswordTip.setForeground(Color.BLUE);
}else if (!confirmPassword.equals(password)){
confirmPasswordTip.setText("输入密码不一致");
confirmPasswordTip.setForeground(Color.RED);
confirmPasswordTip.requestFocus();
}
}
});
//注册按钮 JButton
JButton registButton = new JButton("注册");
registButton.setSize(100, 40);
registButton.setLocation(200,240);
registButton.setFont(new Font("华文行楷", Font.PLAIN, 20));
//注册按钮校验 JOptionPane.showMessageDialog();
registButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 手机号格式校验
String phoneNumber = phoneNumberTextField.getText();
String[] messeges = StringUtils.verifyPhoneNumber(phoneNumber);
if (messeges[1].equals("RED")){
JOptionPane.showMessageDialog(LoginAndRegisterTab.this,messeges[0]);
return;
}
//手机是否注册校验
ArrayList<String> allUsers = IoUtils.allUser();
int index = StringUtils.isExist(phoneNumber, allUsers);
if (index != -1){
JOptionPane.showMessageDialog(LoginAndRegisterTab.this, "该手机号已被注册!");
return;
}
//密码格式校验
String password = passwordTextField.getText();
messeges = StringUtils.verifyPassword(password);
if (messeges[1].equals("RED")){
JOptionPane.showMessageDialog(LoginAndRegisterTab.this,messeges[0]);
return;
}
//密码校验
String confirmPassword = confirmPasswordTextField.getText();
if (!confirmPassword.equals(password)){
JOptionPane.showMessageDialog(LoginAndRegisterTab.this,"密码和确认密码不一致");
return;
}
// 保存 手机号 密码
try {
OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(LoginAndRegisterTab.class.getClassLoader().getResource("user.txt").getPath(),true), "UTF-8");
BufferedWriter bw = new BufferedWriter(writer);
bw.write(phoneNumber+"&"+password+"\n");
/* bw.newLine();*/
bw.flush();
bw.close();
} catch (IOException ioException) {
ioException.printStackTrace();
}
//注册成功
JOptionPane.showMessageDialog(LoginAndRegisterTab.this,"注册成功!");
// 关闭 注册界面
tabContianer.remove(1);
tabContianer.setSelectedIndex(0);
registLable.setVisible(true);
}
});
//添加组件
rigistPanel.add(phoneNumberLabel);
rigistPanel.add(phoneNumberTextField);
rigistPanel.add(phoneNumberTip);
rigistPanel.add(passwordLabel);
rigistPanel.add(passwordTextField);
rigistPanel.add(passwordTip);
rigistPanel.add(confirmPasswordLabel);
rigistPanel.add(confirmPasswordTextField);
rigistPanel.add(confirmPasswordTip);
rigistPanel.add(registButton);
tabContianer.addTab("注册", rigistPanel);
tabContianer.setSelectedIndex(1);
}
}
Io工具类
public class IoUtils {
private IoUtils(){}
/**
*读入已保存用户
*/
public static ArrayList<String> allUser(){
ArrayList<String> al = new ArrayList<>();
InputStreamReader is = null;
try {
is = new InputStreamReader(IoUtils.class.getClassLoader().getResourceAsStream("user.txt"), "UTF-8");
BufferedReader br = new BufferedReader(is);
String s;
while((s=br.readLine()) != null){
if (!s.equals("")){
al.add(s);
}
}
br.close();
} catch (Exception e) {
e.printStackTrace();
}
return al;
}
}
String工具类
package test1;
import java.util.ArrayList;
import java.util.Random;
/**
* @author WJJ
* @create 2022-05-20
*/
public class StringUtils {
private StringUtils(){
}
/**
*用户手机号校验
*/
public static String[] verifyPhoneNumber(String phoneNumber){
//校验null和空
if (phoneNumber==null || phoneNumber.equals("")){
return new String[]{"手机号不能为空","RED"};
}
//校验手机号是11位
if (phoneNumber.length() != 11){
return new String[]{"非11位手机号","RED"};
}
//校验手机号第1位
if (phoneNumber.charAt(0) != '1'){
return new String[]{"手机号非1开头","RED"};
}
//校验手机号中非数字字符
for (int i = 0; i < phoneNumber.length(); i++) {
if (phoneNumber.charAt(i)<'0' || phoneNumber.charAt(i) > '9'){
return new String[]{"含有非数字符号","RED"};
}
}
return new String[]{"手机号输入正确","BLUE"};
}
/**
*密码校验
*/
public static String[] verifyPassword(String password){
//校验密码6-18位
if (password.length()<6 || password.length()>18){
return new String[]{"请输入6到18位密码","RED"};
}
//校验密码为数字+字母组合
int noNumCount = 0;
for (int i = 0; i < password.length(); i++) {
if (password.charAt(i)>'9' || password.charAt(i)<'0'){
noNumCount++;
}
}
if (noNumCount == password.length()){
return new String[]{"密码是由数字加字母组合","RED"};
}
int noLetterCount = 0;
for (int i = 0; i < password.length(); i++) {
char c = password.charAt(i);
if (c<'A' || (c>'z' && c<'a') || c>'z'){
noLetterCount++;
}
}
if (noLetterCount == password.length()){
return new String[]{"密码是由数字加字母组合","RED"};
}
return new String[]{"密码格式正确","BLUE"};
}
/**
* 随机生成校验码
*/
public static String getVerifyCode(){
int index=0;
int n = 0;
char[] chars = new char[52];
for (int i = 'A',j = 'a'; i <= 'Z'; i++,j++) {
chars[index++]=(char)i;
chars[index++]=(char)j;
}
String str = new String(chars)+"0123456789";
String code = "";
for (int i = 0; i < 4; i++) {
char c = str.charAt(new Random().nextInt(str.length()));
code += c;
}
return code;
}
public static int isExist(String phone, ArrayList<String> al){
for (int i = 0; i < al.size(); i++) {
if(phone.equals(al.get(i).substring(0,11))){
return i;
}
}
return -1;
}
}
mian
public class Demo {
public static void main(String[] args) {
LoginAndRegisterTab loginAndRegisterTab = new LoginAndRegisterTab();
}
}