最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java實(shí)現(xiàn)登錄窗口

 更新時(shí)間:2022年04月25日 14:51:51   作者:mlee1018  
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)登錄窗口,含驗(yàn)證碼驗(yàn)證、賬戶注冊(cè)等,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了java實(shí)現(xiàn)登錄窗口的具體代碼,供大家參考,具體內(nèi)容如下

登錄窗口主類

package ccnu.paint;

import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Properties;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import ccnu.util.Answer;
import ccnu.util.Verification;

public class Login extends JFrame
{
? ? private static final long serialVersionUID = 1L;

? ? private Properties pro = new Properties();

? ? private boolean ver_code = false; // 默認(rèn)輸入驗(yàn)證碼錯(cuò)誤

? ? private Answer answer = null;

? ? private JPanel p1 = new JPanel(); // 添加到JPanel中的組件默認(rèn)為流式布局
? ? private JLabel luser = new JLabel("username: ");
? ? private JTextField username = new JTextField(20);

? ? private JPanel p2 = new JPanel();
? ? private JLabel lpwd = new JLabel("password: ");
? ? private JPasswordField pwd = new JPasswordField(20);

? ? private JPanel p4 = new JPanel();
? ? private JLabel lVer = new JLabel("verification: ");
? ? private JTextField ver = new JTextField(10);
? ? private JLabel img = new JLabel();
? ? private JLabel result = new JLabel();

? ? private JPanel p3 = new JPanel();
? ? private JButton ok = new JButton("ok");
? ? private JButton cancel = new JButton("cancel");
? ? private JButton signUp = new JButton("Sign up"); // 用于賬戶注冊(cè)

? ? // 設(shè)置組件的監(jiān)聽
? ? public void initListener()
? ? {
? ? ? ? username.addActionListener(new ActionListener()
? ? ? ? {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void actionPerformed(ActionEvent e)
? ? ? ? ? ? {// JTextField的action是回車鍵
? ? ? ? ? ? ? ? String name = username.getText();
? ? ? ? ? ? ? ? // Login.this.setTitle(name);
? ? ? ? ? ? ? ? // System.out.println(name.hashCode() + "***" +"".hashCode());
? ? ? ? ? ? ? ? if (name.equals(""))
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? JOptionPane.showMessageDialog(Login.this, "Please input a userName!");
? ? ? ? ? ? ? ? } else
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? pwd.grabFocus();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? });

? ? ? ? pwd.addActionListener(new ActionListener(){
? ? ? ? ? ? @Override
? ? ? ? ? ? public void actionPerformed(ActionEvent e)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? String password = new String(pwd.getPassword());
? ? ? ? ? ? ? ? if(password.equalsIgnoreCase(""))
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? JOptionPane.showMessageDialog(Login.this, "please input a password!");
? ? ? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? ? ? ver.grabFocus();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }

? ? ? ? });

? ? ? ? ok.addActionListener(new ActionListener(){

? ? ? ? ? ? @Override
? ? ? ? ? ? public void actionPerformed(ActionEvent e)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? // 重新加載最新的賬戶文件
? ? ? ? ? ? ? ? try
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? pro.load(new FileInputStream(new File("src/res/accouts.properties")));
? ? ? ? ? ? ? ? } catch (IOException e1)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? e1.printStackTrace();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? check();
? ? ? ? ? ? }

? ? ? ? });

? ? ? ? // 判斷驗(yàn)證碼是否正確
? ? ? ? ver.addActionListener(new ActionListener(){
? ? ? ? ? ? @Override
? ? ? ? ? ? public void actionPerformed(ActionEvent e)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? String verCode = ver.getText();
? ? ? ? ? ? ? ? if(verCode.equals(""))
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? JOptionPane.showMessageDialog(Login.this, "Please input a verification!");
? ? ? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? ? ? if(verCode.equals(answer.getResult()))
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? result.setIcon(new ImageIcon(Login.this.getClass().getResource("/res/right.jpg"))); // 顯示提示的圖片信息(如√圖片)
? ? ? ? ? ? ? ? ? ? ? ? ver_code = true;
? ? ? ? ? ? ? ? ? ? ? ? // 檢查之前,重新加載最新的賬戶文件
? ? ? ? ? ? ? ? ? ? ? ? try
? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? pro.load(new FileInputStream(new File("src/res/accouts.properties"))); // 將賬戶文件加載進(jìn)來
? ? ? ? ? ? ? ? ? ? ? ? } catch (IOException e1)
? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? e1.printStackTrace();
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? check();
? ? ? ? ? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? ? ? ? ? result.setIcon(new ImageIcon(Login.this.getClass().getResource("/res/error.jpg"))); // 顯示提示的圖片信息(如×圖片)?
? ? ? ? ? ? ? ? ? ? ? ? ver_code = false;
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? });

