让JTextField响应一个onChanged事件

解决方案 »

  1.   

    给JTextField加个ActionListener
      

  2.   

    分析该代码!!!!
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    public class FlowLayoutTest extends JFrame implements TextListener
    {
    public static final int WIDTh =300;
    public static final int HEIGHT = 200;
    private JTextArea theText;
    public  JTextField name;
    public FlowLayoutTest()
    {
    setSize(480,200);
    addWindowListener(new WindowDestroyer());
    setTitle("测试");
    Container contentPane = getContentPane();
    contentPane.setLayout(new FlowLayout());                  JPanel firstLine1 = new JPanel();
    JLabel nameLabel = new JLabel("输入姓名:");
    JLabel passportLabel = new JLabel("输入密码:");
      name = new JTextField(12);
     name.addTextListener(this);/*该句有错*/    JTextField passport = new JTextField(12); JTextArea theText = new JTextArea(80,40);
    theText.setEditable(true);
    JScrollPane scrolledText = new JScrollPane(theText);
    scrolledText.setVerticalScrollBarPoli(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    contentPane.add(nameLabel);
    contentPane.add(name);
    contentPane.add(passportLabel);
    contentPane.add(passport); contentPane.add(scrolledText);
    } public void textValueChanged(TextEvent e)
    {
    theText.setText("你好,"+name.getText()+",欢迎你!");
    }
    public static void main(String[]args)
    {
    FlowLayoutTest flow = new FlowLayoutTest();
    flow.setVisible(true);
    }}
      

  3.   

    可以给它加入ActionListener, 一回车就会响应
      

  4.   

    我已经解决了,问题的关键在于把JTextArea的对象的误定义为局部变量,之后actionPerformde()中就不能访问它了!!