JTextField  和  JPasswordField 只有一行, 他们接受来自键盘的输入或输出显示信息。
在JPasswordField中,输入的字符是加密的(变成星号);
注意  JTextField  和  JPasswordField  拼写的时候  F   要大写!!!
下面是有关JTextField  和  JPasswordField  使用的源代码
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TextFieldTest extends JFrame
{
    private JTextField textField1,textField2,textField3;
    private JPasswordField  passwordField;
    public TextFieldTest()
    {
        super ( " Sinbad Testing JTextField and JPasswordField " );
        Container container = getContentPane();
        container.setLayout( new FlowLayout() );
        textField1 = new JTextField( 10 );
        container.add( textField1 );
        textField2 = new JTextField( "Enter the Text here");
        container.add( textField2 );
        textField3 = new JTextField ( "Uneditable TextField(不可编辑状态)",20);
        textField3.setEditable( false );
        container.add( textField3 );
        passwordField = new JPasswordField (" Hiden Text ");
        container.add ( passwordField );        
        TextFieldHandler handler = new TextFieldHandler();
        textField1.addActionListener( handler );
        textField2.addActionListener(handler);
        textField3.addActionListener(handler);
        passwordField.addActionListener( handler);
        setSize( 325,100 );
        setVisible( true );
    }
    public static void main( String args[] )
    {
        TextFieldTest application = new TextFieldTest();
        application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    private class TextFieldHandler implements ActionListener
    {
        public void actionPerformed (ActionEvent event)
        {
            String string = "";
            if ( event.getSource() == textField1 )
            {
                string = "TextField1: " + event.getActionCommand();
            }
            if ( event.getSource() == textField2 )
            {
                string = "TextField2: " + event.getActionCommand();
            }
            if ( event.getSource() == textField3 )
            {
                string = "TextField3: " + event.getActionCommand();
            }
            else if ( event.getSource() == passwordField )
            {
                JPasswordField pwd = (JPasswordField)event.getSource();
                string = "passwordField: " + new String(passwordField.getPassword());
            }
            JOptionPane.showMessageDialog(null,string);
        }
    }
}
[ 此贴被八运会在2007-02-14 20:05重新编辑 ]