Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JMenuItem问题。我有五个。其中一个编译起来没有问题。其他的都是相同的,但是他们拒绝编译_Java_Swing_Action_Menuitem - Fatal编程技术网

Java JMenuItem问题。我有五个。其中一个编译起来没有问题。其他的都是相同的,但是他们拒绝编译

Java JMenuItem问题。我有五个。其中一个编译起来没有问题。其他的都是相同的,但是他们拒绝编译,java,swing,action,menuitem,Java,Swing,Action,Menuitem,由于某些原因,我无法在Eclipse中编译此文件。“退出”菜单项有效,其他菜单项无效。为什么呢 import java.awt.*; import java.awt.event.*; import javax.swing.*; public class GUI{ private JFrame frame; public GUI(){ makeFrame(); } //This method makes the overall GUI and adds panels, labels

由于某些原因,我无法在Eclipse中编译此文件。“退出”菜单项有效,其他菜单项无效。为什么呢

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class GUI{

private JFrame frame;

public GUI(){
    makeFrame();
}

//This method makes the overall GUI and adds panels, labels,
//buttons, and everything else to the GUI.
public void makeFrame(){
    frame = new JFrame("Tower Defense");
    Container contentPane = frame.getContentPane();

    makeMenus();

    JButton shootButton = new JButton("Shoot");
    contentPane.add(shootButton);

    frame.pack();
    frame.setVisible(true);

}

//This method makes the menu and all of the items contained
//in the menu which is then called by the makeFrame() method.
//I also add the menuItem's various ActionLiteners here.
public void makeMenus(){
    JMenuBar menubar = new JMenuBar();
    frame.setJMenuBar(menubar);

    JMenu fileMenu = new JMenu("File");
    JMenu actionsMenu = new JMenu("Actions");
    JMenu buildMenu = new JMenu("Build");

    menubar.add(fileMenu);
    menubar.add(actionsMenu);
    menubar.add(buildMenu);

    JMenuItem sellItem = new JMenuItem("Sell");
    JMenuItem quitItem = new JMenuItem("Quit");

    JMenuItem turretsItem = new JMenuItem("Turrets");
    JMenuItem minesItem = new JMenuItem("Mines");
    JMenuItem workersItem = new JMenuItem("Workers");

    quitItem.addActionListener(new QuitActionListener());
    sellItem.addActionListener(new SellActionListener());
    turretsItem.addActionListener(new TurretsActionListener());
    minesItem.addActionListener(new MinesActionListener());
    workersItem.addActionListener(new WorkersActionListener());

    fileMenu.add(quitItem);
    actionsMenu.add(sellItem);
    buildMenu.add(turretsItem);
    buildMenu.add(minesItem);
    buildMenu.add(workersItem);
}

//Main method. It creates a new GUI.
public static void main(String args []){
    GUI gui = new GUI();
}

class QuitActionListener implements ActionListener{
    public void actionPerformed(ActionEvent e){
        System.exit(0);
    }
}

class SellActionListener{

    public void actionPerformed(ActionEvent e){
        System.exit(0);
    }
}

class TurretsActionListener{

    public void actionPerformed(ActionEvent e){
        System.exit(0);
    }
}

class MinesActionListener{

    public void actionPerformed(ActionEvent e){
        System.exit(0);
    }
}

class WorkersActionListener{

    public void actionPerformed(ActionEvent e){
        System.exit(0);
    }
}

class ShootActionListener{
    public void actionPerformed(ActionEvent e){
        System.exit(0);
    }
}

}

它不是编译,因为您提供的操作侦听器(例如
SellActionListener
)没有实现
ActionListener
。方法
addActionListener
需要一个实现
ActionListener
的对象

这:

需要做到这一点:

class SellActionListener implements ActionListener{

    public void actionPerformed(ActionEvent e){
        System.exit(0);
    }
}
(与其他动作侦听器一起)

为子孙后代
这些类型的错误通常可以从编译器的反馈中找出。当eclipse说存在编译错误时,您应该能够看到错误的详细信息。我猜它可能会说“类SellActionListener没有实现接口ActionListener”(或者类似的话)。如果你用谷歌搜索该错误消息,你可能会比等待别人回答你的特定问题更快地找到答案。

或者使用testing for sources Object obj=e.getSource()将所有ActionListener放在一个类中;或者调用java.swing.Action+1@mKorbel-1表示一个大ActionListener,+1表示Action(假设每个项目一个;-)
class SellActionListener implements ActionListener{

    public void actionPerformed(ActionEvent e){
        System.exit(0);
    }
}