问一下高手,JAVA中窗口做出来后,默认是在屏幕左上角显示的,怎么让它在屏幕正中显示呢?先谢谢了啊。

解决方案 »

  1.   

    this.setLocation(x,y);
    设置坐标 就可以了吧
      

  2.   

    要把框架居中要用到java.awt.Toolkit类;
    具体代码:
    import javax.swing.*;
    import java.awt.*;
    public class CenterFrame {
      public static void main(String[] args) {
        JFrame frame = new JFrame("CenterFrame");
        frame.setSize(400, 300);    // New since JDK 1.3 to exit the program upon closing
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    // Get the dimension of the screen
        Dimension screenSize =
          Toolkit.getDefaultToolkit().getScreenSize();
        int screenWidth = screenSize.width;
        int screenHeight = screenSize.height;    // Locate the upper-left corner (x, y) of the centered frame
        int x = (screenWidth - frame.getWidth()) / 2;
        int y = (screenHeight - frame.getHeight()) / 2;    // Set the location of the frame
        frame.setLocation(x, y);
        frame.setVisible(true);
      }
    }
      

  3.   

    一句话解决:frame.setLocationRelativeTo(null);