package wo;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Ex5 implements ActionListener{
/**
* @param args
*/
JLabel lab1,lab2;
JFrame frame;
JButton b1,b2;
Container con;
JTextField use;
JPasswordField pass;
public void actionPerformed(ActionEvent e){
String str;
if(e.getSource()==b1){
str="你的用户名是"+use.getText()+"\n你的密码是"+new String(pass.getPassword());
JOptionPane.showMessageDialog(frame, str);
}else if(e.getSource()==b2){
pass.setText("");
use.setText("");
}else if(e.getSource()==use){
pass.requestFocus();
}else if(e.getSource()==pass){
b1.doClick();
}
}
public Ex5(){
frame=new JFrame("文本框和密码的使用");
con=frame.getContentPane();
//加载一个图片
lab1=new JLabel("用户名:");
lab2=new JLabel("密码:");
b1=new JButton("登录");
b2=new JButton("取消");
b1.addActionListener(this);
b2.addActionListener(this);
use=new JTextField();
use.setColumns(20);
use.addActionListener(this);
pass=new JPasswordField();
pass.setColumns(20);
pass.addActionListener(this);
con.setLayout(new FlowLayout());
con.add(lab1);
con.add(use);
con.add(lab2);
con.add(lab2);
con.add(pass);
con.add(b1);
con.add(b2);
frame.setSize(300,300);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new Ex5();
}
}