中开窗

发布于 2024-09-06 08:47:42 字数 63 浏览 5 评论 0原文

我正在开发一个 Java Swing 应用程序。我怎样才能做到当程序本身和任何其他窗口打开时它们出现在屏幕中央?

I'm developing a Java Swing application. How can I make it so that when the program itself and any other windows open they come up in the center of the screen?

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

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

发布评论

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

评论(5

北斗星光 2024-09-13 08:47:42
frame.setLocationRelativeTo( null );
frame.setLocationRelativeTo( null );
待天淡蓝洁白时 2024-09-13 08:47:42

frame.setLocationRelativeTo(null);让框架打开 cetner

问候,
雷汉·法鲁克

frame.setLocationRelativeTo(null); make the frame open in cetner

Regards,
Rehan Farooq

淡淡離愁欲言轉身 2024-09-13 08:47:42

如果您使用 Netbeans,这很容易。
在 JFrame 的属性窗口中,转到“代码”选项卡。有一个选项为“生成中心”。选中该选项。 JFrame 将显示在中心。

It is easy if you are using Netbeans.
In property window of JFrame go to "Code" tab.There is an option as "Generate center".Check that option. JFrame will display in center.

咆哮 2024-09-13 08:47:42

您必须使用 setLocation(x,y) 手动完成此操作。

比如:

Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation((dim.width-frameWidth)/2, (dim.height-frameHeight)/2);

应该这样做(未经测试)。

You'll have to do it by hand, using setLocation(x,y).

Something like:

Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation((dim.width-frameWidth)/2, (dim.height-frameHeight)/2);

should do it (not tested).

不疑不惑不回忆 2024-09-13 08:47:42

在多屏幕环境中手动执行此操作会给出类似的结果(静态的,因为您可能希望在实用程序类中使用它):

  public static Rectangle getScreenBounds(Component top){
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice[] gd = ge.getScreenDevices();

    if (top != null){
        Rectangle bounds = top.getBounds();
        int centerX = (int) bounds.getCenterX();
        int centerY = (int) bounds.getCenterY();

        for (GraphicsDevice device : gd){
            GraphicsConfiguration gc = device.getDefaultConfiguration();
            Rectangle r = gc.getBounds();
            if (r.contains(centerX, centerY)){
                return r;
            }
        }
    }
    return gd[0].getDefaultConfiguration().getBounds();
}

public void centerWindowOnScreen(Window windowToCenter){
    Rectangle bounds = getScreenBounds(windowToCenter);
    Point newPt = new Point(bounds.x + bounds.width / 2, bounds.y + bounds.height / 2);
    Dimension componentSize = windowToCenter.getSize();
    newPt.x -= componentSize.width / 2;
    newPt.y -= componentSize.height / 2;
    windowToCenter.setLocation(newPt);

}

至于默认按钮,它是 JDialog.getRootPane().setDefaultButton(btn),但按钮必须已添加到对话框中并且可见。

Doing it by hand in multi-screen environment gives something like this (static, 'cause you probably would want it in a utility class):

  public static Rectangle getScreenBounds(Component top){
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice[] gd = ge.getScreenDevices();

    if (top != null){
        Rectangle bounds = top.getBounds();
        int centerX = (int) bounds.getCenterX();
        int centerY = (int) bounds.getCenterY();

        for (GraphicsDevice device : gd){
            GraphicsConfiguration gc = device.getDefaultConfiguration();
            Rectangle r = gc.getBounds();
            if (r.contains(centerX, centerY)){
                return r;
            }
        }
    }
    return gd[0].getDefaultConfiguration().getBounds();
}

public void centerWindowOnScreen(Window windowToCenter){
    Rectangle bounds = getScreenBounds(windowToCenter);
    Point newPt = new Point(bounds.x + bounds.width / 2, bounds.y + bounds.height / 2);
    Dimension componentSize = windowToCenter.getSize();
    newPt.x -= componentSize.width / 2;
    newPt.y -= componentSize.height / 2;
    windowToCenter.setLocation(newPt);

}

As for default button, it's JDialog.getRootPane().setDefaultButton(btn), but button has to be already added to the dialog, and visible.

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