Recieving an ActionListener as a constructor parameter and storing it so other methods in the class can add that action listener to buttons?

huangapple go评论70阅读模式
英文:

Recieving an ActionListener as a constructor parameter and storing it so other methods in the class can add that action listener to buttons?

问题

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 * 数字键盘类
 */
public class NumberPad extends JPanel {
    /**
     * 构造函数
     */
    public NumberPad(ActionListener listener) {
        super();
        this.listener = listener; // 将传入的 ActionListener 对象存储在私有属性 listener 中
        setupLayout();
    }

    /**
     * 设置和布局数字键盘
     */
    private void setupLayout() {
        // 设置和布局数字键盘
        GridLayout pinpad = new GridLayout(4, 3);
        setLayout(pinpad);
        addButton("1");
        addButton("2");
        addButton("3");
        addButton("4");
        addButton("5");
        addButton("6");
        addButton("7");
        addButton("8");
        addButton("9");
        addButton("Del");
        addButton("0");
        addButton("C");
    }

    private void addButton(String anyString) {
        JButton anyButton = new JButton(anyString);
        anyButton.addActionListener(listener); // 将监听器添加到按钮中
        add(anyButton);
    }

    private ActionListener listener; // 私有属性,存储传入的 ActionListener 对象
}
英文:

So I have a NumberPad class, with a class constructor NumberPad(ActionListener listener). I then need to add listener to a button in the addButton() method within the NumberPad class below.
This is specifically how my Professor is asking this to be done. He even says "Modify the constructor in the NumberPad class so that it is now passed an ActionListenerobject that it stores in a private attribute named listener". But I can not figure out how to store an Action Listener object.



import java.awt.event.*;
import javax.swing.*;

;/**
 * A numeric keypad
 */
public class NumberPad extends    JPanel
{
    /**
     * Default Constructor
     */
    public NumberPad(ActionListener listener)
    {
        super();
        setupLayout();
    }

    /**
     * Setup and layout this NumberPad
     */
    private void setupLayout()
    {
        // Setup and layout this NumberPad
    	GridLayout pinpad = new GridLayout (4, 3);
    	setLayout(pinpad);
    	addButton("1");
    	addButton("2");
    	addButton("3");
    	addButton("4");
    	addButton("5");
    	addButton("6");
    	addButton("7");
    	addButton("8");
    	addButton("9");
    	addButton("Del");
    	addButton("0");
    	addButton("C");
    }
    
    private void addButton(String anyString)
    {
    	JButton anyButton = new JButton(anyString);
    	add(anyButton);
    }
    
}```

</details>


# 答案1
**得分**: 0

```java
public class NumberPad extends JPanel
{
    private ActionListener listener;

    public NumberPad(ActionListener listener)
    {
        super();
        this.listener = listener;
        setupLayout();
    }

    private void setupLayout()
    {
        GridLayout pinpad = new GridLayout(4, 3);
        setLayout(pinpad);
        addButton("1");
        addButton("2");
        addButton("3");
        addButton("4");
        addButton("5");
        addButton("6");
        addButton("7");
        addButton("8");
        addButton("9");
        addButton("Del");
        addButton("0");
        addButton("C");
    }
    
    private void addButton(String anyString)
    {
        JButton anyButton = new JButton(anyString);
        add(anyButton);
    }
}
英文:
public class NumberPad extends    JPanel
{
    private ActionListener listener;
    /**
     * Default Constructor
     */
    public NumberPad(ActionListener listener)
    {
        super();
        // Store as private attribute here
        this.listener = listener;
        setupLayout();
    }

    /**
     * Setup and layout this NumberPad
     */
    private void setupLayout()
    {
        // Setup and layout this NumberPad
        GridLayout pinpad = new GridLayout (4, 3);
        setLayout(pinpad);
        addButton(&quot;1&quot;);
        addButton(&quot;2&quot;);
        addButton(&quot;3&quot;);
        addButton(&quot;4&quot;);
        addButton(&quot;5&quot;);
        addButton(&quot;6&quot;);
        addButton(&quot;7&quot;);
        addButton(&quot;8&quot;);
        addButton(&quot;9&quot;);
        addButton(&quot;Del&quot;);
        addButton(&quot;0&quot;);
        addButton(&quot;C&quot;);
    }
    
    private void addButton(String anyString)
    {
        JButton anyButton = new JButton(anyString);
        add(anyButton);
        // You can use `this.listener` to access the stored private attribute
    }
    
}

huangapple
  • 本文由 发表于 2020年10月23日 05:25:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/64490767.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定