JMenuItem 问题。我有五个。其中之一编译没有问题。其他的都是一样的,但他们拒绝编译
由于某种原因,我无法在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它无法编译,因为您提供的操作侦听器(例如
SellActionListener
)(除了退出操作之外的所有操作)都没有实现ActionListener
。addActionListener
方法需要一个实现ActionListener
的对象。这:
需要变成这样:(
与其他动作侦听器一起)
为了后代
这些类型的错误通常可以从编译器反馈中找出。当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 implementingActionListener
. The methodaddActionListener
expects an object that implementsActionListener
.This:
Needs to become this:
(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.