Eclipse开发经典教程之SWT布局 - 编程入门网
og(shell).open();
if (fileName != null) {
dogImage = new Image (display, fileName);
dogPhoto.redraw();
}
}
});
Button delete = new Button(shell, SWT.PUSH);
delete.setText("Delete");
gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.
VERTICAL_ALIGN_BEGINNING);
gridData.horizontalIndent = 5;
delete.setLayoutData(gridData);
delete.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
if (dogImage != null) {
dogImage.dispose();
dogImage = null;
dogPhoto.redraw();
}
}
});
Group ownerInfo = new Group (shell, SWT.NONE);
ownerInfo.setText("Owner Info");
gridLayout = new GridLayout();
gridLayout.numColumns = 2;
ownerInfo.setLayout(gridLayout);
gridData = new GridData (GridData.HORIZONTAL_ALIGN_FILL);
gridData.horizontalSpan = 2;
ownerInfo.setLayoutData(gridData);
new Label(ownerInfo, SWT.NONE).setText ("Name:");
ownerName = new Text(ownerInfo, SWT.SINGLE | SWT.BORDER);
ownerName.setLayoutData(new GridData (GridData.FILL_HORIZONTAL));
new Label(ownerInfo, SWT.NONE).setText ("Phone:");
ownerPhone = new Text(ownerInfo, SWT.SINGLE | SWT.BORDER);
ownerPhone.setLayoutData(new GridData (GridData.FILL_HORIZONTAL));
Button enter = new Button(shell, SWT.PUSH);
enter.setText("Enter");
gridData = new GridData (GridData.HORIZONTAL_ALIGN_END);
gridData.horizontalSpan = 3;
enter.setLayoutData(gridData);
enter.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
System.out.println("\nDog Name: " + dogName.getText());
System.out.println("Dog Breed: " + dogBreed.getText());
System.out.println("Owner Name: " + ownerName.getText());
System.out.println("Owner Phone: " + ownerPhone.getText());
System.out.println("Categories:");
String cats[] = categories.getSelection();
for (int i = 0; i < cats.length; i++) {
System.out.println("\t" + cats[i]);
}
}
});
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (! display.readAndDispatch()) display.sleep();
}
if (dogImage != null) {
dogImage.dispose();
}
}
}
Eclipse开发经典教程之SWT布局(8)时间:2011-04-11 阿甘程序中构建了一个比较复杂 的窗口,设置了GridLayout的布局信息和相关的GridData网格数据信息,程序运行效果如图5 所示。 图5 GridLayout布局实例 注意:每个GridData都必须有单独的实例,几个子组件不能 够共用同一个GridData(即使几个组件有相同属性的GridData也必须新建几个实例)。 自定义布局 在SWT中,用户可以通过setLayout设置组件的布局信息。布局对 象会根据父组件的大小和子组件的布局信息计算出每个子组件的位置和大小,使整个布局空 间符合用户的需求。下面将介绍如何创建自己的布局类,实现用户自定义的布局。 Layout类 在SWT中,所有的布局类都继承于Layout抽象类。Layout有两个抽象 方法。
computeSize方法负责计算组件所有子组件所占的高度和宽度,并返回一个Point 类型的变量(width,height)。layout方 |
凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站:http://www.lingzhong.cn 为了给广大客户了解更多的技术信息,本技术文章收集来源于网络,凌众科技尊重文章作者的版权,如果有涉及你的版权有必要删除你的文章,请和我们联系。以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢! |