swt/jface中的每一个组件之间都有很多同名的方法,很幸运,这些同名方法在各个组件的作用和用法是相同或相似的,在此将一些常用的方法简述如下:
1、setText(String string);说明:设置组件的标签文字。例子:button.setText("确定");
2、setToolTipText(String string);说明:设置鼠标停留在组件上时,出现黄色的提示条中的文字。例子:button.setBounds(46,123,99,23);
3、setEnabled(boolean enabled);说明:设置组件是否可用。false不可用,true(默认值)可用。例子:button.setEnabled(false);
4、setFont(Font font);说明:设置文字的字体。例子:button.setFont(ResourceManager.getFont("",14,SWT.BOLD|SWT.ITALIC));ResourceManager是SWT Desgner在建立SWT项目时自动生成的类。
5、setSelection(boolean selected);说明:设置是否选上。true为选上,false(默认值)为不选上。当Button为复选框或单选框时此方法才会有效。例子:button.setSelection(false);
6、setForeground(Color color);说明:设置前景色。例子:button.setForeground(Display.getCunent().getSystemColor(SWT.COLOR_RED));或者button.setForeground(ResourceManager.getColor(255,255,0));
7、setBackground(Color color);说明:设置背景色。例子:label.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_RED));
8、setAlignment(int alignment);说明:设置标签文字的对齐方式.