? ? ? ? // 點(diǎn)擊圖片會(huì)更改驗(yàn)證碼
? ? ? ? img.addMouseListener(new MouseAdapter(){

? ? ? ? ? ? @Override
? ? ? ? ? ? public void mouseClicked(MouseEvent e)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? answer = Verification.verification();
? ? ? ? ? ? ? ? img.setIcon(new ImageIcon(answer.getBufferedImage())); // 設(shè)置驗(yàn)證碼圖案
? ? ? ? ? ? }
? ? ? ? });

? ? ? ? cancel.addActionListener(new ActionListener()
? ? ? ? {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void actionPerformed(ActionEvent e)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? int option = JOptionPane.showConfirmDialog(Login.this, "Are you sure to exit?");
// ? ? ? ? ? ? ?System.out.println("option = " + option);
? ? ? ? ? ? ? ? if (option == 0)
? ? ? ? ? ? ? ? {// Yes
? ? ? ? ? ? ? ? ? ? Login.this.dispose();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? });

? ? ? ? signUp.addActionListener(new ActionListener(){

? ? ? ? ? ? @Override
? ? ? ? ? ? public void actionPerformed(ActionEvent e)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? new SignUp();
? ? ? ? ? ? }
? ? ? ? });
? ? }

? ? // 初始化登錄窗口及其組件的設(shè)置監(jiān)聽
? ? public Login()
? ? {
? ? ? ? super("Login");

? ? ? ? // 加載賬戶文件
? ? ? ? try
? ? ? ? {
? ? ? ? ? ? pro.load(new FileInputStream(new File("src/res/accouts.properties"))); // 從指定位置將賬戶文件加載進(jìn)來
? ? ? ? } catch (IOException e)
? ? ? ? {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }

? ? ? ? initListener();
? ? ? ? answer = Verification.verification(); // 生成驗(yàn)證碼
? ? ? ? img.setIcon(new ImageIcon(answer.getBufferedImage())); // 設(shè)置初始驗(yàn)證碼

? ? ? ? this.setLocation(new Point(200, 200));
? ? ? ? this.setSize(500, 300);
? ? ? ? this.setLayout(new GridLayout(4, 1, 0, 20)); // 垂直間隙為20px
? ? ? ? p1.add(luser);
? ? ? ? p1.add(username);
? ? ? ? p2.add(lpwd);
? ? ? ? p2.add(pwd);
? ? ? ? p4.add(this.lVer);
? ? ? ? p4.add(this.ver);
? ? ? ? p4.add(this.img);
? ? ? ? result.setForeground(Color.red);
? ? ? ? result.setFont(new Font("楷體", Font.BOLD, 20));
? ? ? ? p4.add(result);
? ? ? ? p3.add(ok);
? ? ? ? p3.add(cancel);
? ? ? ? p3.add(signUp);
? ? ? ? this.add(p1);
? ? ? ? this.add(p2);
? ? ? ? ? ? this.add(p4);
? ? ? ? this.add(p3);

// ? ? ?this.setBackground(Color.blue); // JFrame的上層還有一個(gè)ContentPane
? ? ? ? this.getContentPane().setBackground(Color.gray);
? ? ? ? this.setResizable(false);
? ? ? ? this.setVisible(true);
? ? ? ? this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 等價(jià)于Frame中的windowClosing事件
? ? }

