import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class JPassword
{
JFrame f = new JFrame("单行编辑框、口令输入框、多行编辑框的用法!");
private JTextField user = new JTextField(20);
private JPasswordField pwd = new JPasswordField(10);
private JTextArea ta = new JTextArea(5,10);
Font ft = new Font("Serif",Font.BOLD,28);
JPanel jp = new JPanel(new GridLayout(2,2,10,10));
public static void main(String[] args)
{
JPassword that = new JPassword();
that.go();
}
void go()
{
f.getContentPane().setLayout(new BorderLayout(0,10));
f.getContentPane().add("North",jp);
Label l1 = new Label("用户名:",JLabel.RIGHT);
jp.add(l1);
jp.add(user);
Label l2 = new Label("口令:",JLabel.RIGHT);
jp.add(l2);
jp.add(pwd);
f.getContentPane().add("Center",ta);
ta.setFont(ft);
user.setFont(ft);
pwd.setFont(ft);
l1.setFont(ft);
l2.setFont(ft);
user.addActionListener(new TextHandler(1));
pwd.addActionListener(new TextHandler(2));
f.addWindowListener(new WindowHandler());
f.setSize(450,300);
f.setResizable(true);
f.setVisible(true);
}
class TextHandler implements ActionListener
{
int sel;
TextHandler(int sel)
{
this.sel = sel;
}
public void actionPerformed(ActionEvent e)
{
String uname,upass;
uname = user.getText();
upass = new String(pwd.getPassword());
ta.setText("你输入的用户名是"+uname+"/n"+"你输入的口令是"+upass);
}
}
class WindowHandler extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
System.exit(1);
}
}
}
代码编译没问题,但是运行不起来,高手看了不妨运行下试试看