import java.awt.*;
import javax.swing.*;
import java.awt.event.*;public class TextFieldTest extends JFrame 
{
private JTextField textField1,textField2,textField3;
private JPasswordField passwordField; public TextFieldTest()
{
super("我的swing"); Container container = new Container();
container.setLayout(new FlowLayout()); textField1 = new JTextField(10);
container.add(textField1); textField2 = new JTextField("Enter text here");
container.add(textField2); textField3 = new JTextField("Uneditable text field",20);
textField3.setEditable(false);
container.add(textField3); passwordField = new JPasswordField("Hidden 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);

} class TextFieldHandler implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
String string = ""; if (event.getSource()==textField1)

string = "texField:"+event.getActionCommand(); else if (event.getSource()==textField2)

string = "texField:"+event.getActionCommand();

else if (event.getSource()==textField3)

string = "texField:"+event.getActionCommand(); else if (event.getSource()==passwordField);

string = "passwordField:"+new String (passwordField.getPassword());

JOptionPane.showMessageDialog(null,string);
}
}
}
我运行后怎么没有运行出相关的文本字段
只有标题
我试着分析了一下
可还是没有找出来那里出了问题  请高手指教 感激不尽....

解决方案 »

  1.   

    1、this.setContentPane(container);
    加在
    setSize(325, 100);
    setVisible(true);
    之前
    2、
    else if (event.getSource()==passwordField);
    string = "passwordField:"+new String (passwordField.getPassword());
    改成
    else if (event.getSource()==passwordField)
    string = "passwordField:"+new String (passwordField.getPassword());
      

  2.   

    package com.geostar.client.geosurfEditor;/**
     * <p>Title: </p>
     *
     * <p>Description: </p>
     *
     * <p>Copyright: Copyright (c) 2007</p>
     *
     * <p>Company: </p>
     *
     * @author not attributable
     * @version 1.0
     */
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;public class TextFieldTest extends JFrame {
        private JTextField textField1, textField2, textField3;
        private JPasswordField passwordField;
        public TextFieldTest() {
            super("我的swing");
            Container container = new Container();
            container.setLayout(new FlowLayout());        textField1 = new JTextField(10);
            container.add(textField1);        textField2 = new JTextField("Enter text here");
            container.add(textField2);        textField3 = new JTextField("Uneditable text field", 20);
            textField3.setEditable(false);
            container.add(textField3);        passwordField = new JPasswordField("Hidden text");
            container.add(passwordField);        TextFieldHandler handler = new TextFieldHandler();        textField1.addActionListener(handler);
            textField2.addActionListener(handler);
            textField3.addActionListener(handler);
            passwordField.addActionListener(handler);
            this.setContentPane(container);
            setSize(325, 100);
            setVisible(true);
        }    public static void main(String[] args) {        TextFieldTest application = new TextFieldTest();        application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    }    class TextFieldHandler implements ActionListener {
            public void actionPerformed(ActionEvent event) {
                String string = "";            if (event.getSource() == textField1) {                string = "texField:" + event.getActionCommand();
                }            else if (event.getSource() == textField2) {                string = "texField:" + event.getActionCommand();
                }            else if (event.getSource() == textField3) {                string = "texField:" + event.getActionCommand();
                }//            else if (event.getSource() == passwordField) {
    //                ;
    //            }//            string = "passwordField:" + new String(passwordField.getPassword());
                else if (event.getSource()==passwordField)
                string = "passwordField:"+new String (passwordField.getPassword());            JOptionPane.showMessageDialog(null, string);
            }
        }
    }