AbsoluteLayout是SWT界面设计的默认布局方式,采用坐标绝对定位的方式,定义组件的位置和大小。 java
默认不设置父控件的Layout或者显示使用setLayout(null)设置父控件的布局为AbsoluteLayout绝对布局 shell
例子:使用绝对定位布局制做系统登陆界面 eclipse
要点:设置窗口只有关闭按钮 防止窗口resize后登陆界面变形 难看 布局
截图1、主窗体效果spa
截图2、窗体组件大纲视图.net
java代码:设计
import org.eclipse.swt.SWT; 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; /** * SWT AbsoluteLayout布局使用demo 系统登陆界面设计 * @author xwalker * */ public class AbsoluteLayoutLoginDemo{ private Text unameText; private Text pwdText; private Shell shell; public void open() { Display display = Display.getDefault(); createContents(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } } /** * 建立窗口组件 */ protected void createContents(){ shell=new Shell(SWT.CLOSE); shell.setSize(300, 227); shell.setLayout(null); shell.setText("SWT AbsoluteLayout布局使用"); unameText = new Text(shell, SWT.BORDER); unameText.setBounds(101, 27, 152, 23); pwdText = new Text(shell, SWT.BORDER); pwdText.setBounds(101, 59, 152, 23); Label unameLabel = new Label(shell, SWT.NONE); unameLabel.setBounds(22, 30, 61, 17); unameLabel.setText("用户名"); Label pwdlabel = new Label(shell, SWT.NONE); pwdlabel.setBounds(22, 62, 61, 17); pwdlabel.setText("密码"); Button remeberCheckbox = new Button(shell, SWT.CHECK); remeberCheckbox.setBounds(101, 102, 98, 17); remeberCheckbox.setText("记住登陆帐户"); Button loginBtn = new Button(shell, SWT.NONE); loginBtn.setBounds(34, 134, 80, 27); loginBtn.setText("登陆系统"); Button cancelBtn = new Button(shell, SWT.NONE); cancelBtn.setBounds(147, 134, 80, 27); cancelBtn.setText("取消登陆"); shell.open(); } public static void main(String[] args) { AbsoluteLayoutLoginDemo demo=new AbsoluteLayoutLoginDemo(); demo.open(); } }
注意:通常不推荐使用AbsoluteLayout 布局参数调整麻烦,窗口resize后布局不能适应。code