如何把Java程序窗口在屏幕中间显示
2个回答
展开全部
获取屏幕的长度和高度,然后在location 方法里进行设置边界是它们的一半就可以了,下面的getMidDimesion方法就是这样。
例子:
import java.awt.*;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
public class testFrame extends Frame
{
public testFrame(int width,int height)
{
this.setSize( width,height);
this.setLocation(testFrame.getMidDimesion( new Dimension(width,height)));
this.setBackground( Color.BLACK );
this.setVisible( true);
this.addWindowListener(new WindowListener()
{
public void windowOpened(WindowEvent arg0) {
// TODO Auto-generated method stub
}
public void windowClosing(WindowEvent arg0) {
// TODO Auto-generated method stub
System.exit(0);
}
public void windowClosed(WindowEvent arg0) {
// TODO Auto-generated method stub
}
public void windowIconified(WindowEvent arg0) {
// TODO Auto-generated method stub
}
...获取屏幕的长度和高度,然后在location 方法里进行设置边界是它们的一半就可以了,下面的getMidDimesion方法就是这样。
例子:
import java.awt.*;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
public class testFrame extends Frame
{
public testFrame(int width,int height)
{
this.setSize( width,height);
this.setLocation(testFrame.getMidDimesion( new Dimension(width,height)));
this.setBackground( Color.BLACK );
this.setVisible( true);
this.addWindowListener(new WindowListener()
{
public void windowOpened(WindowEvent arg0) {
// TODO Auto-generated method stub
}
public void windowClosing(WindowEvent arg0) {
// TODO Auto-generated method stub
System.exit(0);
}
public void windowClosed(WindowEvent arg0) {
// TODO Auto-generated method stub
}
public void windowIconified(WindowEvent arg0) {
// TODO Auto-generated method stub
}
public void windowDeiconified(WindowEvent arg0) {
// TODO Auto-generated method stub
}
public void windowActivated(WindowEvent arg0) {
// TODO Auto-generated method stub
}
public void windowDeactivated(WindowEvent arg0) {
// TODO Auto-generated method stub
}
});
}
public static Point getMidDimesion(Dimension d)
{
Point p = new Point();
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
p.setLocation((dim.width - d.width)/2,(dim.height - d.height)/2);
return p;
}
public static void main(String[] args)
{
new testFrame(300,200);
}
}
例子:
import java.awt.*;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
public class testFrame extends Frame
{
public testFrame(int width,int height)
{
this.setSize( width,height);
this.setLocation(testFrame.getMidDimesion( new Dimension(width,height)));
this.setBackground( Color.BLACK );
this.setVisible( true);
this.addWindowListener(new WindowListener()
{
public void windowOpened(WindowEvent arg0) {
// TODO Auto-generated method stub
}
public void windowClosing(WindowEvent arg0) {
// TODO Auto-generated method stub
System.exit(0);
}
public void windowClosed(WindowEvent arg0) {
// TODO Auto-generated method stub
}
public void windowIconified(WindowEvent arg0) {
// TODO Auto-generated method stub
}
...获取屏幕的长度和高度,然后在location 方法里进行设置边界是它们的一半就可以了,下面的getMidDimesion方法就是这样。
例子:
import java.awt.*;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
public class testFrame extends Frame
{
public testFrame(int width,int height)
{
this.setSize( width,height);
this.setLocation(testFrame.getMidDimesion( new Dimension(width,height)));
this.setBackground( Color.BLACK );
this.setVisible( true);
this.addWindowListener(new WindowListener()
{
public void windowOpened(WindowEvent arg0) {
// TODO Auto-generated method stub
}
public void windowClosing(WindowEvent arg0) {
// TODO Auto-generated method stub
System.exit(0);
}
public void windowClosed(WindowEvent arg0) {
// TODO Auto-generated method stub
}
public void windowIconified(WindowEvent arg0) {
// TODO Auto-generated method stub
}
public void windowDeiconified(WindowEvent arg0) {
// TODO Auto-generated method stub
}
public void windowActivated(WindowEvent arg0) {
// TODO Auto-generated method stub
}
public void windowDeactivated(WindowEvent arg0) {
// TODO Auto-generated method stub
}
});
}
public static Point getMidDimesion(Dimension d)
{
Point p = new Point();
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
p.setLocation((dim.width - d.width)/2,(dim.height - d.height)/2);
return p;
}
public static void main(String[] args)
{
new testFrame(300,200);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询