更换 JPanel 时遇到错误

发布于 2024-12-06 10:49:23 字数 3309 浏览 1 评论 0原文

我正在创建一个菜单,其中包含多个面板,当用户通过单击按钮进行导航时,这些面板将被删除/添加。

在尝试了各种方法之后,我找到了一种对我来说最有意义但给我带来错误的方法。

我产生错误的“解决方案”:

public void actionPerformed (ActionEvent evt) {
    Object source = evt.getSource();
    if (source == jButton1) {
        changePanels(jPanel1, singlePanel1);
    }

}

public void changePanels (JPanel a, JPanel b) {
    getContentPane().removeAll();
    getContentPane().add(b);
    validate();
    repaint();
}

由于某种原因,它产生了这些错误:

$Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at java.awt.Container.addImpl(Container.java:1041)
at java.awt.Container.add(Container.java:365)
at phantasma.OriginalFrame.changePanels(OriginalFrame.java:156)
at phantasma.OriginalFrame.actionPerformed(OriginalFrame.java:149)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6288)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
at java.awt.Component.processEvent(Component.java:6053)
at java.awt.Container.processEvent(Container.java:2041)
at java.awt.Component.dispatchEventImpl(Component.java:4651)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Component.dispatchEvent(Component.java:4481)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4577)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
at java.awt.Container.dispatchEventImpl(Container.java:2085)
at java.awt.Window.dispatchEventImpl(Window.java:2478)
at java.awt.Component.dispatchEvent(Component.java:4481)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:643)
at java.awt.EventQueue.access$000(EventQueue.java:84)
at java.awt.EventQueue$1.run(EventQueue.java:602)
at java.awt.EventQueue$1.run(EventQueue.java:600)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98)
at java.awt.EventQueue$2.run(EventQueue.java:616)
at java.awt.EventQueue$2.run(EventQueue.java:614)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:613)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

我已经进行了各种故障排除,但我不确定问题是什么。非常感谢您的帮助,谢谢。

I'm in the process of creating a menu with several panels that are removed/added as the user navigates by clicking on buttons.

After trying various things, I came to one that made the most sense to me but gives me errors.

My error-producing "solution":

public void actionPerformed (ActionEvent evt) {
    Object source = evt.getSource();
    if (source == jButton1) {
        changePanels(jPanel1, singlePanel1);
    }

}

public void changePanels (JPanel a, JPanel b) {
    getContentPane().removeAll();
    getContentPane().add(b);
    validate();
    repaint();
}

For some reason, it produces these errors:

$Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at java.awt.Container.addImpl(Container.java:1041)
at java.awt.Container.add(Container.java:365)
at phantasma.OriginalFrame.changePanels(OriginalFrame.java:156)
at phantasma.OriginalFrame.actionPerformed(OriginalFrame.java:149)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6288)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
at java.awt.Component.processEvent(Component.java:6053)
at java.awt.Container.processEvent(Container.java:2041)
at java.awt.Component.dispatchEventImpl(Component.java:4651)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Component.dispatchEvent(Component.java:4481)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4577)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
at java.awt.Container.dispatchEventImpl(Container.java:2085)
at java.awt.Window.dispatchEventImpl(Window.java:2478)
at java.awt.Component.dispatchEvent(Component.java:4481)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:643)
at java.awt.EventQueue.access$000(EventQueue.java:84)
at java.awt.EventQueue$1.run(EventQueue.java:602)
at java.awt.EventQueue$1.run(EventQueue.java:600)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98)
at java.awt.EventQueue$2.run(EventQueue.java:616)
at java.awt.EventQueue$2.run(EventQueue.java:614)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:613)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

I've run through various troubleshooting, but I'm not sure what the issue is. Help is greatly appreciated, thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陌上芳菲 2024-12-13 10:49:23

NullPointerException 是最容易诊断的问题之一,因为它告诉您源文件及其发生的行号。

在您的情况下,它是

phantasma.OriginalFrame.changePanels(OriginalFrame.java:156)

打开您的 IDE,转到该行,然后查找取消引用的对象引用。其中之一为空;你以为不是。

如果这还不够,请设置断点并导航到调试器中的该位置。它会准确地告诉你什么是 null。

一旦弄清楚这一点,请考虑为什么该对象未正确初始化并修复它。

NullPointerException is one of the easiest problems to diagnose, because it tells you the source file and the line number at which it occurs.

In your case, it's

phantasma.OriginalFrame.changePanels(OriginalFrame.java:156)

Open your IDE, go to that line, and look for dereferenced object references. One of them is null; you assumed it wasn't.

If that's not enough, set a breakpoint and navigate to that spot in the debugger. It'll tell you exactly what's null.

Once you figure that out, think about why that object isn't initialized properly and fix it.

陌生 2024-12-13 10:49:23

1) 你不能声明 getContentPane() 在 Java5 及更高版本中没有用

2) 是否没有声明任何 LayoutManager (可能是你的情况)然后 JFrameJDialogJWindow 默认情况下有 BorderLayout,所有其他 JComponent 都有默认的 FlowLayout

3)如果有BorderLayout 并且没有声明决策区域(CENTER、NORTH...),则 Component 默认放置在 CENTER 区域

4 )用于在 JComponents 之间切换并且仅当使用 BorderLayout 时,才需要调用 only

myContainer.add(someComponent, BorderLayout.DECISION_AREA);
revalidate();
repaint();

并且在您的情况下仅

add(someComponent);
revalidate();
repaint();

1) you can't declare for getContentPane() is useless from Java5 and higher

2) is isn't there declared any LayoutManager (probably your case) then JFrame , JDialog or JWindow has by default BorderLayout, all another JComponents have got ba default FlowLayout

3) if is there BorderLayout and isn't there declared decision Area (CENTER, NORTH...), then Component is by default placed to the CENTER area

4) for switch between JComponents and only if is there used BorderLayout, is required call only

myContainer.add(someComponent, BorderLayout.DECISION_AREA);
revalidate();
repaint();

and in you case is only

add(someComponent);
revalidate();
repaint();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文