AddabtionButton类型中的方法AddActionListener(ActionListener)不适用于参数(JPANEL)

发布于 2025-01-18 18:18:31 字数 1841 浏览 3 评论 0原文

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)

(我尝试将 JPanel 作为参数)

button.addActionListener(redPanel);
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");
        }
    }
}

I wanted to add a fully functioning button inside the main class

i had the error:The method addActionListener(ActionListener) in the type AbstractButton is not applicable for the arguments (JPanel)

(I tried putting JPanel as an argument)

button.addActionListener(redPanel);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

橘香 2025-01-25 18:18:31

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

你应该这样做:

button.addActionListener(new Main());

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

并始终阅读文档;)

You cannot register a java.awt.Component (which JPanel is) as an ActionListener of JButton. Instead you should use one of the appropriate listeners listed here, to register one onto the JPanel: https://docs.oracle.com/javase/7/docs/api/java/awt/Component.html

You should do:

button.addActionListener(new Main());

but it would be better if you create a new class for the ActionListener and implement the interface there, and then in the main method use the constructor of your own listener.

And always read the documentation ;)

葬花如无物 2025-01-25 18:18:31

你尝试过吗
redPanel.addActionListener(this);

Did you try
redPanel.addActionListener(this);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文