Java “AbstractButton 类型中的 addActionListener(ActionListener) 方法不适用于参数”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18222360/
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
"The method addActionListener(ActionListener) in the type AbstractButton is not applicable for the arguments"
提问by David
I am a beginner programmer and I am running into a problem with an action listener and my GUI. Here is my code:
我是一名初学者程序员,我遇到了动作侦听器和 GUI 的问题。这是我的代码:
MAIN CLASS--
主要班级--
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class prog extends JFrame {
//add instance variable to hold lockd
prog app = new prog();
//create buttons
JPanel row1 = new JPanel();
JButton oneLeft = new JButton("oneLeft");
JButton oneRight = new JButton("oneRight");
JPanel row2 = new JPanel();
JButton twoLeft = new JButton("twoLeft");
JButton twoRight = new JButton("twoRight");
JPanel row3 = new JPanel();
JButton threeLeft = new JButton("threeLeft");
JButton threeRight = new JButton("threeRight");
public prog() {
super("Prog");
setLookAndFeel();
setSize(400, 800);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GridLayout layout = new GridLayout(3, 2);
setLayout(layout);
//add Listeners
oneLeft.addActionListener(app);
oneRight.addActionListener(app);
twoLeft.addActionListener(app);
twoRight.addActionListener(app);
threeLeft.addActionListener(app);
threeRight.addActionListener(app);
setVisible(true);
}
private void setLookAndFeel() {
try {
UIManager.setLookAndFeel("com.sun.java.plaf.");
} catch (Exception e) {
//ignore error
}
}
public static void main(String[] args) {
prog progApp = new prog();
}
}
I am getting an error in this class with the action listeners: The method addActionListener(ActionListener) in the type AbstractButton is not applicable for the arguments (prog).
我在使用动作侦听器的此类中遇到错误:AbstractButton 类型中的方法 addActionListener(ActionListener) 不适用于参数 (prog)。
Full errors:
完整错误:
Description Resource Path Location Type
The method addActionListener(ActionListener) in the type AbstractButton is not applicable for the arguments (prog) prog.java /Experiment/src line 33 Java Problem
The method addActionListener(ActionListener) in the type AbstractButton is not applicable for the arguments (prog) prog.java /Experiment/src line 35 Java Problem
The method addActionListener(ActionListener) in the type AbstractButton is not applicable for the arguments (prog) prog.java /Experiment/src line 34 Java Problem
The method addActionListener(ActionListener) in the type AbstractButton is not applicable for the arguments (prog) prog.java /Experiment/src line 37 Java Problem
The method addActionListener(ActionListener) in the type AbstractButton is not applicable for the arguments (prog) prog.java /Experiment/src line 36 Java Problem
The method addActionListener(ActionListener) in the type AbstractButton is not applicable for the arguments (prog) prog.java /Experiment/src line 38 Java Problem
and here is my listener class:
这是我的听众课:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
public class progEvent implements ActionListener {
prog GUI;
String newLine = System.getProperty("line.separater");
public progEvent(prog in) {
GUI = in;
}
public void actionPerformed(ActionEvent event) {
String command = event.getActionCommand();
try {
File numClicks = new File("numClicks.properties");
FileOutputStream outStream = new FileOutputStream(numClicks);
if (command.equals("oneLeft")) {
write(outStream, "oneLeft has been clicked.");
}
if (command.equals("oneRight")) {
write(outStream, "oneRight has been clicked.");
}
if (command.equals("twoLeft")) {
write(outStream, "twoLeft has been clicked.");
}
if (command.equals("twoRight")) {
write(outStream, "twoRight has been clicked.");
}
if (command.equals("threeLeft")) {
write(outStream, "threeLeft has been clicked.");
}
if (command.equals("threeRight")) {
write(outStream, "threeRight has been clicked.");
}
} catch (IOException ioe) {
System.out.println("The file could not be written to.");
}
}
void write(FileOutputStream stream, String output) throws IOException {
output = output + newLine;
byte[] data = output.getBytes();
stream.write(data, 0, data.length);
}
}
I can't get my GUI to show either. I'm using a book to learn it, and using a class as a kind of template for this experiment. But I'm completely stuck. Thanks!
我也无法显示我的 GUI。我用一本书来学习它,并使用一个类作为这个实验的模板。但我完全被困住了。谢谢!
采纳答案by chrylis -cautiouslyoptimistic-
You're trying to add an instance of your prog
class as the ActionListener
, but your ActionListener
is progEvent
instead. You need to add a progEvent listener = new progEvent();
and then addActionListener(progEvent)
.
您正在尝试将您的prog
类的一个实例添加为ActionListener
,但您的ActionListener
却是progEvent
。您需要添加一个progEvent listener = new progEvent();
然后addActionListener(progEvent)
.
(Also, you misspelled "separator" in your system property; you won't retrieve what you're looking for.)
(此外,您在系统属性中拼错了“分隔符”;您将无法检索到您要查找的内容。)
回答by ambassallo
Your class should implement ActionListener. so your prog definition should be "prog extends JFrame implements ActionListener". Then implement your actionperformed . for example
您的类应该实现 ActionListener。所以你的编定义应该是“编扩展 JFrame 实现 ActionListener”。然后执行您的 actionperformed 。例如
public void actionPerformed(ActionEvent arg0) {
if ("connect".equalsIgnoreCase(arg0.getActionCommand())) {
connect();
}
}