gpt4 book ai didi

Java 在单独的类上添加 ActionListener

转载 作者:搜寻专家 更新时间:2023-11-01 01:36:29 24 4
gpt4 key购买 nike

这是我想要做的,其中一个类用于包含所有 JButton 的 JFrame,我希望另一个类监听对 JFrame 类所做的操作。请看下面的代码:

public class Frame extends JFrame{
//all the jcomponents in here

}

public class listener implements ActionListener{
//listen to the actions made by the Frame class

}

感谢您的宝贵时间。

最佳答案

只需将监听器的新实例添加到您想要监听的任何组件即可。任何实现 ActionListener 的类都可以作为监听器添加到您的组件。

public class Frame extends JFrame {
JButton testButton;

public Frame() {
testButton = new JButton();
testButton.addActionListener(new listener());

this.add(testButton);
}
}

关于Java 在单独的类上添加 ActionListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11888463/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com