我想把一个窗口显示在屏幕中央,但是却未能如愿,窗口老是要往右下方向靠一些,我的代码如下:
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();
}
}
是哪里不对啊?
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();
}
}
是哪里不对啊?
向老大致敬!!!
setBounds也可以的。。前两个参数是窗口左上的坐标,后两个是长和宽
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 之后调用
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(,,,)
setBounds((screenSize.width-宽度)/2, (screenSize.height-高度)/2, 宽度, 高度);
setLocationRelativeTo(null);
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);
}