用java写一个系统登陆界面的验证码验证
时间: 2023-05-21 09:06:36 浏览: 107
非常好的问题!以下是一个使用 Java Swing 编写的系统登录界面的验证码验证示例:
```
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.util.*;
import javax.swing.*;
import javax.swing.border.*;
import java.security.*;
import java.math.*;
public class Login extends JFrame implements ActionListener {
private JLabel userLabel, passLabel, verifyLabel;
private JTextField userField;
private JPasswordField passField;
private JTextField verifyField;
private JButton loginButton;
private JPanel mainPanel, buttonPanel;
private BufferedImage verifyImage;
private int verifyCode;
public Login() {
setTitle("系统登录");
setSize(400, 200);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
userLabel = new JLabel("用户名:");
passLabel = new JLabel("密码:");
verifyLabel = new JLabel("验证码:");
userField = new JTextField(20);
passField = new JPasswordField(20);
verifyField = new JTextField(10);
loginButton = new JButton("登录");
loginButton.addActionListener(this);
buttonPanel = new JPanel();
buttonPanel.add(loginButton);
mainPanel = new JPanel();
mainPanel.setLayout(new GridLayout(3, 2));
mainPanel.add(userLabel);
mainPanel.add(userField);
mainPanel.add(passLabel);
mainPanel.add(passField);
mainPanel.add(verifyLabel);
mainPanel.add(verifyField);
mainPanel.setBorder(new EmptyBorder(10, 10, 10, 10));
add(mainPanel, BorderLayout.CENTER);
add(buttonPanel, BorderLayout.SOUTH);
Random rand = new Random();
verifyCode = rand.nextInt(9000) + 1000;
try {
verifyImage = createVerifyImage(Integer.toString(verifyCode));
} catch (NoSuchAlgorithmException ex) {
System.err.println(ex.getMessage());
}
setVisible(true);
}
public void actionPerformed(ActionEvent evt) {
String username = userField.getText();
String password = new String(passField.getPassword());
String verify = verifyField.getText();
if (verify.equals(Integer.toString(verifyCode))) {
JOptionPane.showMessageDialog(this, "欢迎 " + username);
} else {
JOptionPane.showMessageDialog(this, "验证码错误");
}
}
private BufferedImage createVerifyImage(String code) throws NoSuchAlgorithmException {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(code.getBytes());
byte[] hash = md.digest();
String hex = new BigInteger(1, hash).toString(16);
int width = 120, height = 50;
BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g = img.createGraphics();
g.setColor(Color.WHITE);
g.fillRect(0, 0, width, height);
for (int i = 0; i < 100; i++) {
int x1 = (int)(Math.random() * width);
int y1 = (int)(Math.random() * height);
int x2 = (int)(Math.random() * width);
int y2 = (int)(Math.random() * height);
g.setColor(new Color((int)(Math.random() * 0x1000000)));
g.drawLine(x1, y1, x2, y2);
}
g.setColor(Color.BLACK);
g.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 24));
g.drawString(hex.substring(0, 4), 30, 30);
g.dispose();
return img;
}
public void paint(Graphics g) {
super.paint(g);
g.drawImage(verifyImage, 250, 25, this);
}
public static void main(String[] args) {
new Login();
}
}
```
该示例生成一个带有用户名、密码和验证码的登录窗体。在此示例中,验证码是使用 MD5 哈希对四位数的随机整数生成的。每次刷新验证码时,都会生成一个不同的随机数字。当用户单击“登录”按钮时,将检查输入的验证码是否与生成的验证码匹配。
这是一个简单的示例,您可以根据自己的需要进行自定义和扩展。
阅读全文