如何确定ActionListener中的事件源?

问题描述 投票:3回答:4

好。我不确定我的问题的标题以及我是否使用了正确的词语。因为我是一个自学成才的全业余爱好者,我发现很难问我的问题,因为我不知道正确的条款,所以我会在代码中写一些东西然后问我的问题。我写的没有import语句,设置布局和滚动条以及其他一些东西只是为了让它更简单。

public class Foo{
    JTextArea text;

    public static void main(String[] args){
        Foo foo = new Foo;
        foo.go();
    }

    public void go(){
        JFrame frame = new JFrame();
        JButton button = new JButton("One");
        JButton button2 = new JButton("Two");
        JPanel panel = new JPanel();

        frame.setVisible(true);
        frame.setSize(600, 300);

        frame.getContentPane().add(BorderLayout.EAST, panel);
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
        panel.add(button);
        panel.add(button2);

        text = new JTextArea(10, 20);
        panel.add(text);

        button.addActionListener(new ButtLis());
        button2.addActionListener(new ButtLis());
    }

    class ButtLis implements ActionListener{
        @override
         // this is where I have the problem

        text.append();
    }

}

我想要的是一个if语句进入我的内部类(ButtLis),它将决定按下哪个按钮,然后根据它将某些文本附加到JTextArea。但我不知道该怎么做才能找出按下哪个按钮。

java swing jbutton actionlistener
4个回答
2
投票

你有几个选择。在当前情况下,JButton对象在构造函数中是局部作用域的,您需要检查actionCommmand,因为ActionListener无法使用当前作用域访问这些对象。所以你可以做到这一点

class ButtLis implements ActionListener{
    @Override
    public void actionPerformed(ActionEvent e) {
        String command = e.getActionCommand();
        if ("One".equals(command)) {
            // do something
        }
    }
}

如果要比较对象源,则需要为按钮指定全局范围

public class Foo {
    JButton button = new JButton("One");
    JButton button2 = new JButton("Two");

    class ButtLis implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == button) {

            }
        }
    }
}

第三种选择是单独注册按钮

public void go() {
    ...
    button.addActionListener(new ActionListener(){
         @Override
         public void actionPerformed(ActionEvent e) {
             // do something
         }
    });
}

How to use Common ButtonHow to Write ActionListeners上查看更多信息


1
投票

我认为这就是你要找的东西,虽然我很难推荐它:

class ButtLis implements ActionListener {
    private JTextArea text;

    public ButtLis(JTextArea text) {
        this.text = text;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        JButton button = (JButton)e.getSource(); // Warning! This is not good coding practice, because you don't know that the source will be a button
        text.append(button.getText());
    }
}

相反,我建议:

JButton button1 = new JButton("One");
button1.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        text.append("one");
    }
});

它使用“匿名内部类”来定义动作侦听器。对于Button2,你会说类似的事情。这里的好处是动作监听器就在它工作的按钮旁边,它会阻止你有一个ActionListener,它必须检查每个事件的来源(使用e.getSource())。


0
投票

在你的ButtLis里面,加上这个

class ButtLis implements ActionListener {
    public void actionPerformed(ActionEvent e) {
       e.getSource();
       //Your implementation
    }
}

0
投票

这里是实现ActionListener的类:

class ButtLis implements ActionListener {

    JTextArea text;

    public ButtLis(JTextArea text) {
        this.text = text;
    }

    @Override
    public void actionPerformed(ActionEvent ae) {
        if (ae.getSource() instanceof JButton) {
            JButton button = (JButton) ae.getSource();
            if(text != null){
                text.append(" " + button.getText());
            }
        }
    }
}

这里是如何向按钮添加动作侦听器:

   button.addActionListener(new ButtLis(text));
   button2.addActionListener(new ButtLis(text));

对于一般的ActionListener,我建议使用不同的客户ActionListener:

abstract class ButtLis implements ActionListener {

    protected String sourceEvent;  //or you can use a reference for the source object

    public ButtLis(String sourceEvent) {
        this.sourceEvent = sourceEvent;
    }

    public String getSourceEvent() {
        return sourceEvent;
    }

    @Override
    public void actionPerformed(ActionEvent ae) {
        customer_actionPerformed(ae);
    }

    public abstract void customer_actionPerformed(ActionEvent ae);
}

并且为任何组件添加动作侦听器与普通的ActionListener相同:

//for exemple button
button.addActionListener(new ButtLis(button.getText()) {
    @Override
    public void customer_actionPerformed(ActionEvent ae) {        
        text.append(getSourceEvent());
    }
});
© www.soinside.com 2019 - 2024. All rights reserved.