gpt4 book ai didi

java - JMenu 按下抛出异常

转载 作者:行者123 更新时间:2023-12-02 00:39:21 32 4
gpt4 key购买 nike

在我的应用程序中,当我按 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)

I cant find out what's wrong with my app. Here is the code:

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");
}

最佳答案

使用

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

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

您也可以这样做,而不是让它因异常而爆炸

            if(undo.canUndo())
{
undo.undo();
}

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

关于java - JMenu 按下抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6821006/

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