听写32个单词,阅读一篇英文日报;
完成离散课后作业;
优化验证码代码:
import javax.swing.;
import java.awt.;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Random;
//Graphics图形绘制抽象类,绘制直线、矩形、椭圆等
//Graphics2D继承Graphics,提供更多绘制方法
//Color设置图像颜色
//Font设置字体样式
//extends继承
//implements接口,接口只能有抽象方法以及常量;继承抽象类,必须重写其所有抽象方法(Java多继承的实现)
public class ValidCode extends JComponent implements MouseListener {
private String code;
private int width,height=40;
private int codeLength=4;
private Random random=new Random();
//验证码更换实现
public ValidCode() {
width = codeLength * 16 + (codeLength - 1) * 10;
setPreferredSize(new Dimension(width, height));
setSize(width, height);
this.addMouseListener(this);
setToolTipText("点击可以更换验证码");
}
public int getCodeLength() {
return codeLength;
}
public String getCode() {
return code;
}
//设置验证码文字的长度
public void setCodeLength(int Length) {
if (Length < 4) {
codeLength = 4;
} else {
codeLength = Length;
}
}
//产生随机的颜色
public Color getRandColor(int min, int max) {
if (min > 255)
min = 255;
if (max > 255)
max = 255;
int red = random.nextInt(max - min) + min;
int green = random.nextInt(max - min) + min;
int blue = random.nextInt(max - min) + min;
return new Color(red, green, blue);
}
//设置验证码具体的字母是什么
protected String generateCode(){
char[] codes =new char[codeLength];
for(int i=0,len=codeLength;i<len;i++){
int flag=random.nextInt(2);
if(flag==0){
codes[i] = (char) (random.nextInt(26) + 65);
}else{
codes[i] = (char) (random.nextInt(26) + 97);
}
}
code = new String(codes);
return code;
}
@Override
protected void paintComponent(Graphics g) {
if (code == null || code.length() != codeLength) {
code = generateCode();
}
width = codeLength * 16 + (codeLength - 1) * 10;
setSize(width, height);
setPreferredSize(new Dimension(width, height));
//Font字体类,style:Font.PLAIN(普通)、Font.BOLD(加粗)、Font.ITALIC(斜体)、Font.BOLD+Font.ITALIC(粗斜体)
Font mFont = new Font("Arial", Font.BOLD | Font.ITALIC, 25);
g.setFont(mFont);
//绘制出验证码的背景的矩形轮廓
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(getRandColor(200, 250));
g2d.fillRect(0, 0, width, height);
g2d.setColor(getRandColor(180, 200));
g2d.drawRect(0, 0, width - 1, height - 1);
//绘制出验证码背景的干扰线
int i = 0, len = 150;
for (; i < len; i++) {
int x = random.nextInt(width - 1);
int y = random.nextInt(height - 1);
int x1 = random.nextInt(width - 10) + 10;
int y1 = random.nextInt(height - 4) + 4;
g2d.setColor(getRandColor(180, 200));
g2d.drawLine(x, y, x1, y1);
}
//绘制出验证码的具体字母
i = 0;
len = codeLength;
FontMetrics fm = g2d.getFontMetrics();
int base = (height - fm.getHeight()) / 2 + fm.getAscent();
for (; i < len; i++) {
int b = random.nextBoolean() ? 1 : -1;
g2d.rotate(random.nextInt(10) * 0.01 * b);
g2d.setColor(getRandColor(20, 130));
g2d.drawString(code.charAt(i) + "", 16 * i + 10, base);
}
}
//下一个验证码
public void nextCode() {
generateCode();
repaint();
}
//读取鼠标左键操作
@Override
public void mouseClicked(MouseEvent e) {
nextCode();
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
}
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Objects;
public class LoginJFrame extends JFrame implements ActionListener {
JLabel jLabel;//标签,显示文本
JTextField jt_code;//输入框
JButton confirm;//确认按钮
private ValidCode vcode = new ValidCode();
public LoginJFrame() {
//验证码文本和按钮设置
jLabel = new JLabel("验证码:");
jt_code=new JTextField();
confirm = new JButton("确认");
jLabel.setBounds(50, 10, 60, 40);
jt_code.setBounds(120,10,120,40);
confirm.setBounds(120,50,80,30);
vcode.setBounds(250, 10, 100, 40);
confirm.addActionListener(this);
//写入容器
this.setLayout(null);
this.add(jLabel);
this.add(jt_code);
this.add(vcode);
this.add(confirm);
this.setTitle("验证码的实现");
//容器设置
this.setSize(400,130);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.setResizable(false);
}
private void clear(){
jt_code.setText("");
}
public Boolean isValidCodeRight(){
if(jt_code==null){
return false;
} else if (vcode==null) {
return true;
} else if (vcode.getCode().equals(jt_code.getText())) {
return true;
}else {
return false;
}
}
@Override
public void actionPerformed(ActionEvent e) {
if (Objects.equals(e.getActionCommand(), "确认")) {
if (!isValidCodeRight()) {
JOptionPane.showMessageDialog(null, "验证码错误,请重新输入!", "提示消息", JOptionPane.WARNING_MESSAGE);
clear();
} else {
JOptionPane.showMessageDialog(null, "验证通过!", "提示消息", JOptionPane.WARNING_MESSAGE);
clear();
}
}
}
}
public class text {
public static void main(String[] args) {
new LoginJFrame();
}
}