Java Dialog DataExchange

package com.njust.JunH.JavaStart;


import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.*;


import javax.swing.*;




public class DataExchangeTest {


/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
EventQueue.invokeLater(new Runnable() {
public void run() {
DataExchangeFrame frame = new DataExchangeFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true); 
}
});
}


}


class DataExchangeFrame extends JFrame {
private static final int DEFAULT_WIDTH = 300;
    private static final int DEFAULT_HEIGHT = 200;
private PasswordChooser dialog = null;
private JTextArea txtArea;

public DataExchangeFrame() {
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
        setTitle("DataExchange");
        
        JMenuBar menuBar = new JMenuBar();
        setJMenuBar(menuBar);
        JMenu fileMenu = new JMenu("File");
        menuBar.add(fileMenu);
        
        JMenuItem connectItem = new JMenuItem("Connect");
        connectItem.addActionListener(new ConnectAction());
        
        JMenuItem exitItem = new JMenuItem("Exit");
        exitItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
        System.exit(0);
        }
        });
        fileMenu.add(connectItem);
        fileMenu.add(exitItem);
        
        txtArea = new JTextArea();
        add(new JScrollPane(txtArea), BorderLayout.CENTER);
        
}

/**
* the Connect action pops up the password Dialog
* */
private class ConnectAction implements ActionListener {
public void actionPerformed(ActionEvent event) {
//if first time, construct dialog
if (null == dialog) dialog = new PasswordChooser();

//set default values
dialog.setUser(new User("yourname", null));

//pop up dialog
if (dialog.showDialog(DataExchangeFrame.this, "Connect")) {
//if accepted,  retieve user input
User user = dialog.getUser();
txtArea.append("User Name = " + user.getName() + ", Password = " + new String(user.getPassword()) + "\n");
}

}
}


class PasswordChooser extends JPanel {
private JTextField username;
private JPasswordField password;
private JButton okBtn;
private JButton cancelBtn;
private boolean ok;
private JDialog dialog;

public PasswordChooser() {
setLayout(new BorderLayout());

//construct a panel with user name and password fields
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(2, 2));
panel.add(new JLabel("user name: "));
username = new JTextField("");
panel.add(username);

panel.add(new JLabel("Password"));
password = new JPasswordField("");
panel.add(password);
add(panel, BorderLayout.CENTER);

JPanel btnPanel = new JPanel();
okBtn = new JButton("OK");
okBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
ok = true;
dialog.setVisible(false);

});
btnPanel.add(okBtn);

cancelBtn = new JButton("Cancel");
cancelBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
dialog.setVisible(false);

});
btnPanel.add(cancelBtn);

add(btnPanel, BorderLayout.SOUTH);
}

public void setUser(User user) {
username.setText(user.getName());
}

public User getUser() {
return new User(username.getText(), password.getPassword());
}

/**
* Show the chooser panel in a dialog
* @param parent a component in the owner frame or null
* @param title the window title
* */
public boolean showDialog(Component parent, String title) {
ok = false;

//locate the owner frame

Frame owner = null;
//测试parent对象是不是Frame的实例
if (parent instanceof Frame) owner = (Frame)parent;
else owner = (Frame)SwingUtilities.getAncestorOfClass(Frame.class, parent);

//if first time, or if owner has changed, make new dialog
if (null == dialog || dialog.getOwner() != owner) {
dialog = new JDialog(owner, true);
dialog.add(this);
dialog.getRootPane().setDefaultButton(okBtn); //设置默认Btn
dialog.pack();
}

//set title and show dialog
dialog.setTitle(title);
dialog.setVisible(true);

return ok;
}
}


class User {
    private String name = "";
    private char[] password;
    
    public User(String name, char[] password) {
        this.name = name;
        this.password = password;
    }
    
    public String getName() {
        return name;
    }
    
    public char[] getPassword() {
        return password;
    }
    
    public void setName(String name) {
        this.name = name;
    }
    
    public void setPassword(char[] password) {
        this.password = password;
    }
}

你可能感兴趣的:(Java Dialog DataExchange)