实验指导书2验证码的Java实现课案Word文件下载.docx

上传人:b****2 文档编号:475019 上传时间:2023-04-29 格式:DOCX 页数:16 大小:83.98KB
下载 相关 举报
实验指导书2验证码的Java实现课案Word文件下载.docx_第1页
第1页 / 共16页
实验指导书2验证码的Java实现课案Word文件下载.docx_第2页
第2页 / 共16页
实验指导书2验证码的Java实现课案Word文件下载.docx_第3页
第3页 / 共16页
实验指导书2验证码的Java实现课案Word文件下载.docx_第4页
第4页 / 共16页
实验指导书2验证码的Java实现课案Word文件下载.docx_第5页
第5页 / 共16页
实验指导书2验证码的Java实现课案Word文件下载.docx_第6页
第6页 / 共16页
实验指导书2验证码的Java实现课案Word文件下载.docx_第7页
第7页 / 共16页
实验指导书2验证码的Java实现课案Word文件下载.docx_第8页
第8页 / 共16页
实验指导书2验证码的Java实现课案Word文件下载.docx_第9页
第9页 / 共16页
实验指导书2验证码的Java实现课案Word文件下载.docx_第10页
第10页 / 共16页
实验指导书2验证码的Java实现课案Word文件下载.docx_第11页
第11页 / 共16页
实验指导书2验证码的Java实现课案Word文件下载.docx_第12页
第12页 / 共16页
实验指导书2验证码的Java实现课案Word文件下载.docx_第13页
第13页 / 共16页
实验指导书2验证码的Java实现课案Word文件下载.docx_第14页
第14页 / 共16页
实验指导书2验证码的Java实现课案Word文件下载.docx_第15页
第15页 / 共16页
实验指导书2验证码的Java实现课案Word文件下载.docx_第16页
第16页 / 共16页
亲,该文档总共16页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

实验指导书2验证码的Java实现课案Word文件下载.docx

《实验指导书2验证码的Java实现课案Word文件下载.docx》由会员分享,可在线阅读,更多相关《实验指导书2验证码的Java实现课案Word文件下载.docx(16页珍藏版)》请在冰点文库上搜索。

实验指导书2验证码的Java实现课案Word文件下载.docx

开发工具:

JDK、Eclipse

参考资料:

JDKAPI文档

java.awt包

java.awt.event包

javax.swing包

四、实验内容和结果

4.1实验内容

使用Java语言编程,实现登录时生成验证码的功能,实验代码和截图如下:

/*

*生成验证码的代码

*/

importjava.awt.Color;

importjava.awt.Dimension;

importjava.awt.Font;

importjava.awt.FontMetrics;

importjava.awt.Graphics;

importjava.awt.Graphics2D;

importjava.awt.event.MouseEvent;

importjava.awt.event.MouseListener;

importjava.awt.geom.AffineTransform;

importjava.util.Random;

importjavax.swing.JComponent;