? ? // 檢查用戶名或密碼
? ? public void check()
? ? {

? ? ? ? String verCode = ver.getText();
? ? ? ? if(verCode.equals(""))
? ? ? ? {
? ? ? ? ? ? JOptionPane.showMessageDialog(Login.this, "Please input a verification!");
? ? ? ? ? ? return;
? ? ? ? }else{
? ? ? ? ? ? if(verCode.equals(answer.getResult()))
? ? ? ? ? ? {
? ? ? ? ? ? ? ? result.setIcon(new ImageIcon(Login.this.getClass().getResource("/res/right.jpg")));
? ? ? ? ? ? ? ? ver_code = true;
? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? result.setIcon(new ImageIcon(Login.this.getClass().getResource("/res/error.jpg")));
? ? ? ? ? ? ? ? ver_code = false;
? ? ? ? ? ? }
? ? ? ? }

? ? ? ? if(ver_code == false)
? ? ? ? {
? ? ? ? ? ? JOptionPane.showMessageDialog(this, "verification is error!");
? ? ? ? ? ? return;
? ? ? ? }
? ? ? ? String name = username.getText();
? ? ? ? String password = new String(pwd.getPassword()); // return char[]
// ? ? ?if (name.equalsIgnoreCase("admin") && password.equals("123456"))
? ? ? ? if (isPass(name, password))
? ? ? ? {
// ? ? ? ? ?new PaintApp(name);
? ? ? ? ? ? JOptionPane.showMessageDialog(this, "-^_^- ? OK..."); // 此處可以加上其他的登陸成功后進(jìn)一步處理的窗口
? ? ? ? ? ? this.dispose();
? ? ? ? } else
? ? ? ? {
? ? ? ? ? ? JOptionPane.showMessageDialog(this, "userName or password is incorrect!");
? ? ? ? ? ? username.setText("");
? ? ? ? ? ? pwd.setText("");
? ? ? ? ? ? ver.setText("");
? ? ? ? ? ? answer = Verification.verification();
? ? ? ? ? ? img.setIcon(new ImageIcon(answer.getBufferedImage()));
? ? ? ? ? ? result.setIcon(null);
? ? ? ? }
? ? }

? ? // 驗(yàn)證用戶輸入的賬戶名和密碼是否正確(通過與加載進(jìn)來的賬戶 pro 比對(duì))
? ? public boolean isPass(String name, String password)
? ? {
? ? ? ? Enumeration en = pro.propertyNames();
? ? ? ? while(en.hasMoreElements())
? ? ? ? {
? ? ? ? ? ? String curName = (String)en.nextElement();
// ? ? ? ? ?System.out.println(curName + "---" + pro.getProperty(curName));
? ? ? ? ? ? if(curName.equalsIgnoreCase(name))
? ? ? ? ? ? {
? ? ? ? ? ? ? ? if(password.equalsIgnoreCase(pro.getProperty(curName)))
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? return true;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? return false;
? ? }

? ? public static void main(String[] args)
? ? {
? ? ? ? new Login();
? ? }
}

賬戶注冊(cè)類

package ccnu.paint;

import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Properties;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class SignUp extends JFrame
{
? ? private static final long serialVersionUID = 3054293481122038909L;

? ? private Properties pro = new Properties(); // 最好時(shí)靜態(tài)的,因?yàn)橘~戶是共享的

? ? private JPanel panel = new JPanel();
? ? private JLabel label = new JLabel("username: ");
? ? private JTextField field = new JTextField(15);

? ? private JPanel panel2 = new JPanel();
? ? private JLabel label2 = new JLabel("password: ");
? ? private JPasswordField field2 = new JPasswordField(15);

? ? private JPanel panel3 = new JPanel();
? ? private JLabel label3 = new JLabel("confirmation: ");
? ? private JPasswordField field3 = new JPasswordField(15);

? ? private JPanel panel4 = new JPanel();
? ? private JButton button = new JButton("OK");
? ? private JButton button2 = new JButton("Cancel");

? ? public void initListener()
? ? {
? ? ? ? field.addActionListener(new ActionListener()
? ? ? ? {

? ? ? ? ? ? @Override
? ? ? ? ? ? public void actionPerformed(ActionEvent e)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? field2.grabFocus();
? ? ? ? ? ? }

? ? ? ? });

? ? ? ? field2.addActionListener(new ActionListener()
? ? ? ? {

? ? ? ? ? ? @Override
? ? ? ? ? ? public void actionPerformed(ActionEvent e)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? field3.grabFocus();
? ? ? ? ? ? }
? ? ? ? });
? ? ? ? field3.addActionListener(new ActionListener()
? ? ? ? {

? ? ? ? ? ? @Override
? ? ? ? ? ? public void actionPerformed(ActionEvent e)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? ok_actionPerformed(e);
? ? ? ? ? ? }

? ? ? ? });

