Java Swing中JDialog实现用户登陆UI示例

时间:2022-09-18 11:09:02

本文实例讲述了java swing中jdialog实现用户登陆ui。分享给大家供大家参考,具体如下:

jdialog是一种对话框组件,它常常与joptionpane配合使用。joptionpane提供对话框内部的消息、按钮等内容,jdialog提供对话框窗体,提供模态/非模态等属性。jdialog与jframe在外观上的区别在于,它没有最大化/最小化按钮。如下图所示:

Java Swing中JDialog实现用户登陆UI示例

jdialog窗体

 点击file-》connect之后弹出登陆窗口:

Java Swing中JDialog实现用户登陆UI示例

jframe窗体

在下面的demo中,演示了jdialog构造登录窗体,以及从窗体中获得数据的方法。

logindemostart.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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.zzvips.com");
    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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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.zzvips.com");
    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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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程序设计有所帮助。

原文链接:http://www.cnblogs.com/pzy4447/p/4701753.html