更换 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)
我已经进行了各种故障排除,但我不确定问题是什么。非常感谢您的帮助,谢谢。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
NullPointerException 是最容易诊断的问题之一,因为它告诉您源文件及其发生的行号。
在您的情况下,它是
打开您的 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
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.
1) 你不能声明
getContentPane()
在 Java5 及更高版本中没有用2) 是否没有声明任何
LayoutManager
(可能是你的情况)然后JFrame
、JDialog
或JWindow
默认情况下有BorderLayout
,所有其他 JComponent 都有默认的FlowLayout
3)如果有
BorderLayout
并且没有声明决策区域(CENTER、NORTH...),则Component
默认放置在CENTER
区域4 )用于在
JComponents
之间切换并且仅当使用BorderLayout
时,才需要调用only
并且在您的情况下仅
1) you can't declare for
getContentPane()
is useless from Java5 and higher2) is isn't there declared any
LayoutManager
(probably your case) thenJFrame
,JDialog
orJWindow
has by defaultBorderLayout
, all another JComponents have got ba defaultFlowLayout
3) if is there
BorderLayout
and isn't there declared decision Area (CENTER, NORTH...), thenComponent
is by default placed to theCENTER
area4) for switch between
JComponents
and only if is there usedBorderLayout
, is required callonly
and in you case is only