? ? ? ? // OK
? ? ? ? button.addActionListener(new ActionListener()
? ? ? ? {

? ? ? ? ? ? @Override
? ? ? ? ? ? public void actionPerformed(ActionEvent e)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? ok_actionPerformed(e);
? ? ? ? ? ? }
? ? ? ? });

? ? ? ? // Cancel
? ? ? ? button2.addActionListener(new ActionListener()
? ? ? ? {

? ? ? ? ? ? @Override
? ? ? ? ? ? public void actionPerformed(ActionEvent e)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? cancel_actionPerformed(e);
? ? ? ? ? ? }
? ? ? ? });
? ? }

? ? public void ok_actionPerformed(ActionEvent e)
? ? {
? ? ? ? String userName = field.getText();
? ? ? ? String password = new String(field2.getPassword());
? ? ? ? String password2 = new String(field3.getPassword());
? ? ? ? if (userName.equals(""))
? ? ? ? {
? ? ? ? ? ? JOptionPane.showMessageDialog(SignUp.this, "username cannot be empty!");
? ? ? ? } else
? ? ? ? {
? ? ? ? ? ? if (password.equalsIgnoreCase(""))
? ? ? ? ? ? {
? ? ? ? ? ? ? ? JOptionPane.showMessageDialog(SignUp.this, "password cannot be empty!");
? ? ? ? ? ? } else
? ? ? ? ? ? {
? ? ? ? ? ? ? ? if (password2.equalsIgnoreCase(password))
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? if (isExist(userName))
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? JOptionPane.showMessageDialog(SignUp.this, "username has been existed!");
? ? ? ? ? ? ? ? ? ? ? ? field.setText("");
? ? ? ? ? ? ? ? ? ? ? ? field2.setText("");
? ? ? ? ? ? ? ? ? ? ? ? field3.setText("");
? ? ? ? ? ? ? ? ? ? } else
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? pro.setProperty(userName, password);
? ? ? ? ? ? ? ? ? ? ? ? JOptionPane.showMessageDialog(SignUp.this, "SignUp success!");
? ? ? ? ? ? ? ? ? ? ? ? writeToPro(userName, password); // 將其寫入到賬戶文件中
? ? ? ? ? ? ? ? ? ? ? ? SignUp.this.dispose();
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? } else
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? JOptionPane.showMessageDialog(SignUp.this, "password is not consistent!");
? ? ? ? ? ? ? ? ? ? field2.setText("");
? ? ? ? ? ? ? ? ? ? field3.setText("");
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? ? }

? ? public void cancel_actionPerformed(ActionEvent e)
? ? {
? ? ? ? System.exit(0);
? ? }

? ? public SignUp()
? ? {
? ? ? ? super("Sign up");

? ? ? ? // 加載賬戶文件
? ? ? ? try
? ? ? ? {
? ? ? ? ? ? pro.load(new FileInputStream(new File("src/res/accouts.properties")));
? ? ? ? } catch (IOException e)
? ? ? ? {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }

? ? ? ? // 初始化窗口組件的監(jiān)聽
? ? ? ? initListener();

? ? ? ? this.setLocation(new Point(300, 230));
? ? ? ? this.setSize(280, 210);

? ? ? ? this.setLayout(new GridLayout(4, 1, 0, 20)); // 垂直間隙為20px
? ? ? ? panel.add(label);
? ? ? ? panel.add(field);
? ? ? ? panel2.add(label2);
? ? ? ? panel2.add(field2);
? ? ? ? panel3.add(label3);
? ? ? ? panel3.add(field3);
? ? ? ? panel4.add(button);
? ? ? ? panel4.add(button2);
? ? ? ? this.add(panel);
? ? ? ? this.add(panel2);
? ? ? ? this.add(panel3);
? ? ? ? this.add(panel4);

? ? ? ? this.setAlwaysOnTop(true);
? ? ? ? this.setResizable(false);
? ? ? ? this.setVisible(true);
? ? }

