添加Swing菜单栏时出现一堆错误

时间:2019-09-04 00:23:14

标签: java macos swing netbeans

我正在使用NetBeans创建Java应用程序,因此我利用了GUI Builder,但是每当尝试添加一个摆动菜单栏时,都会出现很多错误。你能帮我这个忙吗?

在GUI生成器的菜单“ Swing Menus”下,将“菜单栏”拖动到JFrame的顶部(我有足够的空间来容纳所有内容),并且在GUI Builder中看起来不错(查看下面的链接)截图)。

GUI Builder菜单栏选择 enter image description here

添加菜单栏之前,在GUI Builder中使用

JFrame enter image description here

添加菜单栏后,在GUI Builder中添加

JFrame enter image description here

添加菜单栏后运行

JFrame应用程序 enter image description here

运行应用程序后的JFrame,然后添加菜单栏 enter image description here

这些都是我在菜单栏中运行应用程序时遇到的所有错误

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at java.desktop/com.apple.laf.AquaMenuPainter.paintMenuBarBackground(AquaMenuPainter.java:149)
    at java.desktop/com.apple.laf.AquaMenuBarUI.paint(AquaMenuBarUI.java:82)
    at java.desktop/javax.swing.plaf.ComponentUI.update(ComponentUI.java:161)
    at java.desktop/javax.swing.JComponent.paintComponent(JComponent.java:797)
    at java.desktop/javax.swing.JComponent.paint(JComponent.java:1074)
    at java.desktop/javax.swing.JComponent.paintChildren(JComponent.java:907)
    at java.desktop/javax.swing.JComponent.paint(JComponent.java:1083)
    at java.desktop/javax.swing.JLayeredPane.paint(JLayeredPane.java:590)
    at java.desktop/javax.swing.JComponent.paintChildren(JComponent.java:907)
    at java.desktop/javax.swing.JComponent.paintToOffscreen(JComponent.java:5262)
    at java.desktop/javax.swing.RepaintManager$PaintManager.paintDoubleBufferedImpl(RepaintManager.java:1643)
    at java.desktop/javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(RepaintManager.java:1618)
    at java.desktop/javax.swing.RepaintManager$PaintManager.paint(RepaintManager.java:1556)
    at java.desktop/javax.swing.RepaintManager.paint(RepaintManager.java:1323)
    at java.desktop/javax.swing.JComponent.paint(JComponent.java:1060)
    at java.desktop/java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:39)
    at java.desktop/sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:78)
    at java.desktop/sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:115)
    at java.desktop/java.awt.Container.paint(Container.java:2002)
    at java.desktop/java.awt.Window.paint(Window.java:3926)
    at java.desktop/javax.swing.RepaintManager$4.run(RepaintManager.java:876)
    at java.desktop/javax.swing.RepaintManager$4.run(RepaintManager.java:848)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:389)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
    at java.desktop/javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:848)
    at java.desktop/javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:823)
    at java.desktop/javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:772)
    at java.desktop/javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1884)
    at java.desktop/java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:313)
    at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:770)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:389)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
    at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:740)
    at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

有人知道为什么会这样吗?

1 个答案:

答案 0 :(得分:0)

我搜索了一下,发现(基于异常)它是由外观引起的。不确定为什么只有在添加菜单栏时它才会失败...

请尝试将main方法中的外观设置为如下所示。对于我来说,netbeans总是生成使用nimbus l&f的主要方法。

public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
        } catch (Exception e) {
            e.printStackTrace();
        }

        ...//all other stuff which was there earlier...e.g. java.awt.EventQueue.invokeLater(new Runnable()...

    }

或者代替Nimbus,您可以尝试:

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());