【问题标题】:Unresolved compilation error when using external resources使用外部资源时未解决的编译错误
【发布时间】:2018-08-11 22:14:29
【问题描述】:

我收到一个未解决的编译错误。出现这些错误:

类型中的方法addActionListener(ActionListener) AbstractButton 不适用于参数(Gui.HandlerClass) 类型中的方法addActionListener(ActionListener) AbstractButton 不适用于参数(Gui.HandlerClass) ActionListener 无法解析为 ActionEvent 类型无法解析 解析为一个类型

import java.awt.*;
import javax.swing.*;
public class Gui extends JFrame{

private JButton reg;
private JButton custom;

public Gui(){
    super("The title");
    setLayout(new FlowLayout());

    reg = new JButton("Regular Button");
    add(reg);

    Icon b = new ImageIcon(getClass().getResource("foto 1.png"));
    Icon c = new ImageIcon(getClass().getResource("foto 2.png"));
    custom = new JButton("Custom", b);
    custom.setRolloverIcon(c);
    add(custom);

    HandlerClass handler = new HandlerClass();
    reg.addActionListener(handler);
    custom.addActionListener(handler);
}

private class HandlerClass implements ActionListener{
    public void actionPerformed(ActionEvent event) {
    JOptionPane.showMessageDialog(null, String.format("%s", event.getActionCommand()));
    }
}
}

【问题讨论】:

    标签: actionlistener eventhandler actionevent


    【解决方案1】:

    避免在导入中使用 *。你的问题是 ActionListenerActionEvent 类没有被导入。因此,您必须导入它们:

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    

    另外,您使用的是什么 IDE?大多数 IDE 会发现问题并建议您解决问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-05-29
      • 2013-09-06
      • 2017-03-06
      • 1970-01-01
      • 1970-01-01
      • 2019-10-30
      • 1970-01-01
      相关资源
      最近更新 更多