JMenu被迫抛出异常

时间:2011-07-25 19:03:39

标签: java swing undo-redo jmenuitem

在我的应用程序中,当我按下JMenuBar中的编辑时,我会收到

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at sun.swing.SwingUtilities2.getLeftSideBearing(SwingUtilities2.java:241)
    at sun.swing.MenuItemLayoutHelper.getLeftExtraWidth(MenuItemLayoutHelper.java:162)
    at sun.swing.MenuItemLayoutHelper.calcExtraWidths(MenuItemLayoutHelper.java:158)
    at sun.swing.MenuItemLayoutHelper.reset(MenuItemLayoutHelper.java:148)
    at sun.swing.MenuItemLayoutHelper.(MenuItemLayoutHelper.java:107)
    at javax.swing.plaf.basic.BasicMenuItemUI.getPreferredMenuItemSize(BasicMenuItemUI.java:408)
    at javax.swing.plaf.basic.BasicMenuItemUI.getPreferredSize(BasicMenuItemUI.java:363)
    at javax.swing.JComponent.getPreferredSize(JComponent.java:1634)
    at javax.swing.BoxLayout.checkRequests(BoxLayout.java:481)
    at javax.swing.BoxLayout.preferredLayoutSize(BoxLayout.java:298)
    at javax.swing.plaf.basic.DefaultMenuLayout.preferredLayoutSize(DefaultMenuLayout.java:60)
    at java.awt.Container.preferredSize(Container.java:1605)
    at java.awt.Container.getPreferredSize(Container.java:1590)
    at javax.swing.JComponent.getPreferredSize(JComponent.java:1636)
    at javax.swing.JMenu.getPopupMenuOrigin(JMenu.java:383)
    at javax.swing.JMenu.setPopupMenuVisible(JMenu.java:349)
    at javax.swing.JPopupMenu.menuSelectionChanged(JPopupMenu.java:1433)
    at javax.swing.MenuSelectionManager.setSelectedPath(MenuSelectionManager.java:117)
    at javax.swing.plaf.basic.BasicMenuUI.appendPath(BasicMenuUI.java:223)
    at javax.swing.plaf.basic.BasicMenuUI.access$200(BasicMenuUI.java:49)
    at javax.swing.plaf.basic.BasicMenuUI$Handler.mousePressed(BasicMenuUI.java:463)
    at java.awt.Component.processMouseEvent(Component.java:6265)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
    at java.awt.Component.processEvent(Component.java:6033)
    at java.awt.Container.processEvent(Container.java:2045)
    at java.awt.Component.dispatchEventImpl(Component.java:4629)
    at java.awt.Container.dispatchEventImpl(Container.java:2103)
    at java.awt.Component.dispatchEvent(Component.java:4455)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4633)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4294)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4227)
    at java.awt.Container.dispatchEventImpl(Container.java:2089)
    at java.awt.Window.dispatchEventImpl(Window.java:2517)
    at java.awt.Component.dispatchEvent(Component.java:4455)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:649)
    at java.awt.EventQueue.access$000(EventQueue.java:96)
    at java.awt.EventQueue$1.run(EventQueue.java:608)
    at java.awt.EventQueue$1.run(EventQueue.java:606)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:105)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:116)
    at java.awt.EventQueue$2.run(EventQueue.java:622)
    at java.awt.EventQueue$2.run(EventQueue.java:620)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:105)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:619)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:275)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:200)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:190)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:185)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:177)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:138)

我无法找出我的应用程序出了什么问题。这是代码:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.undo.*;

public class Jaba extends JFrame {
    public Jaba() {
        super("Hello Swing");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(640, 480);
        setLayout(new FlowLayout());
        txt.getDocument().addUndoableEditListener(undoListener);
        add(txt);
        setJMenuBar(menuBar);
        menuBar.add(editMenu);
        editMenu.add(undoAction);
        editMenu.add(redoAction);
    }

    public static void main(String args[]) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Jaba().setVisible(true);
            }
        });
    }

    private UndoableEditListener undoListener = new UndoableEditListener() {
        @Override
        public void undoableEditHappened(UndoableEditEvent e) {
             undo.addEdit(e.getEdit());
        }        
    };
    private AbstractAction undoAction = new AbstractAction() {
        @Override
        public void actionPerformed(ActionEvent e) {
            try {
                undo.undo();
            } catch (CannotUndoException ex) {
                System.err.println("Cannon undo");
                ex.printStackTrace();
            }
        }
    };
    private AbstractAction redoAction = new AbstractAction() {
        @Override
        public void actionPerformed(ActionEvent e) {
            try {
                undo.redo();
            } catch (CannotUndoException ex) {
                System.err.println("Cannon undo");
                ex.printStackTrace();
            }
        }
    };
    private UndoManager undo = new UndoManager();
    private JTextArea txt = new JTextArea(40, 40);
    private JMenuBar menuBar = new JMenuBar();
    private JMenu editMenu = new JMenu("Edit");
}

2 个答案:

答案 0 :(得分:3)

使用

private AbstractAction redoAction = new AbstractAction("Redo"){...}

private AbstractAction redoAction = new AbstractAction("Undo"){...}

此外,您可以执行

,而不是让它以异常方式爆炸
            if(undo.canUndo())
            {
                undo.undo();
            }

            if(undo.canRedo())
            {
                undo.redo();
            }

答案 1 :(得分:0)

将String参数传递给AbstractAction构造函数解决了这个问题。谢谢Bala R。