Joh*_*ohn 2 java swing action menuitem
出于某种原因,我无法在Eclipse中编译它."退出"menuItem工作,没有其他menuItem工作.这是为什么?
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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
它没有编译,因为SellActionListener
您正在提供的动作侦听器(例如,所有禁止退出操作)未实现ActionListener
.该方法addActionListener
需要一个实现的对象ActionListener
.
这个:
class SellActionListener{
public void actionPerformed(ActionEvent e){
System.exit(0);
}
}
Run Code Online (Sandbox Code Playgroud)
需要变成这样:
class SellActionListener implements ActionListener{
public void actionPerformed(ActionEvent e){
System.exit(0);
}
}
Run Code Online (Sandbox Code Playgroud)
(以及其他动作监听器)
通常可以从编译器反馈中找出这些错误.当eclipse说存在编译错误时,您应该能够看到错误详细信息.我想它会说像"类SellActionListener没有实现接口ActionListener"(或类似的东西).如果你谷歌那个错误信息,你可能能够更快地找到答案,等待某人回答你的具体问题.
归档时间: |
|
查看次数: |
133 次 |
最近记录: |