AddabtionButton类型中的方法AddActionListener(ActionListener)不适用于参数(JPANEL)
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您无法将
java.awt.Component
(即 JPanel)注册为JButton
的ActionListener
。相反,您应该使用此处列出的适当侦听器之一,将其注册到JPanel
上:https://docs.oracle.com/javase/7/docs/api/java/awt/Component.html你应该这样做:
但它会如果你为以下创建一个新类会更好
ActionListener
并实现那里的接口,然后在 main 方法中使用您自己的侦听器的构造函数。并始终阅读文档;)
You cannot register a
java.awt.Component
(which JPanel is) as anActionListener
ofJButton
. Instead you should use one of the appropriate listeners listed here, to register one onto theJPanel
: https://docs.oracle.com/javase/7/docs/api/java/awt/Component.htmlYou should do:
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 ;)
你尝试过吗
redPanel.addActionListener(this);
Did you try
redPanel.addActionListener(this);