JMenuItem 问题。我有五个。其中之一编译没有问题。其他的都是一样的,但他们拒绝编译

发布于 2024-11-14 22:50:54 字数 2596 浏览 3 评论 0原文

由于某种原因,我无法在 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);
    }
}

}

for some reason, I am unable to compile this in Eclipse. The "quit" menuItem works and no other menuItem works. Why is that?

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);
    }
}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不气馁 2024-11-21 22:50:54

它无法编译,因为您提供的操作侦听器(例如 SellActionListener)(除了退出操作之外的所有操作)都没有实现 ActionListeneraddActionListener 方法需要一个实现 ActionListener 的对象。

这:

class SellActionListener{

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

需要变成这样:(

class SellActionListener implements ActionListener{

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

与其他动作侦听器一起)

为了后代

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

It is not compiling because the actions listeners (like SellActionListener, for example) that you are providing (all bar the quit action) are not implementing ActionListener. The method addActionListener expects an object that implements ActionListener.

This:

class SellActionListener{

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

Needs to become this:

class SellActionListener implements ActionListener{

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

(along with the other action listeners)

For posterity

These sort of errors can usually be figured out from the compiler feedback. When eclipse said that there were compile errors, you should be able to see the error details. I guess it would say something like "class SellActionListener does not implement the interface ActionListener" (or something along those lines). If you google that error message you might be able to find the answer faster that waiting for someone to answer your specific question.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文