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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1677318/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 17:33:49  来源:igfitidea点击:

Java Swing: positioning dialog on top of existing window

javaswingjdialog

提问by sivabudh

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?

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

回答by sivabudh

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.

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

So in JDialog constructor, you should have something like:

所以在 JDialog 构造函数中,你应该有类似的东西:

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.

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

回答by Chris B.

I usually call the following method:

我通常调用以下方法:

dialog.setLocationRelativeTo(parent);

Link to Javadocs

链接到 Javadoc

回答by rob

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 或类似的东西,请将其父组件设置为 JFrame,它会自动居中在 JFrame 窗口的顶部。

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.

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

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.

如果您的 JDialog 尚未呈现,JFrame.getSize() 可能会给您一个零大小。在这种情况下,您可以使用 JDialog.getPreferredSize() 来确定它在屏幕上呈现后的大小。

回答by Paul Vargas

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