如何添加监听器

时间:2014-03-04 15:03:18

标签: java swing events awt listener

我很难为我的textarea添加自定义事件,但几天之后我还是无法处理它!我已经阅读了很多资源,最后我可以编写下面的代码。

问题

我认为一切都是正确的但我只是错误地添加了听众。(也许其他错误)。我不知道如何修复它或者它是问题的原因吗?

目的

我想做这个工作(测试),然后在我的主项目中使用它,所以下面的代码只是为了避免不相关的事情。

方案

我希望按钮能够触发我的textarea监听的自定义事件。请考虑(由于我的主要项目设计)我必须在方法中定义并添加textarea,这是我的问题的主要来源。但不幸的是,我不能改变这个决定。

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.Serializable;
import java.util.Enumeration;
import java.util.EventListener;
import java.util.EventObject;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.event.EventListenerList;

public class CustomEvent extends JFrame {
    private transient Vector listeners;
    private JPanel contentPane, panel;
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    CustomEvent frame = new CustomEvent();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
    public CustomEvent() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);
        JButton btnNewButton = new JButton("New button");
        btnNewButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                fireEvent("yes");
            }
        });
        contentPane.add(btnNewButton, BorderLayout.NORTH);
        fillPanel();
        contentPane.add(panel, BorderLayout.CENTER);
    }
    void fillPanel() {
        panel = new JPanel();
        panel.setLayout(new BorderLayout(0, 0));
        final MyTextArea textArea;
        textArea = new MyTextArea();
        //textField should listen to customEventObject
        panel.add(textArea, BorderLayout.CENTER);
        textArea.setColumns(10);
        textArea.addMyListener(new MyListener() {
            @Override
            public void actionPerformed(MyEvent e) {
                System.out.println(e.getArg());

            }
        });
    }
    protected void fireEvent(String str) {
        // if we have no listeners, do nothing...
        if (listeners != null && !listeners.isEmpty()) {
            // create the event object to send
            MyEvent event = new MyEvent(this, str);

            // make a copy of the listener list in case
            //   anyone adds/removes listeners
            Vector targets;
            synchronized (this) {
                targets = (Vector) listeners.clone();
            }

            // walk through the listener list and
            //   call the sunMoved method in each
            Enumeration e = targets.elements();
            while (e.hasMoreElements()) {
                MyListener l = (MyListener) e.nextElement();
                l.actionPerformed(event);
            }
        }
    }
}
class MyTextArea extends TextArea implements Serializable {
    private transient Vector listeners;
    synchronized public void addMyListener(MyListener l) {
        if (listeners == null)
            listeners = new Vector();
        listeners.addElement(l);
    }  
    synchronized public void removeMyListener(MyListener l) {
        if (listeners == null)
            listeners = new Vector();
        listeners.removeElement(l);
    }
}
//--------------------------------------------------------------
interface MyListener extends EventListener {
    public void actionPerformed(MyEvent e);
}
//--------------------------------------------------------------
class MyEvent extends EventObject {
    private String arg;
    public MyEvent(Object source, String arg) {
        super(source);
        this.arg = arg;
    }
    public String getArg() {
        return this.arg;
    }
}

1 个答案:

答案 0 :(得分:1)

首先,您没有向listener中的CustomListener字段添加任何侦听器。其次,MyTextArea中的听众从未被调用过。

修改

澄清:

您的Vector课程中有MyTextArea个听众,但没有办法解雇该事件。从我理解您的问题的方式来看,您希望MyTextArea成为CustomEvent的监听器,但您将其设计为生成事件的类,而不是接收事件的类。所以我会像这样更改MyTextArea

class MyTextArea extends TextArea implements MyListener {

    @Override
    public void actionPerformed(MyEvent e) {
        System.out.println(e.getArg());
    }
}

现在MyTextArea实现MyListener,您可以将其添加到CustomEvent.fillPanel()中的侦听器列表中:

void fillPanel() {
    panel = new JPanel();
    panel.setLayout(new BorderLayout(0, 0));
    final MyTextArea textArea;
    textArea = new MyTextArea();
    //textField should listen to customEventObject
    panel.add(textArea, BorderLayout.CENTER);
    textArea.setColumns(10);
    listeners.add(textArea);
}