import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class TextApp extends JFrame implements ActionListener
{
private JLabel label1,label2;
private JTextField inputText,displayText;
private JPasswordField inputPwd;
public TextApp()
{
super("单行文本框的应用");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
label1=new JLabel("用户名:");
label2=new JLabel("密码:");
inputText=new JTextField("请输入名字:");
inputPwd=new JPasswordField(15);
displayText=new JTextField(30);
inputPwd.addActionListener(this);
inputText.addActionListener(this);
Container cp=getContentPane();
cp.setLayout(new FlowLayout());
cp.add(label1);
cp.add(inputText);
cp.add(label2);
cp.add(inputPwd);
cp.add(displayText);
setSize(400,200);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==inputText||e.getSource()==inputPwd)
displayText.setText("用户名:"+inputText.getText()+",密码:"+new String(inputPwd.getPassword()));
}
public static void main(String[] args)
{
TextApp frame=new TextApp();
frame.show();
}
}