Java ActionListener 错误:类型不兼容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19865312/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java ActionListener error: incompatible types
提问by benharris
I'm having some trouble setting up an ActionListener on a JButton, here is the code...
我在 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);
}
}
The error is on the line addPipeButton.addActionListener(this);
错误在行 addPipeButton.addActionListener(this);
The (this) it doesn't seem to like, the error says 'incompatible types: PipesUI cannot be converted to ActionListener'
(this) 它似乎不喜欢,错误说“不兼容的类型:PipesUI 无法转换为 ActionListener”
Any help would be great thanks.
任何帮助都会非常感谢。
采纳答案by rgettman
You created your actionPerformed
method, but you didn't declare your class as an ActionListener
. Implement that interface:
您创建了actionPerformed
方法,但没有将类声明为ActionListener
. 实现该接口:
public class PipesUI extends javax.swing.JFrame implements ActionListener {
回答by nanofarad
I'm guessing you saw addActionListener(this)
in another place and decided to use it here. That other place was a completely different context, as this
was an instance of ActionListner.
我猜你addActionListener(this)
在另一个地方看到过并决定在这里使用它。另一个地方是一个完全不同的环境,就像this
ActionListner 的一个实例。
Do as follows:
执行以下操作:
addPipeButton.addActionListener(new ActionListner(){
public void actionPerformed(ActionEvent e){
//handle `e`
}
});
Edit: You already have ActionListener
methods in your class. In thiscase, you can use:
编辑:您ActionListener
的课程中已经有了方法。在这种情况下,您可以使用:
public class PipesUI extends javax.swing.JFrame implements ActionListener {
回答by Markus Koivisto
One possible solution:
一种可能的解决方案:
public class PipesUI extends javax.swing.JFrame implements ActionListener{
//implement the relevant methods
Another:
其他:
private void addUI(){
addPipeButton.addActionListener(new ActionListener(){
//anonymous actionlistener implementation
};
}
and so on and so forth.
等等等等。
回答by Sage
addPipeButton.addActionListener(this);
Within an instance method or a constructor, this
is a reference to the current object — the object whose method or constructor is being called. So, the addActionListener(ActionListener)
method is expecting an implemented instance of interface ActionListener
. When you are passing this
to addActionListener
function, which is currently referencing the instance of PipesUI
wasn't implemented with ActionListener
.
在实例方法或构造函数中,this
是对当前对象的引用——正在调用其方法或构造函数的对象。因此,该addActionListener(ActionListener)
方法需要一个已实现的 interface 实例ActionListener
。当您传递this
给addActionListener
函数时,该函数当前引用的实例PipesUI
未使用ActionListener
.
Implement the ActionListener
as follows:
执行ActionListener
如下:
public class PipesUI extends javax.swing.JFrame implements ActionListener
{
//your code
@Override
public void actionPerformed(ActionEvent e) {
// your code
}
}
But from your code i don't see you have added the addPipeButton
to any container. Just add the button to your frame PipesUI
using add(addPipeButton)
as shown follows:
但是从您的代码中,我没有看到您已将其添加addPipeButton
到任何容器中。只需PipesUI
使用add(addPipeButton)
如下所示将按钮添加到您的框架中:
private void addUI(){
add(addPipeButton); <--- adding pipe button
addPipeButton.addActionListener(this);
}
This Jframe.add(addPipeButton)
will add the addPipeButton
button to the frame's content pane which has BorderLayout
as default layout manager.
这Jframe.add(addPipeButton)
会将addPipeButton
按钮添加到框架的内容窗格中,该窗格具有BorderLayout
默认布局管理器。