将事件监听器放在不同的类中会更好吗?

时间:2022-04-03 00:03:05

I have been told by my teacher that having a separate class for the actionListener is best. In the separate class, should I just make one actionListener method eg.

我的老师告诉我,为actionListener设置一个单独的类是最好的。在单独的类中,我应该只创建一个actionListener方法,例如。

public void actionPerformed(ActionEvent e) {
    if(e.getSource() == myButton) {
        //do something
    }
    else if(e.getSource() == myComboBox) {
        //do something
    }
}

or should I make many? I have a feeling I should make many but I have no idea how to go about it. What would the different parameters be? I will be making an instance of this class in my View class where the button and combobox is. (this would be the Controller class as i'm trying to do MVC)

或者我应该做多少?我有一种感觉,我应该做很多,但我不知道如何去做。不同的参数是什么?我将在我的View类中创建这个类的实例,其中按钮和组合框是。 (这将是Controller类,因为我正在尝试做MVC)

For the button I would like for the background to change colour when the mouse hovers over it and then of course do something when clicked. The combobox simply needs to return whatever option was selected as String.

对于按钮,我希望背景在鼠标悬停在其上时改变颜色,然后当点击时做一些事情。组合框只需返回被选为String的任何选项。

2 个解决方案

#1


2  

I prefer to write a separate action listener for each controller action. It keeps things simpler for my simple mind.

我更喜欢为每个控制器动作编写一个单独的动作侦听器。它让我的简单思想变得简单。

My rule of thumb is, for very short (1 - 3 lines) action listeners, I'll make them inline anonymous classes.

我的经验法则是,对于非常短(1-3行)的动作听众,我会让它们成为内联匿名类。

For medium size action listeners (1 - 3 methods), I'll make them an inner class, especially if the action listener needs 3 or more fields from the outer main class.

对于中等大小的动作侦听器(1 - 3种方法),我将使它们成为内部类,特别是如果动作侦听器需要来自外部主类的3个或更多字段。

For large action listeners, I'll write a separate class.

对于大型动作听众,我会写一个单独的类。

#2


2  

When you make a separate class for the action listener, you can reuse the action event you made.

为动作侦听器创建单独的类时,可以重用所做的动作事件。

I will give you an example for that

我会举一个例子

This class have an action listener

这个类有一个动作监听器

class  AListener implements ActionListener
{
    JTextField tf;
    AListener(JTextField tf)
    {
        this.tf=tf;
    }

    public void actionPerformed(ActionEvent e)
    {
        if((e.getActionCommand()).equals("Button1"))
            tf.setText("Button1 clicked");
        if((e.getActionCommand()).equals("Button2"))
            tf.setText("Button2 clicked");
        if((e.getActionCommand()).equals("Button3"))
            tf.setText("Button3 clicked");
    }
} 

And this is the GUI class, which has 3 buttons and one text field and each button print something different in the text field, so as you can see, it is not logical to make 3 different action listener methods in the GUI class.

这是GUI类,它有3个按钮和一个文本字段,每个按钮在文本字段中打印不同的内容,因此您可以看到,在GUI类中创建3个不同的动作侦听器方法是不合逻辑的。

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ActionDemo extends JFrame 
{
    public ActionDemo() 
    {
        setLayout(new FlowLayout());

        // Add buttons to the frame
        JButton b1=new JButton("Button1");
        JButton b2=new JButton("Button2");
        JButton b3=new JButton("Button3");
        JTextField tf=new JTextField(10);

        AListener listen=new  AListener(tf);
        b1.addActionListener(listen);
        b2.addActionListener(listen);
        b3.addActionListener(listen);

        add(b1);add(b2);add(b3);add(tf);
  }

  public static void main(String[] args) 
  {
      ActionDemo frame = new ActionDemo();
      frame.setTitle("Action Demo");
      frame.setLocationRelativeTo(null);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setSize(500, 100);
      frame.setVisible(true);
  }
}

So now, every time a button clicked, one action listener method only will be called. Inside this method, I will check which button called me.

所以现在,每次单击一个按钮时,只会调用一个动作侦听器方法。在这个方法里面,我会检查哪个按钮叫我。

#1


2  

I prefer to write a separate action listener for each controller action. It keeps things simpler for my simple mind.

我更喜欢为每个控制器动作编写一个单独的动作侦听器。它让我的简单思想变得简单。

My rule of thumb is, for very short (1 - 3 lines) action listeners, I'll make them inline anonymous classes.

我的经验法则是,对于非常短(1-3行)的动作听众,我会让它们成为内联匿名类。

For medium size action listeners (1 - 3 methods), I'll make them an inner class, especially if the action listener needs 3 or more fields from the outer main class.

对于中等大小的动作侦听器(1 - 3种方法),我将使它们成为内部类,特别是如果动作侦听器需要来自外部主类的3个或更多字段。

For large action listeners, I'll write a separate class.

对于大型动作听众,我会写一个单独的类。

#2


2  

When you make a separate class for the action listener, you can reuse the action event you made.

为动作侦听器创建单独的类时,可以重用所做的动作事件。

I will give you an example for that

我会举一个例子

This class have an action listener

这个类有一个动作监听器

class  AListener implements ActionListener
{
    JTextField tf;
    AListener(JTextField tf)
    {
        this.tf=tf;
    }

    public void actionPerformed(ActionEvent e)
    {
        if((e.getActionCommand()).equals("Button1"))
            tf.setText("Button1 clicked");
        if((e.getActionCommand()).equals("Button2"))
            tf.setText("Button2 clicked");
        if((e.getActionCommand()).equals("Button3"))
            tf.setText("Button3 clicked");
    }
} 

And this is the GUI class, which has 3 buttons and one text field and each button print something different in the text field, so as you can see, it is not logical to make 3 different action listener methods in the GUI class.

这是GUI类,它有3个按钮和一个文本字段,每个按钮在文本字段中打印不同的内容,因此您可以看到,在GUI类中创建3个不同的动作侦听器方法是不合逻辑的。

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ActionDemo extends JFrame 
{
    public ActionDemo() 
    {
        setLayout(new FlowLayout());

        // Add buttons to the frame
        JButton b1=new JButton("Button1");
        JButton b2=new JButton("Button2");
        JButton b3=new JButton("Button3");
        JTextField tf=new JTextField(10);

        AListener listen=new  AListener(tf);
        b1.addActionListener(listen);
        b2.addActionListener(listen);
        b3.addActionListener(listen);

        add(b1);add(b2);add(b3);add(tf);
  }

  public static void main(String[] args) 
  {
      ActionDemo frame = new ActionDemo();
      frame.setTitle("Action Demo");
      frame.setLocationRelativeTo(null);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setSize(500, 100);
      frame.setVisible(true);
  }
}

So now, every time a button clicked, one action listener method only will be called. Inside this method, I will check which button called me.

所以现在,每次单击一个按钮时,只会调用一个动作侦听器方法。在这个方法里面,我会检查哪个按钮叫我。