-2

我想编译这个类:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Main implements ActionListener {
    private static JFrame frm = new JFrame();
    private static JLabel username = new JLabel("User:");
    private static JLabel password = new JLabel("Pass:");
    private static JTextField username_field = new JTextField();
    private static JPasswordField password_field = new JPasswordField();
    private static ButtonGroup bg = new ButtonGroup();
    private static JCheckBox sp = new JCheckBox ("Singleplayer");
    private static JCheckBox mp = new JCheckBox("Multiplayer");
    private static JButton play = new JButton("Play");
    private static JPanel p = new JPanel();

    public static void main(String[] args) {
        new Main().initFrame();
    }
    public void initFrame() {
        //Init Frame Stuff
    }
    public void actionPerformed(ActionEvent evt) {
        if (evt.getActionCommand().equals("play")) {
            //Other Stuff
        }
    }
}

正如你在上面看到的,我已经导入了 ActionListener,但是如果我编译代码,我会得到一个未知符号错误:

.\GamePanel.java:3: error: cannot find symbol
 public class GamePanel extends JPanel implements ActionListener {
 symbol: class ActionListener
 1 error

为什么我会收到此错误?

4

1 回答 1

2

对于具体的错误,你必须导入相关的包。添加

import java.awt.event.ActionListener;

在文件GamePanel.java中,就在类声明之前,即在public class GamePanel extends JPanel implements ActionListener {

于 2013-02-24T09:26:17.273 回答