我找了许多资料,发现SWT的很少,但是工作的时候在Eclipse开发窗口化的时候会用到,它相对于Swing来说,外观要更好看一些,性能也好一些,Swing是集成在IDEAJ上的,我通过看SWT face了解一些知识,总结在这里。
是Display的对象,它用来管理事件的循环,Display对象代表了程序级的SWT各个类和底层系统窗口连接的实现。
while(!shell.isDisposed()){
if(!display.readAndDispatch())
diaplay.sleep();
}
通过readAndDispatch()方法进行读取事件
这个我们都熟悉了,他的实例化代码为:
Button button = new Button(shell,SWT(这里写BUtton的按钮类型))
setSelection(Boolen selected)如果接受者样式为SWT.CHECK,SWT.RADIO,SWT.TOGGLE样式风格,则可以设置他的选择状态
文本框用来接收键盘的输入信息。它和按钮一 样都是常用的 SWT 组件,二者常常配合使用
public Text(Composite parent,int style)
Text text=new Text(shell,SWT.NONE); text.setBounds(20, 15, 80, 25);
package basic.widget;
import org.eclipse.swt.*;
import org.eclipse.swt.events.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.jface.dialogs.*;
public class TextExample {
public TextExample() {
final Display display = Display.getDefault();
final Shell shell = new Shell();
shell.setSize(370, 235);
shell.setText("Text 综合实例");
final Label label1 = new Label(shell, SWT.NONE);
label1.setBounds(25, 25, 60, 25);
label1.setText("User Name:");
final Text text1 = new Text(shell, SWT.BORDER);
text1.setBounds(90, 20, 80, 20);
// 当光标停留在该文本框时将出现提示信息
text1.setToolTipText("文本项不能为空");
final Label labe2 = new Label(shell, SWT.NONE);
labe2.setBounds(190, 25, 55, 25);
labe2.setText("PassWord:");
final Text text2 = new Text(shell, SWT.PASSWORD | SWT.BORDER);
text2.setBounds(250, 20, 80, 20);
// setTextLimit(int x)为常用组件方法,用来设置文本框中最多可输入的字符数。
text2.setTextLimit(8);
text2.setToolTipText("文本项不能为空,且输入不超过 8 位密码");
final Text text3 = new Text(shell, SWT.V_SCROLL | SWT.H_SCROLL | SWT.BORDER);
text3.setBounds(50, 55, 250, 90);
final Button button1 = new Button(shell, SWT.NONE);
button1.setBounds(140, 160, 80, 20);
button1.setText("OK");
button1.setToolTipText("单击 OK 按钮,姓名将显示在下面的文本中");
button1.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
String AD = text1.getText();// 获得输入的文本内容
String BD = text2.getText();
// 判断输入文本是否为空
if (AD == null || AD.equals("") || BD == null || BD.equals(""))
{
MessageDialog.openInformation(shell, " 信息提示", "失败信息!" + ’\n’ + ’\n’ + " 注意:文本项不能为空!!!……");
} else
text3.append("User Name: " + ’\n’ + "" + AD + ’\n’
+ "PassWord:" + ’\n’ + "" + BD);
/**
* append()方法用来在文本框中显示内容。
* 将 append()方法改为 insert()方法可达到同样的效果
*/
}
});
final Button button2 = new Button(shell, SWT.NONE);
button2.setBounds(250, 160, 80, 20);
button2.setText("Cancel");
button2.setToolTipText("单击 Cancel 按钮,清除文本中的内容");
button2.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
text1.setText("");// 清除文本内容方法
text2.setText("");
text3.setText("");
}
});
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
}
public static void main(String[] args) {
new TextExample();
}
}
addSelectionListener(SelectonListener listener) 添加监听器到监听器集合中,当某个 窗口部件被选中时通知监听集合。通过监听器发送在 SelectonListener 接口中定义的 一个消息来通知监听器。
下拉框(Combo)作用是用户从下拉项中 选择选项,用户也可以在下拉框(Combo)中键入选项值。
public Combo (Composite parent, int style)
Combo combo=new Combo(shell,SWT.NONE);