Java 中的菜单 GUI 帮助
由于几个错误,我无法编译此代码。大多数错误都发生在 initUI() 方法下,该方法是从教程站点复制的。我该如何解决这个问题?
ActionEvent 无法解析为类型 MenuGUI.java /Misc/src 第 35 行 Java 问题
ActionListener 无法解析为类型 MenuGUI.java /Misc/src 第 34 行 Java 问题
KeyEvent 无法解析为变量 MenuGUI.java /Misc/src line 29 Java Problem
KeyEvent 无法解析为变量 MenuGUI.java /Misc/src line 32 Java 问题
AbstractButton 类型中的方法 addActionListener(ActionListener) 不适用于参数 (new ActionListener(){}) MenuGUI.java /Misc/src line 34 Java Problem
import javax.swing.*;
import java.awt.event.*;
public class MenuGUI extends JFrame{
private static final long serialVersionUID = 1L;
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable() {
public void run() {
MenuGUI ex = new MenuGUI();
ex.setVisible(true);
}
});
}
public MenuGUI()
{
initUI();
}
public void initUI()
{
JMenuBar menubar = new JMenuBar();
ImageIcon icon = new ImageIcon(getClass().getResource("exit.png"));
JMenu file = new JMenu("File");
file.setMnemonic(KeyEvent.VK_F);
JMenuItem eMenuItem = new JMenuItem("Exit", icon);
eMenuItem.setMnemonic(KeyEvent.VK_C);
eMenuItem.setToolTipText("Exit application");
eMenuItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
System.exit(0);
}
});
file.add(eMenuItem);
menubar.add(file);
setJMenuBar(menubar);
setTitle("Simple menu");
setSize(300, 200);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
I can't compile this code because of several errors. Most of the errors are under the initUI() method, which was copied from a tutorial site. How do I fix this?
ActionEvent cannot be resolved to a type MenuGUI.java /Misc/src line 35 Java Problem
ActionListener cannot be resolved to a type MenuGUI.java /Misc/src line 34 Java Problem
KeyEvent cannot be resolved to a variable MenuGUI.java /Misc/src line 29 Java Problem
KeyEvent cannot be resolved to a variable MenuGUI.java /Misc/src line 32 Java Problem
The method addActionListener(ActionListener) in the type AbstractButton is not applicable for the arguments (new ActionListener(){}) MenuGUI.java /Misc/src line 34 Java Problem
import javax.swing.*;
import java.awt.event.*;
public class MenuGUI extends JFrame{
private static final long serialVersionUID = 1L;
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable() {
public void run() {
MenuGUI ex = new MenuGUI();
ex.setVisible(true);
}
});
}
public MenuGUI()
{
initUI();
}
public void initUI()
{
JMenuBar menubar = new JMenuBar();
ImageIcon icon = new ImageIcon(getClass().getResource("exit.png"));
JMenu file = new JMenu("File");
file.setMnemonic(KeyEvent.VK_F);
JMenuItem eMenuItem = new JMenuItem("Exit", icon);
eMenuItem.setMnemonic(KeyEvent.VK_C);
eMenuItem.setToolTipText("Exit application");
eMenuItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
System.exit(0);
}
});
file.add(eMenuItem);
menubar.add(file);
setJMenuBar(menubar);
setTitle("Simple menu");
setSize(300, 200);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的问题在于 ImageIcon 的加载。由于在正确的文件夹中找不到图像,因此您将收到空指针异常。解决方案很简单:将“exit.png”图像放在与编译的类文件相同的文件夹中。
Your problem is on the loading of the ImageIcon. Since no image is found at the correct folder, you are getting a nullpointer exception. The solution is simple: place the "exit.png" image in the same folder as your compiled class file.
我可以毫无问题地编译代码,但出现运行时异常:
您的图片位于正确的目录中吗?在您的代码中,您尝试加载图标,当它不存在时,您会得到 NullPointerException
否则将其注释掉,并创建不带图标的菜单项
I can compile the code without problems, but I get a runtime exception:
Do you have the picture in the right directory? In your code you try load an icon and when it not exist, you get a NullPointerException
Otherwise comment it out, and create the menu item without an icon