制造假数据
制造数据
练习一
package Demo1;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Demo1 {
public static void main(String[] args) throws IOException {
//爬取数据
//获取链接
/*
制造假数据:
获取姓氏:https://hanyu.baidu.com/shici/detail?pid=0b2f26d4c0ddb3ee693fdb1137ee1b0d&from=kg0
获取男生名字:http://www.haoming8.cn/baobao/10881.html
获取女生名字:http://www.haoming8.cn/baobao/7641.html
*/
//1.定义变量记录网址
String familyNameNet = "https://hanyu.baidu.com/shici/detail?pid=0b2f26d4c0ddb3ee693fdb1137ee1b0d&from=kg0";
String boyNameNet = "http://www.haoming8.cn/baobao/10881.html";
String girlNameNet = "http://www.haoming8.cn/baobao/7641.html";
//2.爬取数据,把网址上所有的数据拼接成一个字符串
String familyNameStr = webCrawler(familyNameNet);
System.out.println(familyNameStr);
String boyNameStr = webCrawler(boyNameNet);
String girlNameStr = webCrawler(girlNameNet);
// 将爬取到的数据使用正则表达式进行筛选
ArrayList<String> familyNameTempList = getData(familyNameStr,"(.{4})(,|。)",1);
ArrayList<String> boyNameTempList = getData(boyNameStr,"([\\u4E00-\\u9FA5]{2})(、|。)",1);
ArrayList<String> girlNameTempList = getData(girlNameStr,"(.. ){4}..",0);
// System.out.println(familyNameTempList.toString());
// System.out.println("===============================================================");
// System.out.println(boyNameTempList.toString());
// System.out.println("==============================================================");
// System.out.println(girlNameTempList.toString());
//处理获取的数据
//处理姓氏
//处理方案:每个集合拆成char数据进行写入
ArrayList<String> familyNameList = new ArrayList<>();
for (String s : familyNameTempList) {
for (int i = 0; i < 4; i++) {
familyNameList.add(s.charAt(i)+"");
}
}
//处理男名字
//处理方案:去重
ArrayList<String> boyNameList = new ArrayList<>();
for (String s : boyNameTempList) {
if (!boyNameList.contains(s)){
boyNameList.add(s);
}
}
//处理女名字
//处理方案:拆fen
ArrayList<String> girlNameList = new ArrayList<>();
for (String s : girlNameTempList) {
String[] split = s.split(" ");
for (int i = 0; i < split.length; i++) {
girlNameList.add(split[i]);
}
}
//创建数据
/**
* 姓名(唯一)-性别-年龄
*/
ArrayList<String> namelist = getData(familyNameList, boyNameList, girlNameList, 70, 40);
System.out.println(namelist.toString());
//写出数据
BufferedWriter bw = new BufferedWriter(new FileWriter("a\\sd\\scs\\a.txt"));
int a;
for (String s : namelist) {
bw.write(s);
bw.nexyLine();
}
bw.close();
}
public static ArrayList<String> getData(ArrayList<String> familyNameList,ArrayList<String> boyNameList,ArrayList<String> girlNameList,int boyname,int girlbame){
//男名字
HashSet<String> boyset = new HashSet<>();
while (true){
if (boyset.size() == boyname){
break;
}
Collections.shuffle(familyNameList);
Collections.shuffle(boyNameList);
boyset.add(familyNameList.get(0)+boyNameList.get(0));
}
//女名字
HashSet<String> grilset = new HashSet<>();
while (true){
if (grilset.size() == girlbame){
break;
}
Collections.shuffle(familyNameList);
Collections.shuffle(girlNameList);
grilset.add(familyNameList.get(0)+girlNameList.get(0));
}
ArrayList<String> namelist = new ArrayList<>();
Random r = new Random();
for (String s : boyset) {
int i = r.nextInt(10)+18;
namelist.add(s+"-男-"+i);
}
for (String s : grilset) {
int i = r.nextInt(8)+18;
namelist.add(s+"-女-"+i);
}
return namelist;
}
/**
* * 作用:根据正则表达式获取字符串中的数据
* * 参数一:
* * 完整的字符串
* * 参数二:
* * 正则表达式
* * 参数三:
* * 获取数据
* * 0:获取符合正则表达式所有的内容
* * 1:获取正则表达式中第一组数据
* * 2:获取正则表达式中第二组数据
* * ...以此类推
* *
* * 返回值:
* * 真正想要的数据
* 将爬取的数据进行筛选
* @param str 爬取的数据
* @param s 正则表达式
* @param i 正则表达式筛选后保存的位置
* @return
*/
private static ArrayList<String> getData(String str, String s, int i) {
//使用正则表达式筛选
//创建集合保存数据
ArrayList<String> list = new ArrayList<>();
Pattern p=Pattern.compile(s);
Matcher matcher=p.matcher(str);
if(matcher.find()){
System.out.println(matcher.group(i));
list.add(matcher.group(i));
}
return list;
}
/**
* 爬取网站返回网站的数据
// * @param 网站地址
* @return
*/
private static String webCrawler(String src) throws IOException {
//创建网站的流
//记录读取的数据
StringBuilder sb = new StringBuilder();
//创建URL对象
URL url = new URL(src);
//使用URL创建网络连接
URLConnection urlConnection = url.openConnection();
//将网络连接交给InputStreamReader对象进行数据操作
InputStreamReader isr = new InputStreamReader(urlConnection.getInputStream());
//读取数据将数据交给StringBuilder进行记录
int ch;
while ((ch = isr.read()) != -1){
sb.append((char)ch);
}
//关闭流
isr.close();
return sb.toString();
}
}
使用hutools包进行的爬取
public class Demo2 {
public static void main(String[] args) throws IOException {
//爬取数据
//获取链接
/*
制造假数据:
获取姓氏:https://hanyu.baidu.com/shici/detail?pid=0b2f26d4c0ddb3ee693fdb1137ee1b0d&from=kg0
获取男生名字:http://www.haoming8.cn/baobao/10881.html
获取女生名字:http://www.haoming8.cn/baobao/7641.html
*/
//1.定义变量记录网址
String familyNameNet = "https://hanyu.baidu.com/shici/detail?pid=0b2f26d4c0ddb3ee693fdb1137ee1b0d&from=kg0";
String boyNameNet = "http://www.haoming8.cn/baobao/10881.html";
String girlNameNet = "http://www.haoming8.cn/baobao/7641.html";
//2.爬取数据,把网址上所有的数据拼接成一个字符串
String familyNameStr = HttpUtil.get(familyNameNet);
String boyNameStr = HttpUtil.get(boyNameNet);
String girlNameStr = HttpUtil.get(girlNameNet);
// 将爬取到的数据使用正则表达式进行筛选
ArrayList<String> familyNameTempList = (ArrayList<String>) ReUtil.findAll("(.{4})(,|。)",familyNameStr,1);
ArrayList<String> boyNameTempList = (ArrayList<String>) ReUtil.findAll("([\\u4E00-\\u9FA5]{2})(、|。)",boyNameStr,1);
ArrayList<String> girlNameTempList = (ArrayList<String>) ReUtil.findAll("(.. ){4}..",girlNameStr,0);
//处理获取的数据
//处理姓氏
//处理方案:每个集合拆成char数据进行写入
ArrayList<String> familyNameList = new ArrayList<>();
for (String s : familyNameTempList) {
for (int i = 0; i < 4; i++) {
familyNameList.add(s.charAt(i)+"");
}
}
//处理男名字
//处理方案:去重
ArrayList<String> boyNameList = new ArrayList<>();
for (String s : boyNameTempList) {
if (!boyNameList.contains(s)){
boyNameList.add(s);
}
}
//处理女名字
//处理方案:拆fen
ArrayList<String> girlNameList = new ArrayList<>();
for (String s : girlNameTempList) {
String[] split = s.split(" ");
for (int i = 0; i < split.length; i++) {
girlNameList.add(split[i]);
}
}
//创建数据
/**
* 姓名(唯一)-性别-年龄
*/
ArrayList<String> namelist = getData(familyNameList, boyNameList, girlNameList, 70, 40);
Collections.shuffle(namelist);
//写出数据
FileUtil.writeLines(namelist,"C:\\Users\\20724\\Desktop\\name.txt","UTF-8");
}
public static ArrayList<String> getData(ArrayList<String> familyNameList,ArrayList<String> boyNameList,ArrayList<String> girlNameList,int boyname,int girlbame){
//男名字
HashSet<String> boyset = new HashSet<>();
while (true){
if (boyset.size() == boyname){
break;
}
Collections.shuffle(familyNameList);
Collections.shuffle(boyNameList);
boyset.add(familyNameList.get(0)+boyNameList.get(0));
}
//女名字
HashSet<String> grilset = new HashSet<>();
while (true){
if (grilset.size() == girlbame){
break;
}
Collections.shuffle(familyNameList);
Collections.shuffle(girlNameList);
grilset.add(familyNameList.get(0)+girlNameList.get(0));
}
ArrayList<String> namelist = new ArrayList<>();
Random r = new Random();
for (String s : boyset) {
int i = r.nextInt(10)+18;
namelist.add(s+"-男-"+i);
}
for (String s : grilset) {
int i = r.nextInt(8)+18;
namelist.add(s+"-女-"+i);
}
return namelist;
}
}
随机点名器
练习一:
public class Test1 {
public static void main(String[] args) throws IOException {
//"C:\\Users\\20724\\Desktop\\name.txt","UTF-8"
//创建读取流
BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\20724\\Desktop\\name.txt"));
//读取的数据使用list集合进行存储
ArrayList<String> list = new ArrayList<>();
String str;
while ((str = br.readLine()) != null){
list.add(str);
}
br.close();
//随机数获取要的名字
Random r = new Random();
int index = r.nextInt(list.size());
String s = list.get(index);
System.out.println("选中的同学的名字为:"+s.split("-")[0]);
}
}
练习二:
public class Test2 {
public static void main(String[] args) throws IOException {
//"C:\\Users\\20724\\Desktop\\name.txt","UTF-8"
//创建读取流
BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\20724\\Desktop\\name.txt"));
//读取的数据使用list集合进行存储
ArrayList<String> list = new ArrayList<>();
String str;
while ((str = br.readLine()) != null){
list.add(str);
}
br.close();
for (int i = 0; i < 1000000; i++) {
String name = RannderName(br,list);
System.out.println(name);
}
}
private static String RannderName(BufferedReader br,ArrayList<String> list) throws IOException {
//随机数获取要的名字
Random r = new Random();
//随机数确定要的是男还是女
int inderSex = r.nextInt(10)+1;
String s;
if (inderSex<=7){
while (true) {
//抽男生
int index = r.nextInt(list.size());
s = list.get(index);
//判断是否为男生
String[] split = s.split("-");
if (split[1].equals("男")){
//是男生
break;
}
}
}else {
while (true) {
//抽男生
int index = r.nextInt(list.size());
s = list.get(index);
//判断是否为女生
String[] split = s.split("-");
if (split[1].equals("女")){
//是女生
break;
}
}
}
String name = "选中的同学的名字为:"+s;
return name;
}
}
练习三:
public class Test3 {
public static void main(String[] args) throws IOException {
//"C:\\Users\\20724\\Desktop\\name.txt","UTF-8"
//创建读取流
BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\20724\\Desktop\\name.txt"));
//读取的数据使用list集合进行存储
ArrayList<String> list = new ArrayList<>();
String str;
while ((str = br.readLine()) != null){
list.add(str);
}
br.close();
//将运行次数存入文件,每次运行时读取判断是第几次运行
//读取文件
BufferedReader Runnum = new BufferedReader(new FileReader("C:\\Users\\20724\\Desktop\\demo\\javase2day01\\javaseday30\\a.txt"));
String s1 = Runnum.readLine();
Runnum.close();
int i = Integer.parseInt(s1);
if (i==3){
//显示张三并将文件加一
System.out.println("选中的同学的名字为:张三");
//写入数据
int n = i+1;
BufferedWriter bw = new BufferedWriter(new FileWriter("C:\\Users\\20724\\Desktop\\demo\\javase2day01\\javaseday30\\a.txt"));
bw.write(n+"");
bw.close();
return;
}
//随机数获取要的名字
Random r = new Random();
int index = r.nextInt(list.size());
String s = list.get(index);
System.out.println("选中的同学的名字为:"+s.split("-")[0]);
//写入数据
int n = i+1;
BufferedWriter bw = new BufferedWriter(new FileWriter("C:\\Users\\20724\\Desktop\\demo\\javase2day01\\javaseday30\\a.txt"));
bw.write(n+"");
bw.close();
}
}
练习四:
public class Test4 {
public static void main(String[] args) throws IOException {
//"C:\\Users\\20724\\Desktop\\name.txt","UTF-8"
//创建读取流,两个读取流每次运行将这两个都读取出来
BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\20724\\Desktop\\name.txt"));
BufferedReader Runnum = new BufferedReader(new FileReader("C:\\Users\\20724\\Desktop\\demo\\javase2day01\\javaseday30\\b.txt"));
//读取的数据使用list集合进行存储
ArrayList<String> list = new ArrayList<>();
ArrayList<String> next = new ArrayList<>();
String str;
while ((str = br.readLine()) != null){
list.add(str);
}
br.close();
String str2;
while ((str2 = Runnum.readLine()) != null){
next.add(str2);
}
Runnum.close();
//先进行判断是否已经读取一轮
if (list.size()==next.size()){
//清空已选文件
BufferedWriter bw = new BufferedWriter(new FileWriter("C:\\Users\\20724\\Desktop\\demo\\javase2day01\\javaseday30\\b.txt"));
bw.close();
return;
}
Random r = new Random();
//获取的名字
String s;
//随机数获取要的名字
while (true) {
int index = r.nextInt(list.size());
s = list.get(index);
//判断已选文件中有没有这个名字
if (next.contains(s)){
continue;
}
System.out.println("选中的同学的名字为:"+s.split("-")[0]);
break;
}
//写入数据
BufferedWriter writer = new BufferedWriter(new FileWriter("C:\\Users\\20724\\Desktop\\demo\\javase2day01\\javaseday30\\b.txt", true));
writer.write(s);
writer.newLine();
writer.close();
}
}
练习五:
public class Demo5 {
public static void main(String[] args) throws IOException {
//带权重的随机
BufferedReader br = new BufferedReader(new FileReader("javaseday30\\a.txt"));
//将数据存入Student对象存入集合中
ArrayList<Student> list = new ArrayList<>();
String str;
while ((str = br.readLine()) != null){
String[] split = str.split("-");
Student student = new Student(split[0],split[1],Integer.parseInt(split[2]),Double.parseDouble(split[3]));
list.add(student);
}
br.close();
//获取总的权重和
double weight = 0;
for (Student student : list) {
weight += student.getWeight();
}
//计算每个对象的权重,并存入数组中
double[] arr= new double[list.size()];
int count = 0;
for (Student student : list) {
arr[count] = student.getWeight()/weight;
count++;
}
//计算各个区间的数据
for (int i = 1; i < arr.length; i++) {
arr[i] = arr[i] + arr[i-1];
}
//进行随机得到0-1之间的随机数
double random = Math.random();
// System.out.println(random);
//判断random在arr 中的位置
//使用二分法查找,使用Arrays.binarySearch方法得到的返回值是 -插入点-1 = f
//据此获取位置
int i = Arrays.binarySearch(arr, random);
int index = -i-1;
// System.out.println(index);
//根据索引获取数据
System.out.println("抽到的数据是: "+list.get(index).toString());
//修改权重为2分之1;
list.get(index).setWeight(list.get(index).getWeight()/2);
//将数据写会文件中
BufferedWriter bw = new BufferedWriter(new FileWriter("javaseday30\\a.txt"));
for (Student student : list) {
bw.write(student.toString());
bw.newLine();
}
bw.close();
}
}
登录注册
练习一
public class Test1 {
public static void main(String[] args) throws IOException {
//链接
BufferedReader br = new BufferedReader(new FileReader("javaseday30\\userinfo.txt"));
//将数据读取到list集合中
ArrayList<User> list = new ArrayList<>();
String str;
while ((str = br.readLine())!=null){
//拆解字符串得到数据
String[] split = str.split("&");
String[] split1 = split[0].split("=");
String[] split2 = split[1].split("=");
User user = new User(split1[1],split2[1]);
list.add(user);
}
Scanner sc = new Scanner(System.in);
System.out.println("请输入用户名");
String name = sc.next();
System.out.println("请输入密码");
String password = sc.next();
boolean flag = true;
//遍历集合判断是否正确
for (User user : list) {
if (user.getName().equals(name)){
if (user.getPassword().equals(password)){
System.out.println("登录成功");
flag = false;
}
}
}
if (flag){
System.out.println("账号和密码错误");
}
}
}
public class User {
private String name;
private String password;
public User() {
}
public User(String name, String password) {
this.name = name;
this.password = password;
}
/**
* 获取
* @return name
*/
public String getName() {
return name;
}
/**
* 设置
* @param name
*/
public void setName(String name) {
this.name = name;
}
/**
* 获取
* @return password
*/
public String getPassword() {
return password;
}
/**
* 设置
* @param password
*/
public void setPassword(String password) {
this.password = password;
}
public String toString() {
return "User{name = " + name + ", password = " + password + "}";
}
}
练习二:
public class Test2 {
public static void main(String[] args) throws IOException {
//链接
BufferedReader br = new BufferedReader(new FileReader("javaseday30\\userinfo.txt"));
//将数据读取到list集合中
HashSet<User> list = new HashSet<>();
String str;
while ((str = br.readLine())!=null){
//拆解字符串得到数据
String[] split = str.split("&");
String[] split1 = split[0].split("=");
String[] split2 = split[1].split("=");
String[] split3 = split[2].split("=");
int i = Integer.parseInt(split3[1]);
User user = new User(split1[1],split2[1],i);
list.add(user);
}
Scanner sc = new Scanner(System.in);
System.out.println("请输入用户名");
String name = sc.next();
System.out.println("请输入密码");
String password = sc.next();
boolean flag = true;
//遍历集合判断是否正确
for (User user : list) {
if (user.getName().equals(name)){
//判断是否登录超过3次
if (user.getCount()>=3){
System.out.println("您已经连续输错三次密码,请与相关人员联系");
flag = false;
//修改登录次数
int n = user.getCount()+1;
user.setCount(n);
//写入文件
BufferedWriter bw = new BufferedWriter(new FileWriter("javaseday30\\userinfo.txt"));
for (User user1 : list) {
bw.write(user1.toString());
bw.newLine();
}
bw.close();
return;
}
if (user.getPassword().equals(password)){
System.out.println("登录成功");
flag = false;
//登录成功则将登录次数置为0
int n = 0;
user.setCount(n);
//写入文件
BufferedWriter bw = new BufferedWriter(new FileWriter("javaseday30\\userinfo.txt"));
for (User user1 : list) {
bw.write(user1.toString());
bw.newLine();
}
bw.close();
}else {
//修改登录次数
int n = user.getCount()+1;
user.setCount(n);
//写入文件
BufferedWriter bw = new BufferedWriter(new FileWriter("javaseday30\\userinfo.txt"));
for (User user1 : list) {
bw.write(user1.toString());
bw.newLine();
}
bw.close();
}
}
}
if (flag){
System.out.println("账号或密码错误");
}
}
}
修改了USer类,添加了登录次数和toString方法。
public class User {
private String name;
private String password;
private int count;
public User() {
}
public User(String name, String password, int count) {
this.name = name;
this.password = password;
this.count = count;
}
/**
* 获取
* @return name
*/
public String getName() {
return name;
}
/**
* 设置
* @param name
*/
public void setName(String name) {
this.name = name;
}
/**
* 获取
* @return password
*/
public String getPassword() {
return password;
}
/**
* 设置
* @param password
*/
public void setPassword(String password) {
this.password = password;
}
/**
* 获取
* @return count
*/
public int getCount() {
return count;
}
/**
* 设置
* @param count
*/
public void setCount(int count) {
this.count = count;
}
public String toString() {
return "username="+name+"&password="+password+"&count="+count;
}
}
练习三:
练习四:
练习三和练习四写在了一起
package com.itheima.ui;
import cn.hutool.core.io.FileUtil;
import com.itheima.domain.User;
import com.itheima.util.CodeUtil;
import javax.swing.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.List;
import static cn.hutool.core.map.MapUtil.getDate;
public class LoginJFrame extends JFrame implements MouseListener {
ArrayList<User> allUsers = new ArrayList<>();
JButton login = new JButton();
JButton register = new JButton();
JTextField username = new JTextField();
//JTextField password = new JTextField();
JPasswordField password = new JPasswordField();
JTextField code = new JTextField();
//正确的验证码
JLabel rightCode = new JLabel();
public LoginJFrame() {
//读取数据
getDate();
//初始化界面
initJFrame();
//在这个界面中添加内容
initView();
//让当前界面显示出来
this.setVisible(true);
}
private void getDate() {
//读取数据
List<String> list = FileUtil.readUtf8Lines("C:\\Users\\20724\\Desktop\\demo\\javase2day01\\puzzlegame\\userinfo.txt");
//处理数据
for (String s : list) {
String[] split = s.split("&");
String[] split1 = split[0].split("=");
String[] split2 = split[1].split("=");
String[] split3 = split2[1].split("-");
User u = new User(split1[1],split3[0],Integer.parseInt(split3[1]));
allUsers.add(u);
}
}
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);
code.addMouseListener(this);
this.getContentPane().add(code);
String codeStr = CodeUtil.getCode();
//设置内容
rightCode.setText(codeStr);
//绑定鼠标事件
rightCode.addMouseListener(this);
//位置和宽高
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(3);//设置关闭模式
this.setLocationRelativeTo(null);//居中
this.setAlwaysOnTop(true);//置顶
this.setLayout(null);//取消内部默认布局
}
//点击
@Override
public void mouseClicked(MouseEvent e) {
if (e.getSource() == login) {
System.out.println("点击了登录按钮");
//获取两个文本输入框中的内容
String usernameInput = username.getText();
String passwordInput = password.getText();
//获取用户输入的验证码
String codeInput = code.getText();
//创建一个User对象
User userInfo = new User(usernameInput, passwordInput,0);
System.out.println("用户输入的用户名为" + usernameInput);
System.out.println("用户输入的密码为" + passwordInput);
if (codeInput.length() == 0) {
showJDialog("验证码不能为空");
} else if (usernameInput.length() == 0 || passwordInput.length() == 0) {
//校验用户名和密码是否为空
System.out.println("用户名或者密码为空");
//调用showJDialog方法并展示弹框
showJDialog("用户名或者密码为空");
} else if (!codeInput.equalsIgnoreCase(rightCode.getText())) {
showJDialog("验证码输入错误");
} else if (contains(userInfo)) {
System.out.println("用户名和密码正确可以开始玩游戏了");
//关闭当前登录界面
this.setVisible(false);
//打开游戏的主界面
//需要把当前登录的用户名传递给游戏界面
new GameJFrame();
} else {
System.out.println("用户名或密码错误");
showJDialog("用户名或密码错误");
//更改登录错误的次数
//遍历集合看有没相同的用户名
for (User user : allUsers) {
if (user.getUsername().equals(usernameInput)){
user.setCount(user.getCount()+1);
//将数据写回文件
FileUtil.writeLines(allUsers,"C:\\Users\\20724\\Desktop\\demo\\javase2day01\\puzzlegame\\userinfo.txt","UTF-8");
}
}
}
} else if (e.getSource() == register) {
System.out.println("点击了注册按钮");
//关闭当前页面
this.setVisible(false);
//打开注册页面,并将数据传入
new RegisterJFrame(allUsers);
} else if (e.getSource() == rightCode) {
System.out.println("更换验证码");
//获取一个新的验证码
String code = CodeUtil.getCode();
rightCode.setText(code);
}
}
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 mousePressed(MouseEvent e) {
if (e.getSource() == login) {
login.setIcon(new ImageIcon("puzzlegame\\image\\login\\登录按下.png"));
} else if (e.getSource() == register) {
register.setIcon(new ImageIcon("puzzlegame\\image\\login\\注册按下.png"));
}
}
//松开按钮
@Override
public void mouseReleased(MouseEvent e) {
if (e.getSource() == login) {
login.setIcon(new ImageIcon("puzzlegame\\image\\login\\登录按钮.png"));
} else if (e.getSource() == register) {
register.setIcon(new ImageIcon("puzzlegame\\image\\login\\注册按钮.png"));
}
}
//鼠标划入
@Override
public void mouseEntered(MouseEvent e) {
}
//鼠标划出
@Override
public void mouseExited(MouseEvent e) {
}
//判断用户在集合中是否存在
public boolean contains(User userInput){
for (int i = 0; i < allUsers.size(); i++) {
User rightUser = allUsers.get(i);
//先判断重复错误登录的次数
if (userInput.getUsername().equals(rightUser.getUsername())){
int count = rightUser.getCount();
if (count>=3){
showJDialog("您密码错误的次数已经超过三次,账号被锁定,请联系相关人员处理");
return false;
}
}
if(userInput.getUsername().equals(rightUser.getUsername()) && userInput.getPassword().equals(rightUser.getPassword())){
//有相同的代表存在,返回true,后面的不需要再比了
rightUser.setCount(0);
//将数据写回文件
FileUtil.writeLines(allUsers,"C:\\Users\\20724\\Desktop\\demo\\javase2day01\\puzzlegame\\userinfo.txt","UTF-8");
return true;
}
}
//循环结束之后还没有找到就表示不存在
return false;
}
}
package com.itheima.ui;
import cn.hutool.core.io.FileUtil;
import com.itheima.domain.User;
import javax.swing.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
public class RegisterJFrame extends JFrame implements MouseListener {
//存储已经注册的用户信息,赋值为null等待写入数据
ArrayList<User> allUsers = null;
//提升三个输入框的变量的作用范围,让这三个变量可以在本类中所有方法里面可以使用。
JTextField username = new JTextField();
JTextField password = new JTextField();
JTextField rePassword = new JTextField();
//提升两个按钮变量的作用范围,让这两个变量可以在本类中所有方法里面可以使用。
JButton submit = new JButton();
JButton reset = new JButton();
public RegisterJFrame(ArrayList<User> allUsers) {
this.allUsers = allUsers;
initFrame();
initView();
setVisible(true);
}
//点击判断
@Override
public void mouseClicked(MouseEvent e) {
if (e.getSource() == submit) {
//点击注册按钮
//判断三个数据是否为空
String usernameText = username.getText();
String passwordText = password.getText();
String rePasswordText = rePassword.getText();
if (usernameText.length()==0){
showDialog("用户为空,请重新输入");
return;
}
if (passwordText.length()==0){
showDialog("密码为空,请重新输入");
return;
}
if (rePasswordText.length()==0){
showDialog("确认密码为空,请重新输入");
return;
}
//判断两个密码是否一致
if (!passwordText.equals(rePasswordText)){
showDialog("两次密码输入不相同,请重新输入");
return;
}
//判断用户名密码是否符合要求
if (!usernameText.matches("[a-zA-Z0-9_-]{4,16}")){
showDialog("用户名不符合要求,请重新输入");
return;
}
if (!passwordText.matches("\\S*(?=\\S{6,})(?=\\S*\\d)(?=\\S*[A-Z])\\S*")){
showDialog("密码不符合要求,至少包含1个小写字母,1个数字长度至少6位,请重新输入");
return;
}
//判断用户名是否重复
for (User user : allUsers) {
if (user.getUsername().equals(usernameText)){
showDialog("用户名已存在,请重新输入");
return;
}
}
//写入数据
User user = new User(usernameText,passwordText,0);
allUsers.add(user);
FileUtil.writeLines(allUsers,"C:\\Users\\20724\\Desktop\\demo\\javase2day01\\puzzlegame\\userinfo.txt","UTF-8");
//表示用户注册成功
showDialog("恭喜注册成功");
//关闭当前页面
this.setVisible(false);
//打开登录页面
new LoginJFrame();
} else if (e.getSource() == reset) {
//点击重置按钮,将三个输入框赋值为“”
//点击重置按钮,将三个输入框赋值为“”
username.setText("");
password.setText("");
rePassword.setText("");
}
}
@Override
public void mousePressed(MouseEvent e) {
if (e.getSource() == submit) {
submit.setIcon(new ImageIcon("puzzlegame\\image\\register\\注册按下.png"));
} else if (e.getSource() == reset) {
reset.setIcon(new ImageIcon("puzzlegame\\image\\register\\重置按下.png"));
}
}
@Override
public void mouseReleased(MouseEvent e) {
if (e.getSource() == submit) {
submit.setIcon(new ImageIcon("puzzlegame\\image\\register\\注册按钮.png"));
} else if (e.getSource() == reset) {
reset.setIcon(new ImageIcon("puzzlegame\\image\\register\\重置按钮.png"));
}
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
private void initView() {
//添加注册用户名的文本
JLabel usernameText = new JLabel(new ImageIcon("puzzlegame\\image\\register\\注册用户名.png"));
usernameText.setBounds(85, 135, 80, 20);
//添加注册用户名的输入框
username.setBounds(195, 134, 200, 30);
//添加注册密码的文本
JLabel passwordText = new JLabel(new ImageIcon("puzzlegame\\image\\register\\注册密码.png"));
passwordText.setBounds(97, 193, 70, 20);
//添加密码输入框
password.setBounds(195, 195, 200, 30);
//添加再次输入密码的文本
JLabel rePasswordText = new JLabel(new ImageIcon("puzzlegame\\image\\register\\再次输入密码.png"));
rePasswordText.setBounds(64, 255, 95, 20);
//添加再次输入密码的输入框
rePassword.setBounds(195, 255, 200, 30);
//注册的按钮
submit.setIcon(new ImageIcon("puzzlegame\\image\\register\\注册按钮.png"));
submit.setBounds(123, 310, 128, 47);
submit.setBorderPainted(false);
submit.setContentAreaFilled(false);
submit.addMouseListener(this);
//重置的按钮
reset.setIcon(new ImageIcon("puzzlegame\\image\\register\\重置按钮.png"));
reset.setBounds(256, 310, 128, 47);
reset.setBorderPainted(false);
reset.setContentAreaFilled(false);
reset.addMouseListener(this);
//背景图片
JLabel background = new JLabel(new ImageIcon("puzzlegame\\image\\register\\background.png"));
background.setBounds(0, 0, 470, 390);
this.getContentPane().add(usernameText);
this.getContentPane().add(passwordText);
this.getContentPane().add(rePasswordText);
this.getContentPane().add(username);
this.getContentPane().add(password);
this.getContentPane().add(rePassword);
this.getContentPane().add(submit);
this.getContentPane().add(reset);
this.getContentPane().add(background);
}
private void initFrame() {
//对自己的界面做一些设置。
//设置宽高
setSize(488, 430);
//设置标题
setTitle("拼图游戏 V1.0注册");
//取消内部默认布局
setLayout(null);
//设置关闭模式
setDefaultCloseOperation(3);
//设置居中
setLocationRelativeTo(null);
//设置置顶
setAlwaysOnTop(true);
}
//只创建一个弹框对象
JDialog jDialog = new JDialog();
//因为展示弹框的代码,会被运行多次
//所以,我们把展示弹框的代码,抽取到一个方法中。以后用到的时候,就不需要写了
//直接调用就可以了。
public void showDialog(String content){
if(!jDialog.isVisible()){
//把弹框中原来的文字给清空掉。
jDialog.getContentPane().removeAll();
JLabel jLabel = new JLabel(content);
jLabel.setBounds(0,0,200,150);
jDialog.add(jLabel);
//给弹框设置大小
jDialog.setSize(200, 150);
//要把弹框在设置为顶层 -- 置顶效果
jDialog.setAlwaysOnTop(true);
//要让jDialog居中
jDialog.setLocationRelativeTo(null);
//让弹框
jDialog.setModal(true);
//让jDialog显示出来
jDialog.setVisible(true);
}
}
}
package com.itheima.domain;
public class User {
private String username;
private String password;
private int count;
@Override
public String toString() {
return "username="+username+"&password="+password+"-"+count;
}
public User() {
}
public User(String username, String password,int count) {
this.username = username;
this.password = password;
this.count = count;
}
/**
* 获取
* @return username
*/
public String getUsername() {
return username;
}
/**
* 设置
* @param username
*/
public void setUsername(String username) {
this.username = username;
}
/**
* 获取
* @return password
*/
public String getPassword() {
return password;
}
/**
* 设置
* @param password
*/
public void setPassword(String password) {
this.password = password;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
}
游戏的存档和读档
游戏配置
public class Demo1 {
public static void main(String[] args) {
//创建集合对象
Properties properties = new Properties();
//Properties是一个Map集合,可以使用Map集合的方法,通常只存入字符串
properties.put(1,2);
properties.put(2,3);
properties.put(4,2);
properties.put(5,2);
properties.put(6,2);
//Properties的独特方法
//Map集合的方法
// Set<Map.Entry<Object, Object>> entries = properties.entrySet();
// for (Map.Entry<Object, Object> entry : entries) {
// System.out.println(entry.getKey()+"="+entry.getValue());
// }
// Set<Object> objects = properties.keySet();
// for (Object object : objects) {
// System.out.println(object+"="+properties.get(object));
// }
}
}
public class Demo2 {
public static void main(String[] args) throws IOException {
//Properties和IO流结合
// //创建集合对象
Properties properties = new Properties();
//Properties是一个Map集合,可以使用Map集合的方法,通常只存入字符串
properties.put("asd","dsdsd");
properties.put("mnkev","dsdsd");
BufferedWriter fos = new BufferedWriter(new FileWriter("C:\\Users\\20724\\Desktop\\demo\\javase2day01\\javaseday30\\a.properties"));
//参数二:对文件的注解
properties.store(fos,"test");
fos.close();
//普通的流操作
// BufferedWriter fos = new BufferedWriter(new FileWriter("C:\\Users\\20724\\Desktop\\demo\\javase2day01\\javaseday30\\a.properties"));
// String str;
// Set<Object> objects = properties.keySet();
// for (Object object : objects) {
// Object value = properties.get(object);
// fos.write(object+"="+value);
// fos.newLine();
// }
// fos.close();
}
}
public class Demo3 {
public static void main(String[] args) throws IOException {
//从文件中读取数据
Properties properties = new Properties();
FileInputStream fis = new FileInputStream("C:\\Users\\20724\\Desktop\\demo\\javase2day01\\javaseday30\\a.properties");
properties.load(fis);
fis.close();
System.out.println(properties.toString());
}
}
配置公众号显示的图片
} else if (obj == accountItem) {
System.out.println("公众号");
//创建一个弹框对象
JDialog jDialog = new JDialog();
//创建键一个配置文件对象
Properties properties = new Properties();
try {
//创建一个流读取配置文件
FileInputStream fis= new FileInputStream("puzzlegame\\game.properties");
//读取数据
properties.load(fis);
fis.close();
} catch (IOException ex) {
throw new RuntimeException(ex);
}
String path = (String) properties.get("acount");
//创建一个管理图片的容器对象JLabel
JLabel jLabel = new JLabel(new ImageIcon(path));
//设置位置和宽高
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);