gpt4 book ai didi

java - ActionListener 没有实现

转载 作者:行者123 更新时间:2023-11-29 08:17:51 24 4
gpt4 key购买 nike

JFrameWithPanel 不是抽象的,不会覆盖 java.awt.event.ActionListener 中的抽象方法 actionPerformed(java.awt.event.ActionEvent)公共(public)类 JFrameWithPanel 扩展 JFrame 实现 ActionListener

我没有得到这个代码。 Book 和 Java 站点告诉我这是该方法的语法,但此错误再次不断出现。

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界面。这意味着它必须实现一个方法:

public void actionPerformed(ActionEvent)

但是,您发布的类定义不包含此方法,因此您会看到编译错误。要修复代码,请尝试添加以下方法:

public void actionPerformed(ActionEvent evt) {
Object obj = packageChoice.getSelectedItem();
JOptionPane.showMessageDialog(this, "You selected: " + obj);
}

关于java - ActionListener 没有实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3045982/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com