事件处理和界面不在同一个类中的设计方法

作者在 2010-05-20 14:10:01 发布以下内容
package com.yds.gui.fourpart;

import java.awt.Button;
import java.awt.Frame;
import java.awt.Panel;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class TextAreaBea {
    Frame f;
    TextArea ta;
    Button b;
    TextField tf;

    public void UserUI() {
        f = new Frame("提交事件");
        ta = new TextArea();
        Panel p = new Panel();
        b = new Button("提交");
        tf = new TextField(15);
        
        Monst m = new Monst(ta, tf,b);
        tf.addActionListener(m);
        ta.addMouseMotionListener(m);
        b.setActionCommand("commit");
        b.addActionListener(m);
        p.add(tf);
        p.add(b);
        f.add(ta, "Center");

        f.add(p, "South");
        f.pack();
        f.addWindowListener(new WindowAdapter() {

            public void windowClosing(WindowEvent e) {
                System.exit(1);
            }

        });
        f.setVisible(true);
    }

    public static void main(String arg[]) {
        TextAreaBea tab = new TextAreaBea();
        tab.UserUI();
    }
}
上面是界面类
package com.yds.gui.fourpart;

import java.awt.Button;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseMotionAdapter;

public class Monst extends MouseMotionAdapter implements ActionListener {
    TextField tf;
    TextArea ta;
    Button b;
    Monst(){
        
    }
    Monst(TextArea ta,TextField tf,Button b){
        this.ta=ta;
        this.tf=tf;
        this.b=b;
    }
    
    public void actionPerformed(ActionEvent e) {
        if(e.getActionCommand()=="commit"){
          ta.append("你说的是:"+tf.getText()+"\n");
          tf.setText(null);
        }

    }

}
awt和Sring | 阅读 1054 次
文章评论,共0条
游客请输入验证码
浏览290869次