我想把一个窗口显示在屏幕中央,但是却未能如愿,窗口老是要往右下方向靠一些,我的代码如下:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Login extends JFrame{
JLabel JLhost=new JLabel("SQL Server(S):");
JLabel JLport=new JLabel("Port(P):");
JLabel JLuser=new JLabel("用户名(U):");
JLabel JLpwd=new JLabel("密码(P):");

JTextField JFhost=new JTextField(10);
JTextField JFport=new JTextField(10);
JTextField JFuser=new JTextField("sa",10);
JPasswordField JPasspwd=new JPasswordField(10);

JButton JBOk=new JButton("确定");
JButton JBCancel=new JButton("取消");

JPanel JPmain=new JPanel(new GridLayout(5,1));
JPanel JPhost=new JPanel(new GridLayout(1,2));
JPanel JPport=new JPanel(new GridLayout(1,2));
JPanel JPuser=new JPanel(new GridLayout(1,2));
JPanel JPpwd=new JPanel(new GridLayout(1,2));
JPanel JPbutton=new JPanel(new GridLayout(1,2));
public Login(){
init();
}
public void init(){
setThisLayout();
setThislocation();
}
public void setThisLayout(){
this.getContentPane().add(JPmain);
JPmain.add(JPhost);
JPmain.add(JPport);
JPmain.add(JPuser);
JPmain.add(JPpwd);
JPmain.add(JPbutton);

JPhost.add(JLhost);
JPhost.add(JFhost);

JPport.add(JLport);
JPport.add(JFport);

JPuser.add(JLuser);
JPuser.add(JFuser);

JPpwd.add(JLpwd);
JPpwd.add(JPasspwd);

JPbutton.add(JBOk);
JPbutton.add(JBCancel);

setSize(250,150);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public void setThislocation(){
Dimension dimension= Toolkit.getDefaultToolkit().getScreenSize();
int screenheight=dimension.height;
int screenwidth=dimension.width;
this.setLocation((screenwidth-JFrame.WIDTH)/2,(screenheight-JFrame.HEIGHT)/2);
}
public static void main(String[] args){
new Login();
}
}
是哪里不对啊?

解决方案 »

  1.   

    I am sorry, my company has just reset our systems, so now I can only communicate with you in English. I found the problem. Your method setThislocation is not right. The statement      this.setLocation((screenwidth-JFrame.WIDTH)/2,(screenheight-JFrame.HEIGHT)/2);  should be this.setLocation((screenwidth-this.getWidth())/2,(screenheight-this.getHeigth())/2);
      

  2.   

    一个简单实用的方法:this.setLocationRelativeTo(null);
      

  3.   

    to mq612(五斗米)老大真负责啊,总是耐心的回答大家的问题呵呵
    向老大致敬!!!
      

  4.   

    可以不用setLocation()方法,用setBounds(screenwidth/4,screenheight/4,screenwidth/2,screenheight/2)
      

  5.   

    可以先取得屏幕大大小然后用setloction来定义窗口左上点的坐标
    setBounds也可以的。。前两个参数是窗口左上的坐标,后两个是长和宽
      

  6.   

    import java.awt.Component;
    import java.awt.Dimension;
    import java.awt.Toolkit;
    /**
     * Size util
     * 
     * @author ufobizsoft
     *
     */
    public final class JTPSize { /** Screen size */
    private static Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

    /**
     * Return x value in the center of screen
     * 
     * @param width frame's width
     * @return x value in the center of screen
     */
    public static int getStartX(int width){
    double x = (screenSize.getWidth() - width) / 2;
    return (int)x;
    }

    /**
     * Return y value in the center of screen
     * 
     * @param height frame's height
     * @return y value in the center of screen
     */
    public static int getStartY(int height){
    double y = (screenSize.getHeight() - height) / 2;
    return (int)y;
    }

    /**
     * Return start point in the center of sceen
     * 
     * @param width frame's width
     * @param height frame's height
     * @return start point in the center of sceen
     */
    public static Dimension getStartPoint(int width, int height){
    return new Dimension(getStartX(width), getStartY(height));
    }

    /**
     * Locate the component in the center of screen
     * 
     * @param component component to be located
     */
    public static void locateCenter(Component component){
    component.setLocation(getStartX(component.getWidth()), getStartY(component.getHeight()));
    }
    }做了个类, 所有控件按如下调用JPanel panel = new JPanel();
    panel.setSize(300, 400);
    JTPSize.locateCenter(panel);但必须在设置完size 之后调用
      

  7.   

    方法多了,一般的做法是
    Toolkit kit = Toolkit.getDefaultToolkit();
    Dimension screenSize = kit.getScreenSize();
    int height = screenSize.height;
    int width = screenSize.width;

    //居中显示
    setSize(width/2, height/2);
    setLocation(width/4, height/4);//----这里也可以直接用setBounds(,,,)
      

  8.   

    java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
    setBounds((screenSize.width-宽度)/2, (screenSize.height-高度)/2, 宽度, 高度);
      

  9.   

    最简单的方法就是
    setLocationRelativeTo(null);
      

  10.   

    修改这个方法
    public void setThislocation(){
        //Center the window
         Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
         Dimension frameSize = this.getSize();
         if (frameSize.height > screenSize.height) {
           frameSize.height = screenSize.height;
         }
         if (frameSize.width > screenSize.width) {
           frameSize.width = screenSize.width;
         }
         this.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
         this.setVisible(true);
    }
      

  11.   

    有没有必要搞那么多的Jpanel啊