publicclassValidCodeextendsJComponentimplementsMouseListener{

privateStringcode;

privateintwidth,height=40;

privateintcodeLength=4;

privateRandomrandom=newRandom();

privateStringvalid_code="

q0w1e2r3t4y5u6i7o8p9lkjhgfdsazxcvbnm"

;

publicValidCode(){

width=this.codeLength*16+(this.codeLength-1)*10;

setPreferredSize(newDimension(width,height));

setSize(width,height);

this.addMouseListener(this);

setToolTipText("

点击可以更换验证码"

);

}

publicintgetCodeLength(){

returncodeLength;

/*

设置验证码文字的长度

*/

publicvoidsetCodeLength(intcodeLength){

if(codeLength<

4){

this.codeLength=4;

}else{

this.codeLength=codeLength;

publicStringgetCode(){

returncode;

产生随机的颜色

publicColorgetRandColor(intmin,intmax){

if(min>

255)

min=255;

if(max>

max=255;

intred=random.nextInt(max-min)+min;

intgreen=random.nextInt(max-min)+min;

intblue=random.nextInt(max-min)+min;

returnnewColor(red,green,blue);

设置验证码具体的字母是什么

protectedStringgenerateCode(){

intj;

char[]codes=newchar[this.codeLength];

for(inti=0,len=codes.length;

i<

len;

i++){

j=random.nextInt(valid_code.length());

if(valid_code.charAt(j)>

='

0'

&

&

valid_code.charAt(j)<

9'

codes[i]=valid_code.charAt(j);

else

if(random.nextBoolean()){

codes[i]=(char)(valid_code.charAt(j)-32);

codes[i]=valid_code.charAt(j);

this.code=newString(codes);

returnthis.code;

@Override

protectedvoidpaintComponent(Graphicsg){

super.paintComponent(g);

if(this.code==null||this.code.length()!

=this.codeLength){

this.code=generateCode();

super.setSize(width,height);

super.setPreferredSize(newDimension(width,height));

FontmFont=newFont("

Arial"

Font.BOLD|Font.ITALIC,25);

g.setFont(mFont);

//绘制出验证码的背景的矩形轮廓

Graphics2Dg2d=(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);

//绘制出验证码背景的线

inti=0,len=150;

for(;

intx=random.nextInt(width-1);

inty=random.nextInt(height-1);

intx1=random.nextInt(width-10)+10;

inty1=random.nextInt(height-4)+4;

g2d.drawLine(x,y,x1,y1);

//绘制出验证码的具体字母

i=0;

len=this.codeLength;

FontMetricsfm=g2d.getFontMetrics();

intbase=(height-fm.getHeight())/2+fm.getAscent();

for(;

i<

len;

i++){

intb=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);

//下一个验证码

publicvoidnextCode(){

generateCode();

repaint();

publicvoidmouseClicked(MouseEvente){

nextCode();

publicvoidmousePressed(MouseEvente){

publicvoidmouseReleased(MouseEvente){

publicvoidmouseEntered(MouseEvente){

publicvoidmouseExited(MouseEvente){

}

 

程序的截图

*java图形界面

importjava.awt.FlowLayout;

importjava.awt.event.ActionEvent;

importjava.awt.event.ActionListener;

importjava.awt.event.WindowStateListener;

importjavax.swing.Action;

importjavax.swing.JApplet;

importjavax.swing.JButton;

importjavax.swing.JFrame;

importjavax.swing.JLabel;

importjavax.swing.JOptionPane;

importjavax.swing.JPanel;

importjavax.swing.JPasswordField;

importjavax.swing.JTextField;

importjavax.swing.event.DocumentEvent;

importjavax.swing.event.DocumentListener;

importjavax.swing.text.Document;

publicclassWindowLoginextendsJFrameimplementsActionListener,DocumentListener{

JTextFieldtext_userName,

text_code;

JPasswordFieldtext_userPass;

JLabellabel_prompt,

label_title,

label_userName,

label_userPass,

label_code;

JPanelpanel_firt;

JButtonbtn_land;

//btn_register;

ValidCodevcode;

publicWindowLogin(){

}

publicWindowLogin(Stringtitle,intx,inty,intwidth,intheigth){

init();

this.setLayout(null);

this.setBounds(x,y,width,heigth);

this.setTitle(title);

this.setVisible(true);

this.setDefaultCloseOperation(EXIT_ON_CLOSE);

this.getContentPane().setBackground(newColor(190,250,249));

voidinit(){

this.setLocationRelativeTo(null);

label_title=newJLabel("

教务管理系统登录"

label_title.setFont(newFont("

宋体"

0,30));

panel_firt=newJPanel();

panel_firt.add(label_title);

panel_firt.setBounds(100,30,250,45);

panel_firt.setOpaque(false);

this.add(panel_firt);

label_userName=newJLabel("

用户名:

label_userName.setBounds(80,90,60,35);

label_userPass=newJLabel("

密码:

label_userPass.setBounds(80,140,60,35);

label_code=newJLabel("

验证码:

label_code.setBounds(80,190,60,35);

label_prompt=newJLabel("

请输入验证码,再登录"

label_prompt.setBounds(270,230,120,35);

label_prompt.setForeground(Color.red);

text_userName=newJTextField();

text_userName.setBounds(140,90,135,35);

text_userName.setFont(newFont("

Font.BOLD,20));

text_userPass=newJPasswordField();

text_userPass.setBounds(140,140,135,35);

text_code=newJTextField();

text_code.setBounds(140,190,75,35);

text_code.getDocument().addDocumentListener(this);

text_code.setFont(newFont("

btn_land=newJButton("

登录"

btn_land.setBounds(165,230,100,35);

btn_land.setBackground(newColor(224,255,255));

btn_land.setForeground(Color.blue);

btn_land.setFont(newFont("

Dialog"

Font.BOLD,24));

btn_land.addActionListener(this);

btn_land.setEnabled(false);

vcode=newValidCode();

vcode.setBounds(220,185,80,35);

add(label_userName);

add(text_userName);

add(label_userPass);

add(text_userPass);

add(label_code);

add(text_code);

add(label_prompt);

add(vcode);

add(btn_land);

@Override

publicvoidactionPerformed(ActionEventarg0){

if(arg0.getSource()==btn_land){

Stringcode=text_code.getText();

code.trim();

if(code.toLowerCase().equals(vcode.getCode().toLowerCase())){

if(text_userName.getText().equals("

admin"

)){

if(String.valueOf(text_userPass.getPassword()).equals("

123456"

code="

登录成功"

}else{

密码错误"

}

}else{

code="

用户名错误"

}

}else{

code="

验证码错误"

}

vcode.nextCode();

JOptionPane.showMessageDialog(this,code,"

提示"

JOptionPane.WARNING_MESSAGE);

}

publicvoidinsertUpdate(DocumentEvente){

Stringcode=text_code.getText();

code.trim();

if(code.length()==4){

btn_land.setEnabled(true);

label_prompt.setVisible(false);

else{

btn_land.setEnabled(false);

label_prompt.setVisible(true);

//vcode.getCode();

publicvoidremoveUpdate(DocumentEvente){

insertUpdate(e);

publicvoidchangedUpdate(DocumentEvente){

}

4.2实验结果

登录时,系统随机生成验证码,用户输入验证码后系统先判断验证码是否正确,则提示用户验证失败,并重新随机生成一个新的验证码,如果输入的验证码正确,则提示验证成功。

5、实验总结

通过本次实验了解到验证码的作用所在,也简单实现了,登录时系统随机生成验证码,并显示在图片中;

用户输入验证码后系统先判断验证码是否正确,如果不正确,则提示用户验证失败,并重新随机生成一个新的验证码,等待用户再次输入;

如果输入的验证码正确,则提示验证成功。

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 法律文书 > 调解书

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2