? ? // 如果注冊(cè)始終可用,就要保存起來,否則不需要寫入文件中,注冊(cè)賬戶本次使用
? ? // 將賬戶名與其對(duì)應(yīng)密碼保存到指定的賬戶文件中
? ? public void writeToPro(String userName, String password)
? ? {
? ? ? ? pro.setProperty(userName, password);
? ? ? ? try
? ? ? ? {
? ? ? ? ? ? pro.store(new FileOutputStream(new File("src/res/accouts.properties")), "allAccouts");
? ? ? ? } catch (IOException e)
? ? ? ? {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? }


? ? // 判斷此用戶名是否已經(jīng)存在
? ? public boolean isExist(String userName)
? ? {
? ? ? ? Enumeration enumer = pro.propertyNames();
? ? ? ? while (enumer.hasMoreElements())
? ? ? ? {
? ? ? ? ? ? String temp = (String) enumer.nextElement();
? ? ? ? ? ? if (temp.equals(userName))
? ? ? ? ? ? {
? ? ? ? ? ? ? ? return true;
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? return false;
? ? }
}

生成驗(yàn)證碼類

package ccnu.util;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.image.BufferedImage;
import java.util.Random;

// 用于生成驗(yàn)證碼
public class Verification
{
? ? private static Answer answer = new Answer();
? ? private static BufferedImage bufferedImage = null;
? ? private static String result = null;
? ? private static String words = null;
? ? private static String words2 = null;

? ? // 生成驗(yàn)證碼
? ? public static Answer verification()
? ? {

? ? ? ? bufferedImage = new BufferedImage(200, 35, BufferedImage.TYPE_INT_RGB);
? ? ? ? Graphics g = bufferedImage.getGraphics();
? ? ? ? Random rand = new Random();
? ? ? ? for (int i = 0; i < 20; i++)
? ? ? ? {
? ? ? ? ? ? Point p1 = new Point(rand.nextInt(200), rand.nextInt(30));
? ? ? ? ? ? Point p2 = new Point(rand.nextInt(200), rand.nextInt(30));
? ? ? ? ? ? g.drawLine(p1.x, p1.y, p2.x, p2.y);
? ? ? ? }
? ? ? ? g.setColor(Color.RED);
? ? ? ? g.setFont(new Font("楷體", Font.BOLD, 22));
? ? ? ? int plan = 2;
? ? ? ? switch (rand.nextInt(plan))
? ? ? ? {
? ? ? ? case 0:
? ? ? ? ? ? plan(g);
? ? ? ? ? ? break;
? ? ? ? case 1:
? ? ? ? ? ? plan1(g);
? ? ? ? ? ? break;
? ? ? ? default:
? ? ? ? ? ? break;
? ? ? ? }
? ? ? ? answer.setBufferedImage(bufferedImage);
? ? ? ? answer.setResult(result);
? ? ? ? g.dispose();
? ? ? ? return answer;
? ? }

? ? // 方案一
? ? private static void plan(Graphics g)
? ? {
? ? ? ? words = ReadTxt.read("/res/words.txt"); // 指定生成驗(yàn)證碼問題的資源文件的路徑
? ? ? ? Random rand = new Random();
? ? ? ? String first = String.valueOf(words.charAt(rand.nextInt(words.length())));
? ? ? ? String second = String.valueOf(words.charAt(rand.nextInt(words.length())));
? ? ? ? String third = String.valueOf(words.charAt(rand.nextInt(words.length())));
? ? ? ? g.drawString(first, rand.nextInt(40) + 20, rand.nextInt(12) + 15);
? ? ? ? g.drawString(second, rand.nextInt(40) + 80, rand.nextInt(12) + 15);
? ? ? ? g.drawString(third, rand.nextInt(40) + 140, rand.nextInt(12) + 15);
? ? ? ? result = first + second + third;
? ? }

? ? // 方案二
? ? private static void plan1(Graphics g)
? ? {
? ? ? ? words2 = ReadTxt.read("/res/words2.txt"); // 指定生成驗(yàn)證碼問題的資源文件的路徑
? ? ? ? Random rand = new Random();
? ? ? ? String first = String.valueOf(words2.charAt(rand.nextInt(words2.length() - 2)));
? ? ? ? String second = String.valueOf(words2.charAt(rand.nextInt(2) + 9));
? ? ? ? String third = String.valueOf(words2.charAt(rand.nextInt(words2.length() - 2)));
? ? ? ? g.drawString(first, rand.nextInt(30) + 20, rand.nextInt(12) + 15);
? ? ? ? g.drawString(second, rand.nextInt(40) + 60, rand.nextInt(12) + 15);
? ? ? ? g.drawString(third, rand.nextInt(30) + 110, rand.nextInt(12) + 15);
? ? ? ? g.drawString("=", rand.nextInt(40) + 150, rand.nextInt(12) + 15);
? ? ? ? if(second.equals("+"))
? ? ? ? {
? ? ? ? ? ? result = String.valueOf(Integer.valueOf(first) + Integer.valueOf(third));?
? ? ? ? }else{
? ? ? ? ? ? result = String.valueOf(Integer.valueOf(first) - Integer.valueOf(third));?
? ? ? ? }
? ? }

}

讀取生成驗(yàn)證碼所需文件類

package ccnu.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

// 專門用于讀取文件
public class ReadTxt
{
? ? public static String read(String path) // 根據(jù)指定路徑path來讀取它,并返回它所包含的內(nèi)容
? ? {
? ? ? ? StringBuffer sb = new StringBuffer();
? ? ? ? try
? ? ? ? {
? ? ? ? ? ? BufferedReader br = new BufferedReader(new InputStreamReader(Verification.class.getResourceAsStream(path)));
? ? ? ? ? ? String temp = null;
? ? ? ? ? ? while(null != (temp = br.readLine()))
? ? ? ? ? ? {
? ? ? ? ? ? ? ? sb.append(temp);
? ? ? ? ? ? }
? ? ? ? ? ? br.close();
? ? ? ? } catch (IOException e)
? ? ? ? {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? ? ? return sb.toString();
? ? }

}

得到生成的驗(yàn)證碼所包含的信息類(圖案、問題)

package ccnu.util;

import java.awt.image.BufferedImage;

// 用于將生成的驗(yàn)證碼的圖案信息以及問題結(jié)果封裝
public class Answer
{
? ? private BufferedImage bufferedImage = null; // 驗(yàn)證碼圖像
? ? private String result = null; // 驗(yàn)證碼圖像問題的答案

? ? public BufferedImage getBufferedImage()
? ? {
? ? ? ? return bufferedImage;
? ? }
? ? public void setBufferedImage(BufferedImage bufferedImage)
? ? {
? ? ? ? this.bufferedImage = bufferedImage;
? ? }
? ? public String getResult()
? ? {
? ? ? ? return result;
? ? }
? ? public void setResult(String result)
? ? {
? ? ? ? this.result = result;
? ? }

}

驗(yàn)證碼生成漢字識(shí)別的問題的文件words.txt
如: 中國湖北省武漢市漢東大學(xué)政法學(xué)院

驗(yàn)證碼生成算術(shù)運(yùn)算的問題的文件words2.txt
123456789+-

提示圖片

登錄效果

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Jmeter內(nèi)置變量vars和props的使用詳解

    Jmeter內(nèi)置變量vars和props的使用詳解

    JMeter是一個(gè)功能強(qiáng)大的負(fù)載測試工具,它提供了許多有用的內(nèi)置變量來支持測試過程,其中最常用的變量是 vars 和 props,本文通過代碼示例詳細(xì)給大家介紹了Jmeter內(nèi)置變量vars和props的使用,需要的朋友可以參考下
    2024-08-08
  • 解決引用jip-common jar包,報(bào)401 Unauthorized錯(cuò)誤問題

    解決引用jip-common jar包,報(bào)401 Unauthorized錯(cuò)誤問題

    這篇文章主要介紹了解決引用jip-common jar包,報(bào)401 Unauthorized錯(cuò)誤問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-03-03
  • java 中HashMap、HashSet、TreeMap、TreeSet判斷元素相同的幾種方法比較

    java 中HashMap、HashSet、TreeMap、TreeSet判斷元素相同的幾種方法比較

    這篇文章主要介紹了從源碼的角度淺析HashMap、TreeMap元素的存儲(chǔ)和獲取元素的邏輯;從Map與Set之間的關(guān)系淺析常用的Set中元素的存儲(chǔ)和判斷是否重復(fù)的邏輯,需要的朋友可以參考下
    2017-01-01
  • Java8中常見函數(shù)式接口的使用示例詳解

    Java8中常見函數(shù)式接口的使用示例詳解

    在 Java 8 中,函數(shù)式接口是一個(gè)關(guān)鍵的特性,它們?cè)试S將方法作為參數(shù)傳遞或返回類型,本文為大家整理了一些常見的函數(shù)式接口的使用,希望對(duì)大家有所幫助
    2023-12-12
  • java正則表達(dá)式簡單應(yīng)用

    java正則表達(dá)式簡單應(yīng)用

    這篇文章主要介紹了java正則表達(dá)式簡單應(yīng)用,在之前幾篇文章中已經(jīng)深入學(xué)習(xí)了java正則表達(dá)式基礎(chǔ)知識(shí),本文對(duì)java正則表達(dá)式應(yīng)用進(jìn)行研究,感興趣的小伙伴們可以參考一下
    2015-12-12
  • Mybatis-plus如何在xml中傳入自定義的SQL語句

    Mybatis-plus如何在xml中傳入自定義的SQL語句

    這篇文章主要介紹了Mybatis-plus如何在xml中傳入自定義的SQL語句問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-05-05
  • Java通過jersey實(shí)現(xiàn)客戶端圖片上傳示例

    Java通過jersey實(shí)現(xiàn)客戶端圖片上傳示例

    本篇文章主要介紹了Java通過jersey實(shí)現(xiàn)客戶端圖片上傳示例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。
    2017-03-03
  • mybatis的ParamNameResolver參數(shù)名稱解析

    mybatis的ParamNameResolver參數(shù)名稱解析

    這篇文章主要為大家介紹了mybatis的ParamNameResolver參數(shù)名稱解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-08-08
  • Java編程中的條件判斷之if語句的用法詳解

    Java編程中的條件判斷之if語句的用法詳解

    這篇文章主要介紹了Java編程中的條件判斷之if語句的用法詳解,是Java入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下
    2015-11-11
  • MyBatis-Plus枚舉和自定義主鍵ID的實(shí)現(xiàn)步驟

    MyBatis-Plus枚舉和自定義主鍵ID的實(shí)現(xiàn)步驟

    這篇文章主要給大家介紹了關(guān)于MyBatis-Plus枚舉和自定義主鍵ID的相關(guān)資料,文中通過實(shí)例代碼以及圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2022-02-02

最新評(píng)論

石景山区| 娱乐| 阿拉善右旗| 利川市| 翼城县| 达拉特旗| 苍梧县| 东乌| 那曲县| 萨嘎县| 筠连县| 兰考县| 繁昌县| 张家港市| 渑池县| 偃师市| 平邑县| 武邑县| 从江县| 晋江市| 江孜县| 芜湖县| 柯坪县| 高邮市| 当雄县| 阳城县| 石台县| 兴安县| 黔南| 慈溪市| 荣成市| 南华县| 芦溪县| 元谋县| 广西| 香河县| 汕头市| 手游| 涞水县| 涡阳县| 社旗县|