gpt4 book ai didi

java - EventQueue 和 EventDispatch 错误

转载 作者:行者123 更新时间:2023-12-02 04:45:51 25 4
gpt4 key购买 nike

我正在构建一个 Java 程序,该程序允许根据菜单栏中的径向按钮单击来更改图像。我相信大部分代码都是准确的,但是,我遇到了障碍。我怎样才能找出为什么会遇到这么多错误?

import java.awt.*;
import java.awt.event.*;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.ButtonGroup;
import javax.swing.JMenuBar;
import javax.swing.KeyStroke;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.*;

public class MenuAssignment extends JFrame implements ActionListener {

// Images, taken from the official Java Documentation
static String catString = "Cat";
static String dogString = "Dog";

// Setup label
JLabel picture;
ImageIcon image;

// Radio buttons
private JRadioButtonMenuItem catSelect;
private JRadioButtonMenuItem dogSelect;

public MenuAssignment() {

// Start with cat image
image = new ImageIcon("images/" + catString + ".gif");
picture.setIcon(image);
}

private void createMenuBar() {
JMenuBar menuBar;
JMenu menu, submenu;
JMenuItem menuItem;
JRadioButtonMenuItem rbMenuItem;

//Create the menu bar.
menuBar = new JMenuBar();

//Build the first menu.
menu = new JMenu("Animals");
menuBar.add(menu);

image = new ImageIcon("images/Dog.gif");
picture = new JLabel(image);
add(picture);

// Create group of radio buttons
ButtonGroup group = new ButtonGroup();

// Cat Button
catSelect = new JRadioButtonMenuItem("Cat");
catSelect.setActionCommand("Cat");
catSelect.setSelected(true);

// Dog Button
dogSelect = new JRadioButtonMenuItem("Dog");
dogSelect.setActionCommand("Dog");

// Listen for Option
catSelect.addActionListener(this);
dogSelect.addActionListener(this);

// return menuBar;
}

public void actionPerformed(ActionEvent e) {
image = new ImageIcon("images/" + e.getActionCommand() + ".gif");
picture.setIcon(image);
System.out.println(e.getActionCommand());
}

public static void main(String[] args) {

EventQueue.invokeLater(new Runnable() {
public void run() {
MenuAssignment start = new MenuAssignment();
start.setVisible(true);
}
});
}
}

错误:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at MenuAssignment.<init>(MenuAssignment.java:33)
at MenuAssignment$1.run(MenuAssignment.java:82)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:715)
at java.awt.EventQueue.access$400(EventQueue.java:82)
at java.awt.EventQueue$2.run(EventQueue.java:676)
at java.awt.EventQueue$2.run(EventQueue.java:674)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:86)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:685)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

最佳答案

  1. Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException由以下事实引起:picture使用前未初始化...
  2. createMenuBar从未被调用,因此屏幕上不会添加任何内容
  3. menuBar从未分配给框架 ( setJMenuBar ),因此它不会出现
  4. menucatSelectdogSelect永远不会添加到 menu
  5. 默认选择“猫”菜单项,该菜单项与屏幕上的内容不匹配

关于java - EventQueue 和 EventDispatch 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29664380/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com