|
Posted on 2009-02-26 22:21 泰仔在线 阅读(1659) 评论(0) 编辑 收藏 所属分类: Java 相关
一、使用SWT本身的Display.getClientArea().width. height

////
loginShell.setLocation(display.getClientArea().width / 2 - loginShell.getSize().x/2, display
.getClientArea().height / 2 - loginShell.getSize().y/2);
////
二、使用SWT本身的loginShell.getMonitor().getBounds() 与loginShell.getBounds()
////
Monitor primary = loginShell.getMonitor();
Rectangle bounds = primary.getBounds();
Rectangle rect = loginShell.getBounds();
int x = bounds.x + (bounds.width - rect.width) / 2;
int y = bounds.y + (bounds.height - rect.height) / 2;
if (x < 0)
x = 0;
if (y < 0)
y = 0;
loginShell.setLocation(x, y);
////
原例:
package demo;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

 public class Login {

 /** *//**
* Launch the application
* @param args
*/
 public static void main(String[] args) {
final Display display = Display.getDefault();
final Shell loginShell = new Shell();
loginShell.setSize(424, 222);
loginShell.setText("Login");
////
loginShell.setLocation(display.getClientArea().width / 2 - loginShell.getSize().x/2, display
.getClientArea().height / 2 - loginShell.getSize().y/2);
////
loginShell.open();

final Label label1 = new Label(loginShell, SWT.NONE);
label1.setAlignment(SWT.RIGHT);
label1.setText("ユーザID:");
label1.setBounds(101, 46, 65, 12);

final Label label2 = new Label(loginShell, SWT.NONE);
label2.setAlignment(SWT.RIGHT);
label2.setBounds(101, 81, 65, 12);
label2.setText("パスワード:");

final Text userName = new Text(loginShell, SWT.BORDER);
userName.setBounds(173, 42, 105, 20);

final Text password = new Text(loginShell, SWT.BORDER);
password.setBounds(173, 77, 105, 20);

final Button btnLogin = new Button(loginShell, SWT.NONE);
 btnLogin.addMouseListener(new MouseAdapter() {
 public void mouseDown(MouseEvent e) {
 try {
MaterialUpload window = new MaterialUpload();
loginShell.close();
window.open();
 } catch (Exception ex) {
ex.printStackTrace();
}
}
});
btnLogin.setText("ログイン");
btnLogin.setBounds(101, 130, 59, 22);

final Button btnClear = new Button(loginShell, SWT.NONE);
 btnClear.addMouseListener(new MouseAdapter() {
 public void mouseDown(MouseEvent arg0) {
userName.setText("");
password.setText("");
}
});
btnClear.setText("クリア");
btnClear.setBounds(183, 130, 59, 22);

final Button btnClose = new Button(loginShell, SWT.NONE);
 btnClose.addMouseListener(new MouseAdapter() {
 public void mouseDown(MouseEvent arg0) {
loginShell.close();
}
});
btnClose.setBounds(264, 130, 59, 22);
btnClose.setText("閉じる");
loginShell.layout();
 while (!loginShell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
}

}
|