Swing-JDialog示例代码-用户登陆UI

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

JDialog窗体

JFrame窗体

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

LoginDemoStart.java

package LoginDemo;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

/*
 * @功能:使用JDialog构造登陆对话框,并获取登陆用户信息
 * @版本:20150804
 * @结构:LoginDemoStart[主类,提供初始UI],PasswordChooser[提供登陆UI并获取用户信息],User[保存用户信息]
 */
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");
        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 LoginDemo;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/*
 * @功能:提供登陆UI并获取用户信息
 * @版本:20150804
 */
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");
        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 LoginDemo;

/*
 * @功能:保存用户信息
 * @版本: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;
    }
}

 

posted on 2015-08-04 13:29  pzy4447  阅读(1300)  评论(0编辑  收藏  举报