Java JDesktopPane内的JInternalFrame对中工作不正常

Java JDesktopPane内的JInternalFrame对中工作不正常,java,center,jinternalframe,jdesktoppane,Java,Center,Jinternalframe,Jdesktoppane,我试图在JDesktopPane内部创建一个JInternalPane,但它没有正确居中 以下是如何创建JDesktopPane(我使用Netbeans拖放): 然后创建JInternalFrame: LoginUI login = new LoginUI(); Dimension desktopSize = desktopPane.getSize(); Dimension loginSize = login.getSize(); int width = (desktopSize.width -

我试图在JDesktopPane内部创建一个JInternalPane,但它没有正确居中

以下是如何创建JDesktopPane(我使用Netbeans拖放):

然后创建JInternalFrame:

LoginUI login = new LoginUI();
Dimension desktopSize = desktopPane.getSize();
Dimension loginSize = login.getSize();
int width = (desktopSize.width - loginSize.width) / 2;
int height = (desktopSize.height - loginSize.height) / 2;
login.setLocation(width, height);
login.setVisible(true);
desktopPane.add(login);
try {
    login.setSelected(true);
} catch (java.beans.PropertyVetoException e) {}
我还设置了JInternalFrame的首选尺寸

但是,登录框显示在桌面面板的左上角,其中大部分不可见(即桌面面板的“外部”)

我大部分时间都跟着。 我还从和获取了setLocation()信息


我在这里做错了什么,导致JInternalFrame无法确定?非常感谢您的帮助。

根据现有信息,我什么也不说,这让我相信这与您没有向我们展示的内容有关

例如,如果我将您发布的基本信息粘贴到一个可运行的示例中,则效果良好

这表明代码中有一些您没有共享的内容导致了您的问题


考虑提供一个能证明你的问题的例子。这不是一个代码转储,而是您正在做的一个示例,它突出了您所遇到的问题。这将减少混乱并获得更好的响应

Toolkit.getDefaultToolkit().getScreenSize()
是一个糟糕的选择,因为它没有考虑任务栏或dock等系统元素-您可以使用
setLocationRelativeTo
并将其传递
null
我猜
LoginUI
没有预定义的大小,根据我写的第一篇帖子,它当前的大小为
0x0
@MadProgrammer链接的JInternalFrame没有setLocationRelativeTo(null)实现。不管怎样,我测试了一下,发现了一个错误。此外,LoginUI(我的JInternalFrame)确实有一个预定义的大小,正如我在文章中提到的。我可以看到框架的一部分,它只是JDesktopPanel的一部分。例如,如果我将位置设置为setLocation(200300),那么框架在JDesktopPane中是完全可见的。“根据我链接JInternalFrame的第一篇文章,它没有setLocationRelativeTo(null)实现”-我不是在谈论
JInternalFrame
,而是在谈论
JFrame
*“确实有我在帖子中提到的预定义大小”-我可以看到您调用
getSize
,但我看不到您调用
setSize
(或
pack
)的任何地方,因为我将您的代码应用于一个示例,谢谢。我用了一种非常奇怪的方式来设置我的。使用您的示例向我展示了如何更好地实现它。有时,拖放方法可能比预期的更复杂。
LoginUI login = new LoginUI();
Dimension desktopSize = desktopPane.getSize();
Dimension loginSize = login.getSize();
int width = (desktopSize.width - loginSize.width) / 2;
int height = (desktopSize.height - loginSize.height) / 2;
login.setLocation(width, height);
login.setVisible(true);
desktopPane.add(login);
try {
    login.setSelected(true);
} catch (java.beans.PropertyVetoException e) {}
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JDesktopPane dp = new JDesktopPane();
                dp.setPreferredSize(new Dimension(200, 200));

                JInternalFrame iFrame = new JInternalFrame("Test", true, true, true, true);
                iFrame.getContentPane().setPreferredSize(new Dimension(100, 100));
                iFrame.pack();
                iFrame.setVisible(true);

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(dp);
                frame.pack();
                frame.setLocationRelativeTo(null);

                dp.add(iFrame);

                Dimension desktopSize = dp.getSize();
                Dimension loginSize = iFrame.getSize();

                int x = (desktopSize.width - loginSize.width) / 2;
                int y = (desktopSize.height - loginSize.height) / 2;
                iFrame.setLocation(x, y);

                frame.setVisible(true);
            }
        });
    }

}