SWT编码技巧积累

1.包命名
org.rssowl.ui.dialogs.cleanup
说明:org.rssowl表示公司域名逆序,ui下面全部都是界面设计的类。
dialogs表示所有对话框,cleanup代表某一个对话框的所有类。

2.控件有visible特性,要充分利用这个特性,如下:
Rectangle rect = fColorItem.getBounds();
Point pt = new Point(rect.x, rect.y + rect.height);
pt = fBar.toDisplay(pt);
fColorMenu.setLocation(pt.x, pt.y);
fColorMenu.setVisible(true);

3.控件有一个setData和getData方法,这个特性也可以加以利用,如:
private void onColorSelected(RGB color) {
    fSelectedColor = color;

    if (fColorItem.getImage() != null)
      fColorItem.getImage().dispose();

    fColorItem.setImage(createColorImage(fSelectedColor));

    MenuItem[] items = fColorMenu.getItems();
    for (MenuItem item : items) {
      if (fSelectedColor.equals(item.getData()))
        item.setSelection(true);
      else if (item.getSelection())
        item.setSelection(false);
    }
}


4.Text有selectAll方法和setFocus方法,有时候,要编辑或更改某个对话框值时,这2个方法可以用上。如:
Label nameLabel = new Label(composite, SWT.NONE);
nameLabel.setText(Messages.LabelDialog_NAME);
fNameInput = new Text(composite, SWT.BORDER | SWT.SINGLE);
fNameInput.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false));//SWT.BEGINNING与SWT.LEFT和SWT.TOP同义(分别针对水平和垂直方向)
fNameInput.setText(fExistingLabel.getName());
fNameInput.selectAll();
fNameInput.setFocus();

5.错误提示,有时候将错误提示放在控件下文,也能有不错的视觉效果。

6.显示对话框合适大小的方式:
 @Override
protected Point getInitialSize() {
    Point shellSize = super.getInitialSize();
    return new Point(Math.max(convertHorizontalDLUsToPixels(MIN_DIALOG_WIDTH), shellSize.x), shellSize.y);
}


7.针对控件设置Focus事件,在Text之类的控件上,这个事件可以利用,以增加界面的友好感。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值