【问题标题】:"The method addActionListener(ActionListener) in the type AbstractButton is not applicable for the arguments"“AbstractButton 类型中的方法 addActionListener(ActionListener) 不适用于参数”
【发布时间】:2013-08-15 20:27:24
【问题描述】:

我是一名初级程序员,我在操作监听器和我的 GUI 方面遇到了问题。这是我的代码:

主类--

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();
}
}

我在使用动作监听器的此类中遇到错误:AbstractButton 类型中的方法 addActionListener(ActionListener) 不适用于参数 (prog)。

全部错误:

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

这是我的监听类:

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);
}
}

我的 GUI 也无法显示。我正在用一本书来学习它,并使用一个类作为这个实验的一种模板。但我完全被困住了。谢谢!

【问题讨论】:

  • 类名大写是Java的标准做法;使用ProgProgEvent 将帮助其他试图阅读您的代码的人。另外,请复制整个错误消息,包括行号/字符号。
  • 我知道,我只需要更改一些东西并发布它。我知道代码有点草率,但我正试图专注于让它运行。不过谢谢。更新错误。
  • app 属于 prog 类型,它是 JFrame 的实例,而不是 ActionListener 的实例

标签: java eclipse swing actionlistener


【解决方案1】:

您尝试将prog 类的实例添加为ActionListener,但您的ActionListener 改为progEvent。您需要添加progEvent listener = new progEvent();,然后添加addActionListener(progEvent)

(另外,您在系统属性中拼错了“分隔符”;您将无法检索到您要查找的内容。)

【讨论】:

  • 在哪里添加 progEvent 监听器 = new progEvent(); ?它会在 add actionlistener 语句所在的主类中吗?
  • 是的。由于您希望使用相同的侦听器实例来处理所有按钮单击(这不是通常的做法),因此您需要创建一个变量以将其保存在您附加它的位置上方。如果您出于某种原因需要保留它以便以后使用它,您可以将变量作为一个字段(在任何方法之外,通常在类的顶部)。
  • 当我尝试添加该行时,我得到 progEvent() 未定义。但是语句“progEvent listener =”的第一部分很好,并且识别得很好。但是“新的 progEvent();”给我那个错误。
  • 查看progEvent 类的构造函数。 (请务必更改大小写......这只是一个约定,但它是如此强大,以至于阅读您的代码会令人困惑。)
  • 不一定是变量,而是对象。在你的情况下,你可能想说new progEvent(this);
【解决方案2】:

你的类应该实现 ActionListener。所以你的 prog 定义应该是“prog extends JFrame implements ActionListener”。然后实现你的 actionperformed 。 例如

public void actionPerformed(ActionEvent arg0) {
        if ("connect".equalsIgnoreCase(arg0.getActionCommand())) {
            connect();
        }

    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-06-28
    • 2014-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多