图形登录与验证码
package login; import java.awt.Color; import java.awt.EventQueue; import java.awt.Label; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFormattedTextField; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPasswordField; import javax.swing.JTextField; import java.awt.Color; import java.awt.Graphics; import java.awt.image.BufferedImage; import java.io.IOException; import java.util.Random; import java.awt.Color; import java.util.Random; import java.util.UUID; import java.util.concurrent.ThreadLocalRandom; public class show { private JFrame frame; private JPasswordField passwordField; private JTextField jtfField; private boolean isLogin = false; private boolean isYanzheng = false; private String code; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { show window = new show(); window.frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the application. */ public show() { initialize(); } /** * Initialize the contents of the frame. */ private void initialize() { String userName = "xrj"; String userPwd = "xrj"; frame = new JFrame(); frame.setTitle("系统登录界面"); frame.setBounds(100, 100, 500, 250); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().setLayout(null); Label label = new Label("登录名:"); label.setAlignment(Label.CENTER); label.setBounds(116, 49, 50, 23); frame.getContentPane().add(label); Label label_1 = new Label("密码:"); label_1.setAlignment(Label.CENTER); label_1.setBounds(116, 85, 50, 23); frame.getContentPane().add(label_1); Label label_2 = new Label("忘记密码"); label_2.setBounds(350, 85, 60, 23); frame.getContentPane().add(label_2); Label label_3 = new Label("未登录"); label_3.setForeground(new Color(255, 0, 0)); label_3.setBounds(350, 49, 40, 23); frame.getContentPane().add(label_3); Label label_4 = new Label("验证码:"); label_4.setAlignment(Label.CENTER); label_4.setBounds(116, 120, 50, 23); frame.getContentPane().add(label_4); String code = UUID.randomUUID().toString().substring(0, 4); Label label_5 = new Label(code); label_5.setAlignment(Label.CENTER); label_5.setBounds(250, 120, 50, 23); frame.getContentPane().add(label_5); Label label_6 = new Label("等待验证"); label_6.setForeground(new Color(255, 0, 0)); label_6.setBounds(350, 120, 40, 23); frame.getContentPane().add(label_6); JFormattedTextField formattedTextField = new JFormattedTextField(); formattedTextField.setBounds(172, 49, 166, 23); frame.getContentPane().add(formattedTextField); passwordField = new JPasswordField(); passwordField.setBounds(172, 85, 166, 23); frame.getContentPane().add(passwordField); jtfField = new JTextField(); jtfField.setBounds(172, 120, 100, 23); frame.getContentPane().add(jtfField); JButton button2 = new JButton("快速注册"); button2.setBackground(new Color(255, 255, 255)); button2.setBounds(300, 180, 100, 23); frame.getContentPane().add(button2); JButton button1 = new JButton("登录"); button1.setBackground(new Color(255, 255, 255)); button1.setBounds(126, 180, 100, 23); frame.getContentPane().add(button1); button1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String getUserName = formattedTextField.getText(); String getUserPwd = passwordField.getText(); String getUseryanzh = jtfField.getText(); if (code.equals(getUseryanzh)) { isYanzheng = true; } else { isYanzheng = false; } if (isYanzheng) { JOptionPane.showMessageDialog(null, "验证成功!", "消息", JOptionPane.PLAIN_MESSAGE); label_6.setText("验证通过"); label_6.setForeground(Color.BLUE); } else { JOptionPane.showMessageDialog(null, "验证失败!", "消息", JOptionPane.WARNING_MESSAGE); label_6.setText("验证失败"); label_6.setForeground(Color.RED); } if (userName.equals(getUserName) && userPwd.equals(getUserPwd)&&(code.equals(getUseryanzh)) ){ isLogin = true; } else { isLogin = false; } if (isLogin) { JOptionPane.showMessageDialog(null, "登录成功!", "消息", JOptionPane.PLAIN_MESSAGE); label_3.setText("已登录"); label_3.setForeground(Color.BLUE); } else { JOptionPane.showMessageDialog(null, "登录失败!", "消息", JOptionPane.WARNING_MESSAGE); label_3.setText("未登录"); label_3.setForeground(Color.RED); } } }); } }