Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement login window with java

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

< 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"); // 指定生成验证码问题的资源文件的路径 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"); // 指定生成验证码问题的资源文件的路径 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)); } }} 读取生成验证码所需文件类 package ccnu.util;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;// 专门用于读取文件public class ReadTxt{ public static String read(String path) // 根据指定路径path来读取它,并返回它所包含的内容 { 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(); }} 得到生成的验证码所包含的信息类(图案、问题) package ccnu.util;import java.awt.image.BufferedImage;// 用于将生成的验证码的图案信息以及问题结果封装public class Answer{ private BufferedImage bufferedImage = null; // 验证码图像 private String result = null; // 验证码图像问题的答案 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; }} 验证码生成汉字识别的问题的文件words.txt 如: 中国湖北省武汉市汉东大学政法学院 验证码生成算术运算的问题的文件words2.txt 123456789+- 提示图片

Login effect

This is the end of the content of "how to implement the login window for java". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report