当前位置: 首页 > 知识库问答 >
问题:

类型AbstractButton中的方法addActionListener(ActionListener)不适用于参数(JPanel)

丁曦哲
2023-03-14
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Main implements ActionListener{
    JButton button;
    public static void main(String[] args) {
        JButton button = new JButton();
        JLabel label = new JLabel();
        label.setText("");
        label.setHorizontalAlignment(JLabel.CENTER);
        JPanel redPanel = new JPanel();
        redPanel.setBackground(Color.red);
        redPanel.setBounds(0,250,250,250);
        redPanel.setLayout(new BorderLayout());
        JPanel bluePanel = new JPanel();
        bluePanel.setBackground(Color.blue);
        bluePanel.setBounds(250,250,250,250);
        bluePanel.setLayout(new BorderLayout());
        JPanel greenPanel = new JPanel();
        greenPanel.setBackground(Color.green);
        greenPanel.setBounds(0,0,500,250);
        greenPanel.setLayout(new BorderLayout());
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(null);
        frame.setSize(750, 750);
        frame.setVisible(true);
        redPanel.add(label);
        redPanel.add(button);
        frame.add(redPanel);
        frame.add(bluePanel);
        frame.add(greenPanel);
        button.setBounds(0,250,250,250);
        button.addActionListener(redPanel);
    }
    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == button) {
            System.out.print("ahahah");
        }
    }
}

我想在主类中添加一个功能齐全的按钮

我有一个错误:类型AbstractButton中的方法addActionListener(ActionListener)不适用于参数(JPanel)

(我试着把杰帕内尔作为论据)

button.addActionListener(redPanel);

共有1个答案

戚浩淼
2023-03-14

不能注册java。awt。组件(JPanel是)作为JButtonActionListener。相反,您应该使用此处列出的适当侦听器之一,将其注册到JPanel:https://docs.oracle.com/javase/7/docs/api/java/awt/Component.html

你应该做:

button.addActionListener(new Main());

但是,如果您为ActionListener创建一个新类,并在那里实现接口,然后在main方法中使用您自己的侦听器的构造函数,那就更好了。

并始终阅读留档;)

 类似资料:
  • 我的代码中有这个错误,我是一个初学者,我不知道为什么在其他程序中,点击时执行的动作事件/动作是成功的,但在这个程序中,我使用了相同的模式,它给了我这个错误,有人能帮助我吗? 当我尝试在用户单击login时放置事件时,错误通过createfirstframe类发生 这是我的课程表 多谢

  • 我试图在一个测试中模拟。我在网上找到了一些人们也这么做的例子。 一个例子来自sping-data-elasticsearch,另一个来自一些教程。 这是我自己的例子。 不幸的是,我看到了一些警告。以下是我得到的: 网络客户。RequestHeaderUrispec是原始类型。对泛型类型WebClient的引用。RequestHeaderUrispec应该参数化 当我更改代码并向添加通配符时,我会收

  • 我已经尝试了网络上提供的所有解决方案,但都无效。请尽快提供帮助。同步错误: 生成文件“C:\Users\AH\U GL\Desktop\apps android commons\apps\Build”。格拉德尔线:149 评估项目“:app”时出现问题。 没有方法的签名:build_bacf6ncg9oj63qiyhi1id1rfi.android()适用于参数类型:(build_bacf6ncg

  • 嗨,我在GAE中的JSP代码面临一个名为“JspWriter类型中的方法print(boolean)不适用于参数(val)”的错误。 在线:

  • 我试图在Apache POI中使用自定义颜色,但遇到了一个小问题。到目前为止,我一直在使用,但作为调色板,它非常单调。因此,使用RGB格式进行颜色选择将有助于我的电子表格看起来更好。 我一直在使用这个问题的答案,因为它从理论上解决了我的问题。然而,我面临着一个非常愚蠢的问题,我真的不知道如何解决。 我收到的错误是: 类型CellStyle中的setFillForegroundColor(short

  • 我得到以下错误当截短一个虚空: 类型Stubber中的(T)不适用于参数(void)时的方法 下面是我的示例代码: 我错过了什么?