JFrame 窗口
setSize(600,400); 设置窗口大小
setBounds(200,50,100,30); 调整按钮的位置和大小。
setFont(new Font("微软雅黑",Font.PLAIN,20)) 设置字体,是否加粗,字号大小
setForeground(Color.RED);设置字体颜色
setCursor(new Cursor(Cursor.HAND_CURSOR));设置为小手
addMouseListener添加鼠标监听器
setLocationRelativeTo(null); 窗口放置到的屏幕中心。
setTitle("登录注册界面"); 设置登录窗口的标题文字
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 设置X号关闭窗口功能
setResizable(false); 用于禁止用户更改窗口的大小
setVisible(true) 设置窗口可见
JPanel容器
JButton按钮
JTabbedPane 选项卡容器
jtp.setSelectedIndex(1);选中索引为1的选项卡
jtp.remove(1);移除
JTextField文本输入框
JLabel创建标签组件,通常用于在GUI界面上显示文本或图片
JPasswordField 密码输入框,密文
requestFocus(); 设置焦点
JOptionPane.showMessageDialog(MyFrame.this,"验证码错误") 弹窗,在MyFrame窗口上弹窗。
demo测试类
package test13;
public class Demo {
public static void main(String[] args) {
MyFrame mf=new MyFrame();
}
}
MyFrame窗口类
package test13;
import test07.Demo01;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Random;
public class MyFrame extends JFrame {
private String phoneNumber;
private JLabel registLabel;
private JTabbedPane jtp;
private JPanel loginPanel;
public MyFrame(){
init();
this.setVisible(true);
}
private void init(){
this.setSize(600,400);
this.setLocationRelativeTo(null);
this.setTitle("登录注册界面");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
// JTabbedPane 选项卡容器
jtp=new JTabbedPane();
//选项卡一 登录
JPanel jp=new JPanel(); // 默认为中间开始的流式布局。
JButton login=new JButton("登录");
JButton regist=new JButton("注册");
regist.addActionListener( //对注册按钮添加监听器,点击注册后,注册选项卡出现,并跳转到注册选项卡。
e -> {
//选项卡二 注册
JPanel jp1=new JPanel(); //创建容器
jp1.setLayout(null); //设置为空布局
JButton zhuce=new JButton("注册成功"); //设置按钮
zhuce.setBounds(200,50,100,30); //调整按钮的位置和大小。
zhuce.addActionListener(ae->{ //点击注册成功按钮,关闭注册选项卡,并跳转到0索引的登录选项卡。
jtp.remove(1);
jtp.setSelectedIndex(0);
regist.setVisible(true); //**点击注册成功,就应该让登录选项卡下的注册按钮再次显示出来
});
jp1.add(zhuce); //把按钮添加到容器中
jtp.addTab("注册",jp1); //设置选项卡名称,把设置为空布局的容器放到选项卡容器中,显示出来
jtp.setSelectedIndex(1); //注册选项卡在jtp容器中,选中注册选项卡。
regist.setVisible(false); //**当没有点击注册选项卡下的”注册成功“按钮前,让登录选项卡下的“注册”按钮始终隐藏
}
);
jp.add(login);
jp.add(regist);
//初始化 登录标签卡里面的容器。
initLoginTab();
jtp.addTab("登录",loginPanel); //为选项卡添加登录标题,并添加流式布局的容器jp.
this.add(jtp); //把选项卡容器添加到窗口中。
}
private void initLoginTab(){
loginPanel=new JPanel();
loginPanel.setLayout(null);
//添加组件 很多代码
//手机号 JLabel
JLabel phoneNumberLabel=new JLabel("手机号");
phoneNumberLabel.setBounds(100,40,70,40);
phoneNumberLabel.setFont(new Font("微软雅黑",Font.PLAIN,20));
//手机号输入 JTextField
JTextField phoneNumberTextField=new JTextField();
phoneNumberTextField.setBounds(200,40,300,40);
//密码 JLabel
JLabel passwordLabelLabel=new JLabel("密 码");
passwordLabelLabel.setBounds(100,110,70,40);
passwordLabelLabel.setFont(new Font("微软雅黑",Font.PLAIN,20));
//密码输入 JPasswordField
JPasswordField passwordTextField=new JPasswordField();
passwordTextField.setBounds(200,110,300,40);
//验证码 JLabel
JLabel veriCodeLabel=new JLabel("验证码");
veriCodeLabel.setBounds(100,180,70,40);
veriCodeLabel.setFont(new Font("微软雅黑",Font.PLAIN,20));
//验证码输入 JTextField
JTextField verifyCodeTextField=new JTextField();
verifyCodeTextField.setBounds(200,180,150,40);
//验证码显示区 JLabel
//设置随机验证码方法
RandomUtil r=new RandomUtil(); //创建随机验证码对象
String random = r.random(); //获取返回的随机码,赋值给random。
JLabel veriCodeDisplayLabel=new JLabel(random); //需要设置字符串,正好random是个字符串
veriCodeDisplayLabel.setBounds(400,180,100,40);
veriCodeDisplayLabel.setFont(new Font("微软雅黑",Font.PLAIN,20));
veriCodeDisplayLabel.setForeground(Color.RED);
veriCodeDisplayLabel.setCursor(new Cursor(Cursor.HAND_CURSOR));
veriCodeDisplayLabel.addMouseListener(
new MouseAdapter(){
@Override
public void mouseClicked(MouseEvent e){
String s=r.random();
veriCodeDisplayLabel.setText(s);
}
}
);
//登录按钮 JButton
JButton loginButton =new JButton("登录");
loginButton.setBounds(220,240,150,40);
loginButton.setFont(new Font("微软雅黑",Font.PLAIN,20));
loginButton.addActionListener(e -> {
//获取手机号,密码,验证码
String phoneNumber = phoneNumberTextField.getText();
String password = passwordTextField.getText();
String verifyCode = verifyCodeTextField.getText();
//比对手机号,比对密码,比对验证码
//1.校验手机号格式
String[] messeges = StringUtils.verifyPhoneNumber(phoneNumber);
if (messeges[1].equals("RED")) {
JOptionPane.showMessageDialog(MyFrame.this,messeges[0]);
passwordTextField.requestFocus();
return;
}
//2.判断手机号是否存在。
ArrayList allUsers = IoUtils.findAllUsers();
int index = StringUtils.isExist(phoneNumber, allUsers);
if(index==-1){
JOptionPane.showMessageDialog(MyFrame.this,"用户名不存在");
phoneNumberTextField.requestFocus();
return;
}
//3.比对密码
String phoneNumberAndPassword = allUsers.get(index);
String pwd = phoneNumberAndPassword.substring(11);
if (password.equals(pwd)) {
JOptionPane.showMessageDialog(MyFrame.this,"密码错误");
passwordTextField.requestFocus();
return;
}
//4.比对验证码
String text = veriCodeDisplayLabel.getText();//验证码显示区显示的内容
if (!verifyCode.equalsIgnoreCase(text)) {
JOptionPane.showMessageDialog(MyFrame.this,"验证码错误");
verifyCodeTextField.requestFocus();
return;
}
//关闭当前登录窗口,跳转到猜数字游戏窗口/抽名字窗口
MyFrame.this.dispose(); //关闭当前窗口的方法
// JFrame jf=new JFrame(); //可以跳转到另外一个带有游戏或者其他功能界面的窗口。这里新建了一个空的窗口。
// jf.setVisible(true);
Demo01 demo01=new Demo01();
});
//注册 JLabel
registLabel=new JLabel("注册");
registLabel.setBounds(380,260,40,25);
registLabel.setFont(new Font("微软雅黑",Font.PLAIN,13));
registLabel.setForeground(Color.BLUE);
registLabel.setCursor(new Cursor(Cursor.HAND_CURSOR));
registLabel.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
initRegistTab();
registLabel.setVisible(false);//这个不放到initRegistTab()方法里,因为是局部变量
}
});
loginPanel.add(phoneNumberLabel);
loginPanel.add(phoneNumberTextField);
loginPanel.add(passwordLabelLabel);
loginPanel.add(passwordTextField);
loginPanel.add(veriCodeLabel);
loginPanel.add(verifyCodeTextField);
loginPanel.add(veriCodeDisplayLabel);
loginPanel.add(loginButton);
loginPanel.add(registLabel);
}
/*
初始化注册标签卡里的容器
*/
private void initRegistTab(){
JPanel registPanel =new JPanel();
registPanel.setLayout(null);
//添加组件 很多代码
//手机号 JLabel
JLabel phoneNumberLabel=new JLabel("手机号");
phoneNumberLabel.setBounds(100,40,70,40);
phoneNumberLabel.setFont(new Font("微软雅黑",Font.PLAIN,20));
//手机号输入 JTextField
JTextField phoneNumberTextField=new JTextField();
phoneNumberTextField.setBounds(200,40,300,40);
//手机号输入校验
JLabel phoneNumberTip=new JLabel();
phoneNumberTip.setBounds(200,80,200,20);
phoneNumberTip.setFont(new Font("微软雅黑",Font.PLAIN,10));
phoneNumberTextField.addFocusListener(new FocusAdapter() { //焦点失去时,给出提示。focusGained focusLost
@Override
public void focusLost(FocusEvent e) {
phoneNumber = phoneNumberTextField.getText();
//用户没写东西
String[] messeges=StringUtils.verifyPhoneNumber(phoneNumber);
phoneNumberTip.setText(messeges[0]); //设置文字提示
if(messeges[1].equals("RED")){
phoneNumberTip.setForeground(Color.RED);
phoneNumberTextField.requestFocus();//在输入正确前,设置焦点到输入框
}
else if(messeges[1].equals("BLUE")){
ArrayList allUsers = IoUtils.findAllUsers();
int index = StringUtils.isExist(phoneNumber, allUsers);
if(index!=-1) { //exist 是true就是存在,给提示红色已存在
phoneNumberTip.setText("手机号已经被注册");
phoneNumberTip.setForeground(Color.RED);
return;
}
phoneNumberTip.setForeground(Color.BLUE);
}
}
});
//密码 JLabel
JLabel passwordLabelLabel=new JLabel("密 码");
passwordLabelLabel.setBounds(100,110,70,40);
passwordLabelLabel.setFont(new Font("微软雅黑",Font.PLAIN,20));
//密码输入 JPasswordField
JPasswordField passwordTextField=new JPasswordField();
passwordTextField.setBounds(200,110,300,40);
//密码错误提示框
JLabel passwordTip=new JLabel();
passwordTip.setBounds(200,153,200,20);
passwordTip.setFont(new Font("微软雅黑",Font.PLAIN,10));
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);
}
}
});
//确认密码 JLabel //修改为确认密码
JLabel confirmPasswordLabel=new JLabel("确认密码");
confirmPasswordLabel.setBounds(100,180,100,40);
confirmPasswordLabel.setFont(new Font("微软雅黑",Font.PLAIN,20));
//确认密码输入 JTextField
JPasswordField confirmPasswordTextField=new JPasswordField();
confirmPasswordTextField.setBounds(200,180,300,40);
//确认密码是否一致提示框
JLabel confirmPasswordTip=new JLabel();
confirmPasswordTip.setBounds(200,223,200,20);
confirmPasswordTip.setFont(new Font("微软雅黑",Font.PLAIN,10));
confirmPasswordTextField.addFocusListener(
new FocusAdapter() {
@Override
public void focusLost(FocusEvent e) {
String password = passwordTextField.getText();
String confirmPassword = confirmPasswordTextField.getText();
if(confirmPassword.equals(password)){
confirmPasswordTip.setForeground(Color.BLUE);
confirmPasswordTip.setText("密码输入一致");
}
else {
confirmPasswordTip.setText("两次密码输入不一致");
confirmPasswordTip.setForeground(Color.RED);
}
}
});
//注册按钮 JButton
JButton registButton =new JButton("注册");
registButton.setBounds(220,250,150,40);
registButton.setFont(new Font("微软雅黑",Font.PLAIN,20));
registButton.addActionListener(
e->{
//继续校验一遍
String phoneNumber = phoneNumberTextField.getText();
String[] message = StringUtils.verifyPhoneNumber(phoneNumber);
if(message[1].equals("RED")){
//弹窗,JOptionPane.showMessageDialog,在窗口MyFrame上弹窗。
JOptionPane.showMessageDialog(MyFrame.this,message[0]);
return;
}
ArrayList allUsers=IoUtils.findAllUsers();
int index = StringUtils.isExist(phoneNumber, allUsers);
if(index!=-1){ //exist 是true的话就是存在,是不允许的
JOptionPane.showMessageDialog(MyFrame.this,"手机号已经被注册了");
return;
}
String password = passwordTextField.getText();
message = StringUtils.verifyPassword(password);
if(message[1].equals("RED")){
JOptionPane.showMessageDialog(MyFrame.this,message[0]);
return;
}
String confirmPassword = confirmPasswordTextField.getText();
if(!confirmPassword.equals(password)){
JOptionPane.showMessageDialog(MyFrame.this,"密码和确认密码不一致");
return;
}
//保存手机号,密码
try{
OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(MyFrame.class.getClassLoader().getResource("user.txt").getPath(),true), "UTF-8");
BufferedWriter bw = new BufferedWriter((writer));
bw.write(phoneNumber+password);
bw.newLine();//换行
bw.flush();//刷新
bw.close();//关闭
}
catch (Exception ex){
ex.printStackTrace();
}
//提示注册成功
JOptionPane.showMessageDialog(MyFrame.this,"注册成功!");
//关闭注册界面 显示登录界面的注册按钮
jtp.remove(registPanel);
registLabel.setVisible(true);
});
registPanel.add(phoneNumberLabel);
registPanel.add(phoneNumberTextField);
registPanel.add(passwordLabelLabel);
registPanel.add(passwordTextField);
registPanel.add(passwordTip);
registPanel.add(confirmPasswordLabel);
registPanel.add(confirmPasswordTextField);
registPanel.add(confirmPasswordTip);
registPanel.add(registButton);
registPanel.add(phoneNumberTip);
jtp.addTab("注册",registPanel); //生成注册选项卡。
jtp.setSelectedIndex(1); //选中注册选项卡,即跳转到注册界面
}
}
IoUtils类
package test13;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
public class IoUtils {
private IoUtils(){} //把构造方法私有,不让别的类创建对象使用
public static ArrayList findAllUsers(){
ArrayList al=new ArrayList<>();
try {
//读取文件中所有的用户名和密码
InputStreamReader reader = new InputStreamReader(IoUtils.class.getClassLoader().getResourceAsStream("user.txt"), "UTF-8");
BufferedReader br=new BufferedReader(reader);
String line;
while ((line=br.readLine())!=null){
if(!line.equals("")){
//把所有的用户名和密码装进集合中
al.add(line);
}
}
br.close();
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
//把集合返回出去
return al;
}
}
验证码类
package test13;
public class RandomUtil {
public String random() {
java.util.Random r = new java.util.Random();
String code = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 4; i++) {
int index = r.nextInt(code.length());
char c = code.charAt(index);
sb.append(c);
}
String random = sb.toString();
return random;
}
}
StringUtils工具类
package test13;
import java.awt.*;
import java.awt.event.ActionListener;
import java.util.ArrayList;
public class StringUtils {
private StringUtils() {
} //把构造方法设置为私有,不让别的类通过创建对象来调用。
public static String[] verifyPhoneNumber(String phoneNumber) { //返回一个字符串数组。 设置为有参数方法。
if (phoneNumber == null || phoneNumber.equals("")) {
return new String[]{"手机号不能为空", "RED"};
}
if (phoneNumber.length() != 11) {
return new String[]{"手机号必须是11位", "RED"};
}
if (phoneNumber.charAt(0) != '1') {
return new String[]{"手机号首位必须是1开头", "RED"};
}
for (int i = 0; i < phoneNumber.length(); i++) {
char c = phoneNumber.charAt(i);
if (c < '0' || c > '9') { //字符用单引号括起来。
return new String[]{"手机号里必须全部都是数字", "RED"};
}
}
return new String[]{"手机号可用", "BLUE"};
}
public static String[] verifyPassword(String password) {
if (password.length() < 6 || password.length() > 18) {
return new String[]{"密码不得小于6且不大于18位", "RED"};
}
int noNumCount = 0;
for (int i = 0; i < password.length(); i++) {
char c = password.charAt(i);
if (c < '0' || c > '9') {
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 > 'A' && c < 'a') || c > 'z') {
noLetterCount++;
}
}
if (noLetterCount == password.length()) {
return new String[]{"密码中应该包含字母", "RED"};
}
return new String[]{"密码格式正确", "BLUE"};
}
public static int isExist(String phoneNumber, ArrayList al){
for (int i = 0; i < al.size(); i++) {
String phoneAndPassword = al.get(i);
String phone = phoneAndPassword.substring(0, 11);
if(phoneNumber.equals(phone)){
return i;
}
}
return -1;
}
}
/**
* 判断手机号是否存在集合al中
* @param phoneNumber 手机号
* @param al 集合中装有所的手机号和密码(每一条是一个手机号和密码的拼接)
* @return 如果存在返回true ,如果不存在返回false。
*/
/** public static boolean isExist(String phoneNumber, ArrayList al){
for (int i = 0; i < al.size(); i++) {
String phoneAndPassword = al.get(i);
String phone = phoneAndPassword.substring(0, 11);
if(phoneNumber.equals(phone)){
return true;
}
}
return false;
}
}
*/