ActionListener isn#39;t Implementing(ActionListener 没有实现)
问题描述
JFrameWithPanel 不是抽象的,不会覆盖 java.awt.event.ActionListener 中的抽象方法 actionPerformed(java.awt.event.ActionEvent)公共类 JFrameWithPanel 扩展 JFrame 实现 ActionListener
JFrameWithPanel is not abstract and does not override abstract method actionPerformed(java.awt.event.ActionEvent) in java.awt.event.ActionListener public class JFrameWithPanel extends JFrame implements ActionListener
我没有收到此代码.书籍和 Java 网站告诉我这是该方法的语法,但此错误再次不断出现.
I Don't get this code. Book and Java site tells me this is the syntax for the method, but again this error shows up constantly.
import javax.swing.*;
import javax.swing.JOptionPane;
import java.awt.*;
import java.awt.event.*;
import java.lang.Math.*;
import java.lang.Integer.*;
import java.util.*;
import java.util.Random;
import java.io.*;
import java.text.*;
import java.text.DecimalFormat.*;
public class JFrameWithPanel extends JFrame implements ActionListener
{
JButton button = new JButton("Exit");
public JFrameWithPanel()
{
super("JFrame with Panel");
JComboBox packageChoice = new JComboBox();
packageChoice.addItem("A+ Certification");
packageChoice.addItem("Network+ Certification ");
packageChoice.addItem("Security+ Certifictation");
packageChoice.addItem("CIT Full Test Package");
packageChoice.addActionListener(this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel pane = new JPanel();
pane.add(button);
pane.add(packageChoice);
setContentPane(pane);
setSize(200,100);
setVisible(true);
}
}
后来
public class CreateJFrameWithPanel
{
public static void main(String[] args)
{
JFrameWithPanel panel = new JFrameWithPanel();
}
}
推荐答案
该类实现了ActionListener
接口.这意味着它必须实现一个方法:
The class implements the ActionListener
interface. This means that it must implement a method:
public void actionPerformed(ActionEvent)
但是,您发布的类定义不包含此方法,因此您会看到编译错误.要修复代码,请尝试添加以下方法:
However, the class definition you've posted does not include this method, hence why you are seeing a compilation error. To fix the code, try adding the following method:
public void actionPerformed(ActionEvent evt) {
Object obj = packageChoice.getSelectedItem();
JOptionPane.showMessageDialog(this, "You selected: " + obj);
}
这篇关于ActionListener 没有实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:ActionListener 没有实现
基础教程推荐
- 如何强制对超级方法进行多态调用? 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01