Java Swing中JDialog实现用户登陆UI示例
程序员文章站
2023-12-19 16:50:58
本文实例讲述了java swing中jdialog实现用户登陆ui。分享给大家供大家参考,具体如下:
jdialog是一种对话框组件,它常常与joptionpane配合使...
本文实例讲述了java swing中jdialog实现用户登陆ui。分享给大家供大家参考,具体如下:
jdialog是一种对话框组件,它常常与joptionpane配合使用。joptionpane提供对话框内部的消息、按钮等内容,jdialog提供对话框窗体,提供模态/非模态等属性。jdialog与jframe在外观上的区别在于,它没有最大化/最小化按钮。如下图所示:
jdialog窗体
点击file-》connect之后弹出登陆窗口:
jframe窗体
在下面的demo中,演示了jdialog构造登录窗体,以及从窗体中获得数据的方法。
logindemostart.java
package awtdemo; import java.awt.borderlayout; import java.awt.event.actionevent; import java.awt.event.actionlistener; import javax.swing.*; /* * @功能:使用jdialog构造登陆对话框,并获取登陆用户信息 * @版本:20150804 * @结构:logindemostart[主类,提供初始ui],passwordchooser[提供登陆ui并获取用户信息],user[保存用户信息] */ @suppresswarnings("serial") class logindemostart extends jframe{ /* * @功能:提供初始ui,并调用passwordchooser登陆界面 * @版本:20150804 */ private jtextarea textarea; private passwordchooser passwordchooser ; public logindemostart(){ //构造菜单栏 jmenubar mbar = new jmenubar(); setjmenubar(mbar); jmenu filemenu = new jmenu("file"); jmenuitem connectitem = new jmenuitem("connect"); connectitem.addactionlistener(new actionlistener(){ public void actionperformed(actionevent e) { // todo auto-generated method stub //if 1st time,construct a dialog passwordchooser = new passwordchooser(logindemostart.this); passwordchooser.setvisible(true); if(passwordchooser.islogedin()) { user u = passwordchooser.getuser(); textarea.append("username = " + u.getname() + ", password = " + new string(u.getpassword()) + "\n"); } } }); jmenuitem exititem = new jmenuitem("exit"); exititem.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent e) { // todo auto-generated method stub system.exit(0); } }); mbar.add(filemenu); filemenu.add(connectitem); filemenu.add(exititem); //添加文本区域 textarea = new jtextarea(); add(new jscrollpane(textarea), borderlayout.center); //设置窗体属性 settitle("logindemostart - www.jb51.net"); setsize(300,200); setlocationrelativeto(null); } public static void main(string[] args) { // todo auto-generated method stub logindemostart logindemostart = new logindemostart(); logindemostart.setvisible(true); } }
passwordchooser.java
package awtdemo; import java.awt.*; import java.awt.event.*; import javax.swing.*; /* * @功能:提供登陆ui并获取用户信息 * @版本:20150804 */ @suppresswarnings("serial") class passwordchooser extends jdialog{ private jtextfield username ; private jpasswordfield password ; private jbutton okbutton ; private jbutton cancelbutton ; private boolean islogedin = false; public passwordchooser(frame parent){ super(parent, true); //本ui包含2个panel jpanel inputpanel = new jpanel(); jpanel buttonpanel = new jpanel(); //构造inputpanel inputpanel.setlayout(new gridlayout(2,2)); inputpanel.add(new jlabel("useername:")); username = new jtextfield(); username.setcolumns(10); password = new jpasswordfield(); password.setcolumns(10); inputpanel.add(username); inputpanel.add(new jlabel("password:")); inputpanel.add(password); inputpanel.setborder(borderfactory.createemptyborder(10, 5, 10, 5)); //构造buttonpanel okbutton = new jbutton("ok"); okbutton.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent e) { // todo auto-generated method stub islogedin = true; setvisible(false); } }); cancelbutton = new jbutton("cancel"); cancelbutton.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent e) { // todo auto-generated method stub setvisible(false); } }); buttonpanel.add(okbutton); buttonpanel.add(cancelbutton); buttonpanel.setborder(borderfactory.createemptyborder(10, 5, 10, 5)); //构造主框架 setlayout(new borderlayout()); getcontentpane().add(inputpanel, borderlayout.north); getcontentpane().add(buttonpanel, borderlayout.south); //设置窗体属性 settitle("passwordchooser - www.jb51.net"); setlocationrelativeto(inputpanel); //setpreferredsize(new dimension(300, 200)); pack(); //system.out.println(getpreferredsize()); } public void setuser(user u){ username.settext(u.getname()); } public user getuser(){ return new user(username.gettext(), password.getpassword()); } public boolean islogedin(){ return islogedin; } public static void main(string[] args) { // todo auto-generated method stub } }
user.java
package awtdemo; /* * @功能:保存用户信息 * @版本:20150804 */ class user{ private string name; private char[] password; public user(string aname, char[] apassword){ name = aname; password = apassword; } public string getname(){ return name; } public char[] getpassword(){ return password; } public void setname(string aname){ name = aname; } public void setpassword(char[] apassword){ password = apassword; } }
更多关于java相关内容感兴趣的读者可查看本站专题:《java数据结构与算法教程》、《java字符与字符串操作技巧总结》、《java操作dom节点技巧总结》、《java文件与目录操作技巧汇总》和《java缓存操作技巧汇总》
希望本文所述对大家java程序设计有所帮助。