EventQueue和EventDispatch错误

时间:2015-04-16 02:34:04

标签: java jframe jpanel

我正在构建一个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 个答案:

答案 0 :(得分:2)

  1. Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException由于picture在您使用之前未初始化而导致......
  2. 永远不会调用
  3. createMenuBar,因此屏幕上不会添加任何内容
  4. menuBar永远不会分配到框架(setJMenuBar),因此不会出现
  5. menucatSelect以及dogSelect从未添加到menu
  6. &#34; cat&#34;菜单项默认选中,与屏幕上的内容不匹配