gpt4 book ai didi

Java ActionListener 错误 : incompatible types

转载 作者:行者123 更新时间:2023-11-29 09:41:03 25 4
gpt4 key购买 nike

我在 JButton 上设置 ActionListener 时遇到了一些问题,这是代码...

package pipes;

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

public class PipesUI extends javax.swing.JFrame {

Main main = new Main();

JButton addPipeButton = new JButton("Add Pipe");

public PipesUI(){
addUI();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

private void addUI(){
addPipeButton.addActionListener(this);
}

public void actionPerformed(ActionEvent e){
if (e.getSource()==addPipeButton)
main.addPipe();
else
;
}

public static void main(String args[]) {
PipesUI pipesUI = new PipesUI(); // create an instance of the menu
pipesUI.setSize(500,500);
pipesUI.setVisible(true);
}
}

错误在线addPipeButton.addActionListener(this);

(this) 它似乎不喜欢,错误说“不兼容的类型:PipesUI 无法转换为 ActionListener”

如果有任何帮助,我们将不胜感激。

最佳答案

您创建了actionPerformed 方法,但没有将您的类声明为ActionListener。实现该接口(interface):

public class PipesUI extends javax.swing.JFrame implements ActionListener {

关于Java ActionListener 错误 : incompatible types,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19865312/

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