如何在Java中居中窗口?

And*_*wan 104 java user-interface swing awt

什么是最简单的中心方式java.awt.Window,例如a JFrame或a JDialog

And*_*wan 234

这个链接

如果您使用的是Java 1.4或更高版本,则可以在对话框,框架或窗口上使用简单方法setLocationRelativeTo(null)来使其居中.

  • 正如@kleopatra在另一个答案中所说,必须在pack()之后调用setLocationRelativeTo(null)才能工作. (6认同)
  • 如下所述,必须在调用pack()或setSize()之后调用setLocationRelativeTo(null). (3认同)
  • @Eusebius Odd,我遵循了一个教程,该教程让我在 `pack()` 之前设置它,并将框架的左上角放在我的屏幕中央。将线移到`pack()`下方后,它就正确居中了。 (2认同)
  • Well pack()根据内容和布局设置正确的大小,除非你知道它的大小,否则你不能将某些东西放在中心位置,所以教程让你在居中后打包它确实很奇怪. (2认同)

Dón*_*nal 64

这应该适用于所有版本的Java

public static void centreWindow(Window frame) {
    Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
    int x = (int) ((dimension.getWidth() - frame.getWidth()) / 2);
    int y = (int) ((dimension.getHeight() - frame.getHeight()) / 2);
    frame.setLocation(x, y);
}
Run Code Online (Sandbox Code Playgroud)


Kev*_*Day 25

请注意,setLocationRelativeTo(null)和Tookit.getDefaultToolkit().getScreenSize()技术仅适用于主监视器.如果您处于多监视器环境中,则可能需要在执行此类计算之前获取有关窗口所在的特定监视器的信息.

有时重要,有时不...

有关如何获取此信息的详细信息,请参阅GraphicsEnvironment javadocs.


Dzm*_*ich 23

在使用setSize(x,y)或使用pack()之后应该调用setLocationRelativeTo(null).


Pet*_*abo 16

在Linux上的代码

    setLocationRelativeTo(null)
Run Code Online (Sandbox Code Playgroud)

每次我启动它时,在多显示环境中将窗口置于随机位置.和代码

    setLocation((Toolkit.getDefaultToolkit().getScreenSize().width  - getSize().width) / 2, (Toolkit.getDefaultToolkit().getScreenSize().height - getSize().height) / 2);
Run Code Online (Sandbox Code Playgroud)

将窗口"切"成两半,将其放置在我的两个显示器之间的确切中心.我使用以下方法来集中它:

private void setWindowPosition(JFrame window, int screen)
{        
    GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice[] allDevices = env.getScreenDevices();
    int topLeftX, topLeftY, screenX, screenY, windowPosX, windowPosY;

    if (screen < allDevices.length && screen > -1)
    {
        topLeftX = allDevices[screen].getDefaultConfiguration().getBounds().x;
        topLeftY = allDevices[screen].getDefaultConfiguration().getBounds().y;

        screenX  = allDevices[screen].getDefaultConfiguration().getBounds().width;
        screenY  = allDevices[screen].getDefaultConfiguration().getBounds().height;
    }
    else
    {
        topLeftX = allDevices[0].getDefaultConfiguration().getBounds().x;
        topLeftY = allDevices[0].getDefaultConfiguration().getBounds().y;

        screenX  = allDevices[0].getDefaultConfiguration().getBounds().width;
        screenY  = allDevices[0].getDefaultConfiguration().getBounds().height;
    }

    windowPosX = ((screenX - window.getWidth())  / 2) + topLeftX;
    windowPosY = ((screenY - window.getHeight()) / 2) + topLeftY;

    window.setLocation(windowPosX, windowPosY);
}
Run Code Online (Sandbox Code Playgroud)

使窗口显示在第一个显示屏的中央.这可能不是最简单的解决方案.

适用于Linux,Windows和Mac.


The*_*ker 6

我终于使用 Swing GUI Forms 使这堆代码在 NetBeans 中工作,以便将主 jFrame 居中:

package my.SampleUIdemo;
import java.awt.*;

public class classSampleUIdemo extends javax.swing.JFrame {
    /// 
    public classSampleUIdemo() {
        initComponents();
        CenteredFrame(this);  // <--- Here ya go.
    }
    // ...
    // void main() and other public method declarations here...

    ///  modular approach
    public void CenteredFrame(javax.swing.JFrame objFrame){
        Dimension objDimension = Toolkit.getDefaultToolkit().getScreenSize();
        int iCoordX = (objDimension.width - objFrame.getWidth()) / 2;
        int iCoordY = (objDimension.height - objFrame.getHeight()) / 2;
        objFrame.setLocation(iCoordX, iCoordY); 
    } 

}
Run Code Online (Sandbox Code Playgroud)

或者

package my.SampleUIdemo;
import java.awt.*;

public class classSampleUIdemo extends javax.swing.JFrame {
        /// 
        public classSampleUIdemo() {
            initComponents(); 
            //------>> Insert your code here to center main jFrame.
            Dimension objDimension = Toolkit.getDefaultToolkit().getScreenSize();
            int iCoordX = (objDimension.width - this.getWidth()) / 2;
            int iCoordY = (objDimension.height - this.getHeight()) / 2;
            this.setLocation(iCoordX, iCoordY); 
            //------>> 
        } 
        // ...
        // void main() and other public method declarations here...

}
Run Code Online (Sandbox Code Playgroud)

或者

    package my.SampleUIdemo;
    import java.awt.*;
    public class classSampleUIdemo extends javax.swing.JFrame {
         /// 
         public classSampleUIdemo() {
             initComponents();
             this.setLocationRelativeTo(null);  // <<--- plain and simple
         }
         // ...
         // void main() and other public method declarations here...
   }
Run Code Online (Sandbox Code Playgroud)