Swing-JDialog示例代碼-用戶登錄UI

JDialog是一種對話框組件,它經常與JOptionPane配合使用。JOptionPane提供對話框內部的消息、按鈕等內容,JDialog提供對話框窗體,提供模態/非模態等屬性。JDialog與JFrame在外觀上的區別在於,它沒有最大化/最小化按鈕。以下圖所示:java

JDialog窗體app

JFrame窗體框架

在下面的demo中,演示了JDialog構造登陸窗體,以及從窗體中得到數據的方法。ide

LoginDemoStart.javathis

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.javaspa

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.java3d

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;
    }
}