java abstract window toolkit(awt)的window.setlocation函数为开发人员提供了一个条理清晰的方法来解决在用户屏幕上定位一个窗口的问题。但用这个方法就必须考虑精确的象素点,这就很麻烦,而用一个类似于java.awt.borderlayout的系统就要好的多。
在borderlayout中,各部分都是由下面这几个区域函数之一定位的:
borderlayout.north
borderlayout.south
borderlayout.center
borderlayout.west
borderlayout.east
下面的代码用几乎相同的方法在屏幕上定位了一个窗口。它首先确定了五个固定的位置,然后用屏幕大小和窗口大小来定位一个窗口。
package com.generationjava.awt;
import java.awt.dimension;
import java.awt.window;
public final class windowutilities {
// ints used so a case-switch statement can be used.
static public final int top = 0;
static public final int bottom = 1;
static public final int center = 2;
static public final int left = 3;
static public final int right = 4;
/**
* position the given window on the screen at the given location,
*
* @param w window to position
* @param x int horizontal position
* @param y int vertical position
*/
static public void positionwindowonscreen(window w, int x, int y) {
dimension scrn = w.gettoolkit().getscreensize();
dimension win = w.getsize();
int nx = 0;
int ny = 0;
switch(x) {
case left: nx = 0; break;
case centre: nx = (int)((scrn.width-win.width)/2); break;
case right: nx = (int)(scrn.width-win.width); break;
}
switch(y) {
case top: ny = 0; break;
case centre: ny = (int)((scrn.height-win.height)/2); break;
case bottom: ny = (int)(scrn.height-win.height); break;
}
w.setlocation(nx,ny);
}
}
要确定一个窗口的右下角的位置,就要执行下面的代码:
window window = ....
windowutilities.positionwindowonscreen(window,_
windowutilities.right, windowutilities.bottom);
该方法还可以进行扩展。你可以增加一个简单的centerwindowonscreen函数,该函数调用了positionwindowonscreen函数。例如:
static public final void centerwindowonscreen(window w)
{
positionwindowonscreen(w, center, center);
}
这两个函数简化了在awt和swing中所做的工作,使开发人员可以将更多的时间用在开发用户界面上。(zdnet china)
在borderlayout中,各部分都是由下面这几个区域函数之一定位的:
borderlayout.north
borderlayout.south
borderlayout.center
borderlayout.west
borderlayout.east
下面的代码用几乎相同的方法在屏幕上定位了一个窗口。它首先确定了五个固定的位置,然后用屏幕大小和窗口大小来定位一个窗口。
package com.generationjava.awt;
import java.awt.dimension;
import java.awt.window;
public final class windowutilities {
// ints used so a case-switch statement can be used.
static public final int top = 0;
static public final int bottom = 1;
static public final int center = 2;
static public final int left = 3;
static public final int right = 4;
/**
* position the given window on the screen at the given location,
*
* @param w window to position
* @param x int horizontal position
* @param y int vertical position
*/
static public void positionwindowonscreen(window w, int x, int y) {
dimension scrn = w.gettoolkit().getscreensize();
dimension win = w.getsize();
int nx = 0;
int ny = 0;
switch(x) {
case left: nx = 0; break;
case centre: nx = (int)((scrn.width-win.width)/2); break;
case right: nx = (int)(scrn.width-win.width); break;
}
switch(y) {
case top: ny = 0; break;
case centre: ny = (int)((scrn.height-win.height)/2); break;
case bottom: ny = (int)(scrn.height-win.height); break;
}
w.setlocation(nx,ny);
}
}
要确定一个窗口的右下角的位置,就要执行下面的代码:
window window = ....
windowutilities.positionwindowonscreen(window,_
windowutilities.right, windowutilities.bottom);
该方法还可以进行扩展。你可以增加一个简单的centerwindowonscreen函数,该函数调用了positionwindowonscreen函数。例如:
static public final void centerwindowonscreen(window w)
{
positionwindowonscreen(w, center, center);
}
这两个函数简化了在awt和swing中所做的工作,使开发人员可以将更多的时间用在开发用户界面上。(zdnet china)
闽公网安备 35060202000074号