import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class LoginDialog extends JFrame{
/**
* Method main
*
*
* @param args
*
*/
JButton b = new JButton("click me");
JPanel p = new JPanel();
public static void main(String[] args) {
// TODO: Add your code here
LoginDialog frame = new LoginDialog();
}
/**
* Method LoginDialog
*
*
*/
public LoginDialog() {
// TODO: Add your code here
final JFrame frame = this;
this.getContentPane().add(p,BorderLayout.SOUTH);
p.add(b);
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
showLoginDialog(frame);
}
});
this.setSize(300,200);
this.setTitle("show dialog");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.show();
}
void showLoginDialog(JFrame frame)
{
JPanel pn= new JPanel(new GridLayout(0,1));
JTextField tf = new JTextField(2);
JPasswordField tfPass = new JPasswordField(2);
pn.add(new JLabel("Username:"));
pn.add(tf);
pn.add(new JLabel("Password:"));
pn.add(tfPass);
//password
if(JOptionPane.showConfirmDialog(frame,pn
,"Login",JOptionPane.OK_CANCEL_OPTION
,JOptionPane.PLAIN_MESSAGE)
!=JOptionPane.OK_CANCEL_OPTION)
{
System.out.println("User Name:"+tf.getText());
System.out.println("Password:"+new String(tfPass.getPassword()));
}
}
}
评论