简体   繁体   English

将 JDialog 置于父级之上

[英]Center JDialog over parent

I have a Java swing application with a button that produces a popup window when a certain action is performed.我有一个 Java swing 应用程序,它带有一个按钮,当执行某个操作时会产生一个弹出窗口 window。 I'd like to align the center point of the popup window with the center point of the parent window when it is rendered.我想在渲染时将弹出窗口 window 的中心点与父窗口 window 的中心点对齐。 How can I calculate the x,y coordinates to plug into setLocation() for the popup window?我如何计算 x,y 坐标以插入弹出窗口 window 的setLocation()

Just to clarify.只是为了澄清。 I do not want the behavior of setLocationRelativeTo() because that sets the top-left pixel of the popup over the center pixel of the parent frame.我不想要setLocationRelativeTo()的行为,因为它将弹出窗口的左上角像素设置在父框架的中心像素之上。 I want to set the center pixel of the popup over the center pixel of the parent frame.我想将弹出窗口的中心像素设置在父框架的中心像素上。

On the JDialog you've created you should call pack() first, then setLocationRelativeTo(parentFrame) , and then setVisible(true) .在您创建的JDialog上,您应该首先调用pack() ,然后是setLocationRelativeTo(parentFrame) ,然后setVisible(true) With that order the JDialog should appear centered on the parent frame.按照该顺序, JDialog应该显示在父框架的中心。

If you don't call pack() first, then setting the location relative to the parent doesn't work properly because the JDialog doesn't know what size it is at that point.如果您不首先调用pack() ,那么设置相对于父对象的位置将无法正常工作,因为JDialog不知道此时它的大小。 It appears to take the size as 0 by 0, which results in the "top left pixel of the popup over the center pixel of the parent" positioning mentioned in a comment to one of the other answers.它似乎将大小设置为 0 x 0,这导致在对其他答案之一的评论中提到“弹出窗口的左上角像素超过父级的中心像素”定位。

setLocationRelativeTo ..this sets the top left pixel of the popup over the center pixel of the parent. setLocationRelativeTo ..这将弹出窗口的左上角像素设置在父级的中心像素之上。 .. ..

No it does not!不,不是的!

居中对话框

Each of the 3 dialogs popped by this simple example appears to be centered as far as I can see.据我所知,这个简单示例弹出的 3 个对话框中的每一个似乎都居中。 I can only guess that the code is calling setLocationRelativeTo at the wrong time.我只能猜测代码在错误的时间调用了setLocationRelativeTo

import javax.swing.*;

class CenterTheDialog {

    CenterTheDialog() {
        for (int ii=1; ii<4; ii++) {
            JFrame f = new JFrame("Frame " + ii);
            f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

            f.setSize(400,300);
            f.setLocationByPlatform(true);
            f.setVisible(true);

            JDialog d = new JDialog(f);
            d.setSize(300,200);
            d.setLocationRelativeTo(f);
            d.setVisible(true);
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            new CenterTheDialog();
        });
    }
}

You can utilize event e to get parent window. Use getWindowAncestor and e.getSource() .您可以利用事件e获取父级 window。使用getWindowAncestore.getSource()

dialog.setLocationRelativeTo(SwingUtilities.getWindowAncestor((Component) e.getSource()))

You could try passing the coordinates of the parent window and its size to the new window and adding the coordinates + 1/2 parent frame size in each axis - 1/2 of the popups x/y to center upon center.您可以尝试将父 window 的坐标及其大小传递给新的 window 并在每个轴中添加坐标 + 1/2 父帧大小 - 弹出窗口 x/y 的 1/2 以中心为中心。

Or..If you extend you could use setLocationRelativeTo(owner)或者..如果你扩展你可以使用 setLocationRelativeTo(owner)

Hope this helps希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM