【问题标题】:Centering a JInternalFrame inside JDesktopPane not working correctly在 JDesktopPane 中居中 JInternalFrame 无法正常工作
【发布时间】:2018-02-16 22:56:17
【问题描述】:

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

JDesktopPane 的创建方式如下(我使用的是 Netbeans 拖放):

JDesktopPane desktopPane;
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
setBounds(0, 0, screenSize.width / 2, screenSize.height / 2);
desktopPane = new JDesktopPane();
setContentPane(desktopPane);

然后我创建 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 的首选大小。

但是,登录框架出现在 desktopPane 的左上角,其中大部分是不可见的(即在 desktopPane 的“外部”)。

我主要关注this Java documentation。 我还从this postthis post 获得了 setLocation() 信息。

我在这里做错了什么导致 JInternalFrame 不集中?任何帮助表示赞赏。

【问题讨论】:

  • Toolkit.getDefaultToolkit().getScreenSize() 是一个糟糕的选择,因为它没有考虑任务栏或停靠栏等系统元素 - 您可以使用 setLocationRelativeTo 并传递它 null
  • 我的猜测是LoginUI 没有预定义的大小,目前的大小为0x0
  • @MadProgrammer - 根据我链接的第一篇文章,JInternalFrame 没有 setLocationRelativeTo(null) 实现。无论如何我测试了它并得到了一个错误。此外,正如我在帖子中指出的那样,LoginUI(我的 JInternalFrame)确实具有预定义的大小。我可以看到框架的一部分,它只是在 JDesktopPanel 之外的一部分。例如,如果我将位置设置为 setLocation(200,300),则框架在 JDesktopPane 中完全可见。
  • “根据我链接 JInternalFrame 的第一篇文章没有 setLocationRelativeTo(null) 实现” - 我不是在谈论 JInternalFrame,我在谈论 @ 987654332@。 *“确实有我在帖子中提到的预定义大小” - 我可以看到你打电话给getSize,但我没有看到你打电话给setSize(或pack)的任何地方,因为我们将您的代码应用到它工作的示例中

标签: java center jinternalframe jdesktoppane


【解决方案1】:

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

例如,如果我将您发布的基本信息粘贴到可运行的示例中,它就可以正常工作

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);
            }
        });
    }

}

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

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

【讨论】:

  • 谢谢。我以一种非常奇怪的方式设置了我的。玩你的例子向我展示了如何比我更好地实现它。有时拖放方法可能比预期的更复杂。
猜你喜欢
  • 2014-12-14
  • 2013-08-28
  • 2021-10-31
  • 2011-06-03
  • 2011-12-29
  • 2012-11-29
  • 1970-01-01
  • 1970-01-01
  • 2011-10-15
相关资源
最近更新 更多