我想编译这个类:
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
为什么我收到此错误?
答案 0 :(得分:2)
对于特定错误,您必须导入相关包。添加
import java.awt.event.ActionListener;
在文件GamePanel.java
中,就在类声明之前,即在public class GamePanel extends JPanel implements ActionListener {