Java Swing:将对话框定位在现有窗口顶部

发布于 2024-08-10 06:21:19 字数 83 浏览 2 评论 0原文

有人可以展示简单的 Java Swing 代码/Web 资源,当单击 JFrame 的按钮时,它将弹出对话框在现有 JFrame 窗口的顶部居中对齐吗?

Can someone show simple Java Swing code/web resource that will position the popup dialog center-aligned on top of an existing JFrame window when the JFrame's button clicked?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

浮生未歇 2024-08-17 06:21:19

哦..这非常简单:

假设您有一个包含 JDialog 的 JFrame,并且您希望 JDialog(打开时)位于 JFrame 的正上方。

因此,在 JDialog 构造函数中,您应该具有类似以下内容:

public class MyDialog extends JDialog 
public MyDialog(JFrame parent) 
{
    super.setLocationRelativeTo(parent); // this will do the job
}

换句话说,将 JFrame 指针传递给您的对话框,然后调用 setLocationRelativeTo(...);方法。

Oh..it's pretty simple:

Say you have a JFrame that contains a JDialog, and you want the JDialog (when opened) to be right on top of JFrame.

So in JDialog constructor, you should have something like:

public class MyDialog extends JDialog 
public MyDialog(JFrame parent) 
{
    super.setLocationRelativeTo(parent); // this will do the job
}

In other words, pass JFrame pointer to your dialog, and call setLocationRelativeTo(...); method.

安穩 2024-08-17 06:21:19

我通常调用以下方法:

dialog.setLocationRelativeTo(parent);

Javadocs 链接

I usually call the following method:

dialog.setLocationRelativeTo(parent);

Link to Javadocs

阳光下的泡沫是彩色的 2024-08-17 06:21:19

你说的是哪种弹出对话框?如果您使用 JOptionPane 或类似的组件,请将其父组件设置为 JFrame,它将自动在 JFrame 窗口顶部居中。

JOptionPane.showMessageDialog(frame, "Hello, World!");

如果您要创建自己的 JDialog,则可以使用 JFrame.getLocation() 获取 JFrame 的位置,并使用 JFrame.getSize() 获取其大小。从这里开始,数学就非常简单了;只需计算 JFrame 的中心并减去 JDialog 的宽度/高度的一半即可获得对话框的左上角。

如果您的 JDialog 尚未渲染,JFrame.getSize() 可能会给您零大小。在这种情况下,您可以使用 JDialog.getPreferredSize() 来了解它在屏幕上渲染后的大小。

What kind of popup dialog are you talking about? If you're using a JOptionPane or something similar, set its parent component to the JFrame and it will automatically center on top of the JFrame window.

JOptionPane.showMessageDialog(frame, "Hello, World!");

If you are creating your own JDialog, you can get the JFrame's position using JFrame.getLocation() and its size using JFrame.getSize(). The math is pretty straightforward from there; just compute the center of the JFrame and subtract half the width/height of the JDialog to get your dialog's upper left corner.

If your JDialog has not been rendered yet, JFrame.getSize() might give you a zero size. In that case, you can use JDialog.getPreferredSize() to find out how big it will be once it's rendered on-screen.

夏九 2024-08-17 06:21:19

如果您想要在窗口上有一个模态且居中的对话框...

在对话框的构造函数中:

class CustomDialog extends JDialog {
    public CustomDialog(Frame owner, String title, boolean modal) {
        super(owner, title, modal);
        setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);

        ...

        setSize(150, 100);
        setLocationRelativeTo(owner);
    }
}

要显示对话框(使用按钮等):

public void actionPerformed(ActionEvent e) {
    dialog.setVisible(true);
}

If you want a modal and centered dialog on a window ...

In the dialog's constructor:

class CustomDialog extends JDialog {
    public CustomDialog(Frame owner, String title, boolean modal) {
        super(owner, title, modal);
        setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);

        ...

        setSize(150, 100);
        setLocationRelativeTo(owner);
    }
}

To display the dialog (using a button, etc.):

public void actionPerformed(ActionEvent e) {
    dialog.setVisible(true);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文