java语言图形用户登录界面代码
程序员文章站
2024-03-12 14:32:32
本文实例为大家分享了java登录界面的具体实现代码,供大家参考,具体内容如下
1. login.java
package wzb;
import j...
本文实例为大家分享了java登录界面的具体实现代码,供大家参考,具体内容如下
1. login.java
package wzb; import java.awt.color; import java.awt.font; import java.awt.graphics; import java.awt.panel; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.awt.event.keyevent; import java.util.random; import javax.swing.imageicon; import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.joptionpane; import javax.swing.jpasswordfield; import javax.swing.jtextfield; public class login extends jframe implements actionlistener { string username; string password; string captcha; public static string randomcaptcha; public jlabel logolabel, usernamelabel, passwordlabel, captchalabel; public jtextfield usernameinput, captchainput; public jpasswordfield passwordinput; public jbutton login, logout,change; public panel panel; public login() { settitle("µçâ¼½çãæ"); setsize(400, 300); setlocationrelativeto(null); init(); setvisible(true); setdefaultcloseoperation(jframe.exit_on_close); setresizable(false); } public void init() { setlayout(null); // logolabel= new jlabel(); // logolabel.seticon(new imageicon("e:\\eclipse\\student3\\welcome.gif")); logolabel = new jlabel(new imageicon("welcome.gif")); logolabel.setbounds(125, 10, 150, 70); add(logolabel); usernamelabel = new jlabel("ó㻧ãû:"); usernamelabel.setbounds(90, 90, 60, 40); add(usernamelabel); usernameinput = new jtextfield(); usernameinput.setbounds(150, 100, 150, 20); add(usernameinput); passwordlabel = new jlabel("ãü¡¡âë:"); passwordlabel.setbounds(90, 120, 60, 40); add(passwordlabel); passwordinput = new jpasswordfield(); passwordinput.setbounds(150, 130, 150, 20); add(passwordinput); captchalabel = new jlabel("ñéö¤âë:"); captchalabel.setbounds(90, 150, 60, 40); add(captchalabel); captchainput = new jtextfield(); captchainput.setbounds(150, 160, 70, 20); add(captchainput); panel = new paneldemo(); panel.setbounds(220, 160, 80, 20); add(panel); change = new jbutton("»»ò»»»"); change.setbounds(300, 160, 80, 20); change.setcontentareafilled(false); change.setborderpainted(false); add(change); login = new jbutton("µçâ¼£¨l£©", new imageicon("login.gif")); login.setbounds(70, 200, 120, 30); login.setmnemonic(keyevent.vk_l); add(login); logout = new jbutton("íë³ö£¨x£©", new imageicon("exit.gif")); logout.setbounds(210, 200, 120, 30); logout.setmnemonic(keyevent.vk_x); add(logout); usernameinput.addactionlistener(this); passwordinput.addactionlistener(this); captchainput.addactionlistener(this); login.addactionlistener(this); logout.addactionlistener(this); change.addactionlistener(this); } public void actionperformed(actionevent e) { username = usernameinput.gettext(); password = new string(passwordinput.getpassword()); captcha = captchainput.gettext(); if (e.getsource() == change) { panel.repaint(); } if (e.getsource() == login) { if ((username.equals("w")) && (password.equals("w"))) { if (captcha.equals(randomcaptcha)) { joptionpane.showmessagedialog(this, "»¶óµçâ½!"); } else { joptionpane.showmessagedialog(this, "ñéö¤âë´íîó!"); panel.repaint(); } } else { joptionpane.showmessagedialog(this, "ó㻧ãû»òãüâë´íîó!"); } } if (e.getsource() == logout) { joptionpane.showmessagedialog(this, "»¶óïâ´îôùà´£¡"); //system.exit(0); dispose(); } } public static void main(string[] args) { new login(); } } class paneldemo extends panel { public void paint(graphics g) { int width = 80; int height = 20; g.setcolor(color.light_gray); g.fillrect(0, 0, width, height); g.setcolor(color.black); g.drawrect(0, 0, width, height); random rd = new random(); for (int i = 0; i < 100; i++) { int x = rd.nextint(width) - 2; int y = rd.nextint(height) - 2; g.setcolor(color.red); g.drawoval(x, y, 2, 2); } g.setfont(new font("ºúìå", font.bold, 20)); g.setcolor(color.blue); char[] c = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789".tochararray(); stringbuffer sb = new stringbuffer(); for (int i = 0; i < 4; i++) { int index = rd.nextint(c.length); sb.append(c[index] + " "); } g.drawstring(sb.tostring(), 0, 18); string str = sb.tostring().replaceall(" ", ""); login.randomcaptcha = str; } }
2. 捕获.png
以上就是本文的全部内容,希望对大家学习java程序设计有所帮助。