当前对象this就成为了监听器对象
例如:
public class Test extends JFrame implements ActionListener{
private static final long serialVersionUID = 1L;
private JPanel jPanel;
private JButton btn;
public Test() {
setBounds(700, 500, 500, 500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
initComponet();
setVisible(true);
}
private void initComponet(){
//初始化组件
jPanel = new JPanel();
btn = new JButton("测试");
//设置布局管理器并添加组件
jPanel.add(btn);
add(jPanel);
//给组件添加事件监听器
btn.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
System.out.println("hello world");
}
public static void main(String[] args) {
new Test();
}
}
java学习笔记--让当前类实现监听接口
猜你喜欢
转载自blog.csdn.net/onepiece_loves/article/details/88757420
今日推荐
周排行