本文介绍了Java ActionListener错误:不兼容的类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在JButton上设置ActionListener时遇到了一些问题,这里是代码......
I'm having some trouble setting up an ActionListener on a JButton, here is the code...
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);
The error is on the lineaddPipeButton.addActionListener(this);
它似乎不喜欢(这个),错误说不兼容的类型:PipesUI无法转换为ActionListener
The (this) it doesn't seem to like, the error says 'incompatible types: PipesUI cannot be converted to ActionListener'
任何帮助都会非常感谢。
Any help would be great thanks.
推荐答案
您创建了 actionPerformed
方法,但是您没有声明您的类为 ActionListener
。实现该接口:
You created your actionPerformed
method, but you didn't declare your class as an ActionListener
. Implement that interface:
public class PipesUI extends javax.swing.JFrame implements ActionListener {
这篇关于Java ActionListener错误:不兼容的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!