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;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Jun-H

你的鼓励是我创作的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值