SWT编写界面窗口时让窗口处于屏幕中间
2011-04-08 00:36
811 查看
一、使用SWT本身
Java代码
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class LayoutUtil ...{
public static void centerShell(Display display,Shell shell)...{
Rectangle displayBounds = display.getPrimaryMonitor().getBounds();
Rectangle shellBounds = shell.getBounds();
int x = displayBounds.x + (displayBounds.width - shellBounds.width)>>1;
int y = displayBounds.y + (displayBounds.height - shellBounds.height)>>1;
shell.setLocation(x, y);
}
}
直接调用LayoutUtil.centerShell(Display display,Shell shell)即可使SWT窗口处于屏幕中央,其中,shell 要显示的Shell对象。
二、借助AWT包里面获取屏幕大小的方法
Java代码
import java.awt.Toolkit;
/** *//**
* 在屏幕中间显示Shell
* @param shell 要显示的Shell对象
*/
private void centerShell(Shell shell)
{
//得到屏幕的宽度和高度
int screenHeight = Toolkit.getDefaultToolkit().getScreenSize().height;
int screenWidth = Toolkit.getDefaultToolkit().getScreenSize().width;
//得到Shell窗口的宽度和高度
int shellHeight = shell.getBounds().height;
int shellWidth = shell.getBounds().width;
//如果窗口大小超过屏幕大小,让窗口与屏幕等大
if(shellHeight > screenHeight)
shellHeight = screenHeight;
if(shellWidth > screenWidth)
shellWidth = screenWidth;
//让窗口在屏幕中间显示
shell.setLocation(( (screenWidth - shellWidth) / 2),((screenHeight - shellHeight) / 2) );
}
Java代码
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class LayoutUtil ...{
public static void centerShell(Display display,Shell shell)...{
Rectangle displayBounds = display.getPrimaryMonitor().getBounds();
Rectangle shellBounds = shell.getBounds();
int x = displayBounds.x + (displayBounds.width - shellBounds.width)>>1;
int y = displayBounds.y + (displayBounds.height - shellBounds.height)>>1;
shell.setLocation(x, y);
}
}
import org.eclipse.swt.graphics.Rectangle; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; public class LayoutUtil ...{ public static void centerShell(Display display,Shell shell)...{ Rectangle displayBounds = display.getPrimaryMonitor().getBounds(); Rectangle shellBounds = shell.getBounds(); int x = displayBounds.x + (displayBounds.width - shellBounds.width)>>1; int y = displayBounds.y + (displayBounds.height - shellBounds.height)>>1; shell.setLocation(x, y); } }
直接调用LayoutUtil.centerShell(Display display,Shell shell)即可使SWT窗口处于屏幕中央,其中,shell 要显示的Shell对象。
二、借助AWT包里面获取屏幕大小的方法
Java代码
import java.awt.Toolkit;
/** *//**
* 在屏幕中间显示Shell
* @param shell 要显示的Shell对象
*/
private void centerShell(Shell shell)
{
//得到屏幕的宽度和高度
int screenHeight = Toolkit.getDefaultToolkit().getScreenSize().height;
int screenWidth = Toolkit.getDefaultToolkit().getScreenSize().width;
//得到Shell窗口的宽度和高度
int shellHeight = shell.getBounds().height;
int shellWidth = shell.getBounds().width;
//如果窗口大小超过屏幕大小,让窗口与屏幕等大
if(shellHeight > screenHeight)
shellHeight = screenHeight;
if(shellWidth > screenWidth)
shellWidth = screenWidth;
//让窗口在屏幕中间显示
shell.setLocation(( (screenWidth - shellWidth) / 2),((screenHeight - shellHeight) / 2) );
}
相关文章推荐
- SWT编写界面窗口时让窗口处于屏幕中间
- SWT编写界面窗口时让窗口处于屏幕中间
- SWT编写界面窗口时让窗口处于屏幕中间
- SWT编写界面窗口时让窗口处于屏幕中间
- SWT编写界面窗口时让窗口处于屏幕中间
- swt shell设置窗口位于屏幕中间
- SWT/JFace设置窗口位于屏幕中间
- 将swt窗口居中(屏幕中间)将swing窗口居中
- 聊天窗口界面编写
- Java 使窗口位于屏幕中间
- java swing 中,设置弹出的窗口显示在屏幕中间
- 让弹出窗口在屏幕中间显示,没有滚动条,并且点击某行,窗口大小不变化.
- java设置程序窗口在屏幕中间
- 解决jsp底部footer一直处于界面底端的问题(自适应,随着屏幕滚动)
- 将exe窗口嵌入java的swt界面中
- WinForm 窗口出现在靠屏幕左边中间或者右边中间
- 用MFC编写的程序在别人电脑上显示时窗口界面出现问题
- 定制 SWT/RCP 界面:如何编写一个漂亮的 SWT/RCP 界面
- 一、使用JAVA_SWT编写登陆界面(不含数据库,登陆按钮未处理)
- WinFrom中实现点击关闭按钮窗口在靠屏幕左边中间缓缓收缩