【问题标题】:How to determine the event source in an ActionListener?如何确定 ActionListener 中的事件源?
【发布时间】:2014-03-04 03:27:19
【问题描述】:

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

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();
    }

}

我想要的是进入我的内部类 (ButtLis) 的 if 语句,它将确定按下了哪些按钮,然后基于此将某些文本附加到 JTextArea。但我不知道该调用什么来找出按下了哪个按钮。

【问题讨论】:

标签: java swing jbutton actionlistener


【解决方案1】:

您有几个选择。在当前情况下,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 上查看更多信息

【讨论】:

  • 谢谢。这看起来像我正在寻找的东西。在我发布之后,我想“哦,等一下,也许按钮应该有一个全局范围”,但只是把它们留在了方法中,因为懒惰并认为有人会指出它。
【解决方案2】:

我认为这就是您正在寻找的,尽管我几乎不会推荐它:

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,您会说类似的话。这样做的好处是动作侦听器就在它工作的按钮旁边,它可以防止您拥有一个必须检查每个事件来自哪里的动作侦听器(使用e.getSource())。

【讨论】:

  • 是的,这就是我最初正在做的,但我想避免为每个按钮使用单独的方法,因为它们不止两个。
【解决方案3】:

在你的 ButtLis 中,添加这个

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

【讨论】:

    【解决方案4】:

    这里是实现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));
    

    对于一般的 ActionListsner,我建议像这样的不同客户 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());
        }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-11
      相关资源
      最近更新 更多