英文:
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("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);
// You can use `this.listener` to access the stored private attribute
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论