一不小心發現的,竟然可以用哦。當然,大俠們可能早就知道了。
package com.laozizhu.search.client.test;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
/**
* SWT中,Label垂直居中,水平居中的方法.
*
* @author 老紫竹(laozizhu.com)
*/
public class LabelVerticleMiddle {
Display display = new Display();
Shell shell = new Shell(display);
Image image = new Image(display, "laozizhu.png");
public LabelVerticleMiddle() {
// 一不小心發現,這個SAHDOW竟然可以垂直居中哦。
CLabel clabel = new CLabel(shell, SWT.SHADOW_NONE);
clabel.setAlignment(SWT.CENTER);
clabel.setImage(image);
clabel.setText("老紫竹的家");
clabel.setBackground(display.getSystemColor(SWT.COLOR_YELLOW));
clabel.setBounds(10, 10, 150, 150);
shell.setSize(180, 200);
shell.open();
// Set up the event loop.
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
// If no more entries in event queue
display.sleep();
}
}
display.dispose();
}
public static void main(String[] args) {
new LabelVerticleMiddle();
}
}