第十一次实验课代码
添加C08~C011
This commit is contained in:
11
C10-Swing/C10-Swing.iml
Normal file
11
C10-Swing/C10-Swing.iml
Normal file
@@ -0,0 +1,11 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="JAVA_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
||||
7
C10-Swing/src/Main.java
Normal file
7
C10-Swing/src/Main.java
Normal file
@@ -0,0 +1,7 @@
|
||||
import view.LoginFrame;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
new LoginFrame();
|
||||
}
|
||||
}
|
||||
54
C10-Swing/src/utils/CaptchaUtils.java
Normal file
54
C10-Swing/src/utils/CaptchaUtils.java
Normal file
@@ -0,0 +1,54 @@
|
||||
package utils;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
/**
|
||||
* 验证码工具类
|
||||
*/
|
||||
public class CaptchaUtils { // final修饰类:不能被继承
|
||||
// 【任务1】静态常量:字符集(全局固定)
|
||||
static final String CHAR_SET="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
||||
|
||||
// 【任务2】静态常量:验证码长度(全局固定)
|
||||
static final int CAPTCHA_LENGTH=4;
|
||||
|
||||
// 【任务3】私有构造方法:禁止创建实例(工具类无需实例化)
|
||||
private CaptchaUtils(){
|
||||
}
|
||||
|
||||
/**
|
||||
* 【任务4】
|
||||
* 静态方法:generateCaptcha 生成随机验证码
|
||||
* @return String 返回生成的随机验证码字符串
|
||||
*/
|
||||
public static String generateCaptcha(){
|
||||
Random random = new Random();
|
||||
//生成随机验证码
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for(int i=0;i<CAPTCHA_LENGTH;i++){
|
||||
//生成随机数索引
|
||||
int index = random.nextInt(CHAR_SET.length());
|
||||
//根据索引获取随机字符
|
||||
sb.append(CHAR_SET.charAt(index));
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
/**
|
||||
* 【任务5】
|
||||
* 静态方法:verifyCaptcha 验证验证码
|
||||
* @param inputCaptcha 用户输入的验证码
|
||||
* @param systemCaptcha 系统生成的验证码
|
||||
* @return boolean 验证结果,true表示验证通过,false表示验证失败
|
||||
*/
|
||||
public static boolean verifyCaptcha(String inputCaptcha,String systemCaptcha){
|
||||
return inputCaptcha.equalsIgnoreCase(systemCaptcha);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
for(int i=0;i<10;i++)
|
||||
System.out.println(generateCaptcha());
|
||||
|
||||
System.out.println(CaptchaUtils.verifyCaptcha(generateCaptcha(), generateCaptcha()));
|
||||
}
|
||||
|
||||
}
|
||||
129
C10-Swing/src/view/LoginFrame.java
Normal file
129
C10-Swing/src/view/LoginFrame.java
Normal file
@@ -0,0 +1,129 @@
|
||||
package view;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import utils.CaptchaUtils;
|
||||
|
||||
public class LoginFrame extends JFrame {
|
||||
private JPanel mainPanel;//中间层面板
|
||||
private JTextField usernameTextField;//用户名输入框
|
||||
private JPasswordField passwordField;//密码输入框
|
||||
private JTextField captchaTextField;//验证码输入框
|
||||
private JLabel captchaLabel;//验证码标签
|
||||
private JButton loginButton;//登录按钮
|
||||
private JButton registerButton;//注册按钮
|
||||
public LoginFrame() {
|
||||
initView();
|
||||
}
|
||||
//顶层容器
|
||||
private void initView(){
|
||||
//1.设置标题
|
||||
setTitle("登录");
|
||||
//2.设置点击关闭时的动作
|
||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
//3.中间层容器
|
||||
add(initPanel());
|
||||
//4.不允许窗口改变大小
|
||||
setResizable(false);
|
||||
//5.自动调整窗口大小 居中显示
|
||||
pack();//自动调整窗口大小
|
||||
setLocationRelativeTo(null);//剧中
|
||||
//6.显示窗口
|
||||
setVisible(true);
|
||||
}
|
||||
private JPanel initPanel(){
|
||||
mainPanel = new JPanel();
|
||||
GridLayout gridLayout = new GridLayout(6,1,10,10);
|
||||
mainPanel.setLayout(gridLayout);
|
||||
//第1层 空白面板 占用
|
||||
mainPanel.add(new JPanel());
|
||||
//第2层 账号面板
|
||||
mainPanel.add(initAcountPanel());
|
||||
//第3层 密码面板
|
||||
mainPanel.add(initPasswordPanel());
|
||||
//第4层 验证码面板
|
||||
mainPanel.add(initCaptchaPanel());
|
||||
//第5层 按钮面板
|
||||
mainPanel.add(initButtonPanel());
|
||||
//第6层 空白面板 占用
|
||||
return mainPanel;
|
||||
}
|
||||
/**
|
||||
* 初始化账户信息面板
|
||||
* @return 包含用户名输入框的面板组件
|
||||
*/
|
||||
private JPanel initAcountPanel(){
|
||||
JPanel panel = new JPanel();
|
||||
panel.setLayout(new GridLayout(1,4,10,0));
|
||||
JLabel label = new JLabel("用户名:");
|
||||
//让label居右
|
||||
label.setHorizontalAlignment(SwingConstants.RIGHT);
|
||||
usernameTextField = new JTextField(15);
|
||||
panel.add(label);
|
||||
panel.add(usernameTextField);
|
||||
panel.add(new JPanel());
|
||||
return panel;
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化密码输入面板
|
||||
* @return 包含密码标签和输入框的JPanel面板
|
||||
*/
|
||||
private JPanel initPasswordPanel(){
|
||||
JPanel panel = new JPanel();
|
||||
// 设置网格布局,1行4列,水平间距10像素
|
||||
panel.setLayout(new GridLayout(1,4,10,0));
|
||||
JLabel label = new JLabel("密 码:");
|
||||
// 设置标签文本右对齐
|
||||
label.setHorizontalAlignment(SwingConstants.RIGHT);
|
||||
passwordField = new JPasswordField(15);
|
||||
panel.add(label);
|
||||
panel.add(passwordField);
|
||||
// 添加空白面板占位
|
||||
panel.add(new JPanel());
|
||||
return panel;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 初始化验证码面板
|
||||
*
|
||||
* @return 包含验证码相关组件的JPanel面板
|
||||
*/
|
||||
private JPanel initCaptchaPanel(){
|
||||
// 创建面板并设置网格布局
|
||||
JPanel panel = new JPanel();
|
||||
panel.setLayout(new GridLayout(1,4,10,0));
|
||||
|
||||
// 创建验证码标签
|
||||
JLabel label = new JLabel("验证码:");
|
||||
label.setHorizontalAlignment(SwingConstants.RIGHT);
|
||||
|
||||
// 创建验证码输入框和验证码显示标签
|
||||
captchaTextField = new JTextField(4);
|
||||
captchaLabel = new JLabel(CaptchaUtils.generateCaptcha());
|
||||
|
||||
// 将组件添加到面板中
|
||||
panel.add(label);
|
||||
panel.add(captchaTextField);
|
||||
panel.add(captchaLabel);
|
||||
|
||||
return panel;
|
||||
}
|
||||
|
||||
|
||||
private JPanel initButtonPanel(){
|
||||
JPanel panel = new JPanel();
|
||||
panel.setLayout(new GridLayout(1,4,10,0));
|
||||
loginButton = new JButton("登录");
|
||||
registerButton = new JButton("注册");
|
||||
panel.add(new JPanel());
|
||||
panel.add(loginButton);
|
||||
panel.add(registerButton);
|
||||
panel.add(new JPanel());
|
||||
return panel;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user