如何在中心屏幕中定位表单?

Joh*_*Woo 50 java swing netbeans layout-manager

我是一名.Net开发人员,但不知何故,我的任务是在java中创建一个简单的应用程序,原因还有一些.我能够创建该应用程序但我的问题是如何在应用程序启动时将窗体置于屏幕中心?

这是我的代码:

private void formWindowActivated(java.awt.event.WindowEvent evt) 
{
        // Get the size of the screen
        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();

        // Determine the new location of the window
        int w = this.getSize().width;
        int h = this.getSize().height;
        int x = (dim.width-w)/2;
        int y = (dim.height-h)/2;

        // Move the window
        this.setLocation(x, y);
}
Run Code Online (Sandbox Code Playgroud)

上面的代码工作正常,但问题是我已经看到表单从最顶层移动到中心屏幕.我也尝试在formWindowOpened事件中添加该代码并仍然显示相同的操作.有更好的方法吗?就像.NET Application有一个CenterScreen Position.或者,如果上面的代码是正确的,我将把它放在什么事件上?

感谢您阅读本文.

Hov*_*els 116

在JFrame上调用pack后,只需设置相对于null的位置,就是这样.

例如,

  JFrame frame = new JFrame("FooRendererTest");
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.getContentPane().add(mainPanel); // or whatever...
  frame.pack();
  frame.setLocationRelativeTo(null);  // *** this will center your app ***
  frame.setVisible(true);
Run Code Online (Sandbox Code Playgroud)

  • @Xynariz:不,如果您在“setLocationrelativeTo(null)”之前先调用“pack()”,则不会,但您可以用一个小测试程序自己回答这个问题。事实上——你为什么不检查一下呢? (2认同)

Jan*_*nar 9

以下示例在屏幕上居中显示框架:

package com.zetcode;

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GraphicsEnvironment;
import java.awt.Point;
import javax.swing.JFrame;


public class CenterOnScreen extends JFrame {

    public CenterOnScreen() {

        initUI();
    }

    private void initUI() {

        setSize(250, 200);
        centerFrame();
        setTitle("Center");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    private void centerFrame() {

            Dimension windowSize = getSize();
            GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
            Point centerPoint = ge.getCenterPoint();

            int dx = centerPoint.x - windowSize.width / 2;
            int dy = centerPoint.y - windowSize.height / 2;    
            setLocation(dx, dy);
    }


    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                CenterOnScreen ex = new CenterOnScreen();
                ex.setVisible(true);
            }
        });       
    }
}
Run Code Online (Sandbox Code Playgroud)

为了在屏幕上居中框架,我们需要获得本地图形环境.从这个环境,我们确定中心点.结合框架尺寸,我们设法使框架居中.这setLocation()是将框架移动到中心位置的方法.

请注意,这实际上是这样setLocationRelativeTo(null)做的:

public void setLocationRelativeTo(Component c) {
    // target location
    int dx = 0, dy = 0;
    // target GC
    GraphicsConfiguration gc = getGraphicsConfiguration_NoClientCode();
    Rectangle gcBounds = gc.getBounds();

    Dimension windowSize = getSize();

    // search a top-level of c
    Window componentWindow = SunToolkit.getContainingWindow(c);
    if ((c == null) || (componentWindow == null)) {
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        gc = ge.getDefaultScreenDevice().getDefaultConfiguration();
        gcBounds = gc.getBounds();
        Point centerPoint = ge.getCenterPoint();
        dx = centerPoint.x - windowSize.width / 2;
        dy = centerPoint.y - windowSize.height / 2;
    }

  ...

  setLocation(dx, dy);
}
Run Code Online (Sandbox Code Playgroud)


mes*_*kin 5

改变这个:

public FrameForm() { 
    initComponents(); 
}
Run Code Online (Sandbox Code Playgroud)

对此:

public FrameForm() {
    initComponents();
    this.setLocationRelativeTo(null);
}
Run Code Online (Sandbox Code Playgroud)