gpt4 book ai didi

java - "The method addActionListener(ActionListener) in the type AbstractButton is not applicable for the arguments"

转载 作者:行者123 更新时间:2023-11-30 04:16:28 30 4
gpt4 key购买 nike

我是一名初学者程序员,我在操作监听器和 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。我正在使用一本书来学习它,并使用一个类作为该实验的模板。但我完全卡住了。谢谢!

最佳答案

您正在尝试添加 prog 类的实例作为 ActionListener,但您的 ActionListenerprogEvent 代替。您需要添加一个 progEvent Listener = new progEvent();,然后添加 addActionListener(progEvent)

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

关于java - "The method addActionListener(ActionListener) in the type AbstractButton is not applicable for the arguments",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18222360/

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