替换JPanel时遇到错误

时间:2011-09-24 16:32:49

标签: java swing replace jframe jpanel

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

在尝试了各种各样的事情之后,我找到了一个对我最有意义但却给我错误的文章。

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

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)

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

2 个答案:

答案 0 :(得分:2)

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

在你的情况下,它是

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

打开IDE,转到该行,然后查找解除引用的对象引用。其中一个是null;你以为它不是。

如果这还不够,请设置断点并导航到调试器中的那个位置。它会告诉你究竟什么是空的。

一旦弄明白,想一想为什么没有正确初始化该对象并修复它。

答案 1 :(得分:1)

1)您无法声明getContentPane()对Java5及更高版本

无效

2)是没有声明任何LayoutManager(可能是您的情况),然后JFrameJDialogJWindow默认BorderLayout,所有另一个JComponents的默认值为FlowLayout

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

4)要在JComponents之间切换,只有在使用BorderLayout时,才需要致电only

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

在你的情况下只是

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