在jDesktopPane的中心打开jInternalFrame

时间:2016-03-12 19:26:23

标签: java swing jinternalframe jdesktoppane

我正在使用Netbeans在Java中创建和应用程序。我有一个jFrame,其上有很少的jmenus和jdesktoppane和jinternalframes。 现在我的问题是我无法在jDesktopPane的中心设置jInternalFrame。我从stackoverflow post那里得到了一些帮助,就像这样提到了

Dimension desktopSize = desktopPane.getSize();
Dimension jInternalFrameSize = jInternalFrame.getSize();
jInternalFrame.setLocation((desktopSize.width - jInternalFrameSize.width)/2,
    (desktopSize.height- jInternalFrameSize.height)/2);

我根据我的用法改变了这个 -

Dimension desktopSize = this.getDesktopPane().getSize(); // Line No. 29
Dimension jInternalFrameSize = this.getSize();
this.setLocation((desktopSize.width - jInternalFrameSize.width)/2,
    (desktopSize.height- jInternalFrameSize.height)/2);

但是我的代码抛出了NullPointerException -

  

线程中的异常" AWT-EventQueue-0"显示java.lang.NullPointerException     at newUserReg。(newUserReg.java:29)at   smsMDI.mnuNewUserActionPerformed(smsMDI.java:278)at   smsMDI.access $ 000(smsMDI.java:16)at   smsMDI $ 2.actionPerformed(smsMDI.java:97)at   javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)     在   javax.swing.AbstractButton中的$ Handler.actionPerformed(AbstractButton.java:2341)     在   javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)     在   javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)     在javax.swing.AbstractButton.doClick(AbstractButton.java:376)at   javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:833)     在   javax.swing.plaf.basic.BasicMenuItemUI $ Handler.mouseReleased(BasicMenuItemUI.java:877)     在java.awt.Component.processMouseEvent(Component.java:6504)at   javax.swing.JComponent.processMouseEvent(JComponent.java:3321)at   java.awt.Component.processEvent(Component.java:6269)at   java.awt.Container.processEvent(Container.java:2229)at   java.awt.Component.dispatchEventImpl(Component.java:4860)at   java.awt.Container.dispatchEventImpl(Container.java:2287)at   java.awt.Component.dispatchEvent(Component.java:4686)at   java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)     在   java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)     at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)     在java.awt.Container.dispatchEventImpl(Container.java:2273)at   java.awt.Window.dispatchEventImpl(Window.java:2713)at   java.awt.Component.dispatchEvent(Component.java:4686)at   java.awt.EventQueue.dispatchEventImpl(EventQueue.java:707)at at   java.awt.EventQueue.access $ 000(EventQueue.java:101)at   java.awt.EventQueue $ 3.run(EventQueue.java:666)at   java.awt.EventQueue $ 3.run(EventQueue.java:664)at   java.security.AccessController.doPrivileged(Native Method)at   java.security.ProtectionDomain $ 1.doIntersectionPrivilege(ProtectionDomain.java:76)     在   java.security.ProtectionDomain $ 1.doIntersectionPrivilege(ProtectionDomain.java:87)     在java.awt.EventQueue $ 4.run(EventQueue.java:680)at   java.awt.EventQueue $ 4.run(EventQueue.java:678)at   java.security.AccessController.doPrivileged(Native Method)at   java.security.ProtectionDomain $ 1.doIntersectionPrivilege(ProtectionDomain.java:76)     在java.awt.EventQueue.dispatchEvent(EventQueue.java:677)at   java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)     在   java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)     在   java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)     在   java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)     在   java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)     在java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

如果有人能够解释或告诉我正确的方法,那对我来说将是一个很大的帮助。

1 个答案:

答案 0 :(得分:1)

您更改了找到的代码并添加了getDesktopPane()方法。

我猜你还没有将内部框架添加到桌面窗格,以便该方法返回null。

为什么要更改代码?您必须具有对桌面窗格的引用才能将内部框架添加到桌面窗格。所以只需使用该参考。

我建议您从How to Use Internal Frames上的Swing教程部分的工作代码开始。只需修改该代码,使框架在创建时居中。然后,您可以修改此工作示例以添加实际应用程序的逻辑。

如果这没有帮助,那么发布一个证明问题的正确SSCCE,因为我们无法猜测你到底在做什么。