sendText.addKeyListener(new KeyListener(){
public void keyPressed(KeyEvent e) {
if(e.keyCode == SWT.CR){
//讓按鍵原有的功能失效
e.doit = false;
//執行你本身的事件
MessageBox box = new MessageBox(new Shell(), SWT.ICON_INFORMATION | SWT.OK);
box.setText("提示信息");
box.setMessage("按回車鍵了");
box.open();
}
}
public void keyReleased(KeyEvent e) {}
});java
- package test.ftp00;
- import java.net.InetSocketAddress;
- import java.text.DateFormat;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import org.eclipse.swt.SWT;
- import org.eclipse.swt.events.KeyEvent;
- import org.eclipse.swt.events.KeyListener;
- import org.eclipse.swt.events.SelectionEvent;
- import org.eclipse.swt.events.SelectionListener;
- import org.eclipse.swt.layout.GridData;
- import org.eclipse.swt.layout.GridLayout;
- import org.eclipse.swt.widgets.Button;
- import org.eclipse.swt.widgets.Composite;
- import org.eclipse.swt.widgets.Display;
- import org.eclipse.swt.widgets.MessageBox;
- import org.eclipse.swt.widgets.Shell;
- import org.eclipse.swt.widgets.Text;
- public class keyWindow {
- Display display;
- Shell shell;
- GridLayout gridLayout;
- GridData layoutData;
- Composite composite;
- Text sendText;
- Text mesText;
- DateFormat formatter = new SimpleDateFormat("HH:mm:ss");
- public keyWindow() {
- display = Display.getDefault();
- shell = new Shell(display);
- // 初始化shell
- initShell();
- layoutData = new GridData();
- layoutData.widthHint = 260;
- layoutData.heightHint = 200;
- mesText = new Text(shell, SWT.MULTI | SWT.WRAP | SWT.V_SCROLL
- | SWT.BORDER);
- mesText.setLayoutData(layoutData);
- layoutData = new GridData();
- layoutData.widthHint = 260;
- layoutData.heightHint = 60;
- sendText = new Text(shell, SWT.MULTI | SWT.WRAP | SWT.V_SCROLL
- | SWT.BORDER);
- sendText.setLayoutData(layoutData);
- sendText.setFocus();
- sendText.addKeyListener(new KeyListener() {
- public void keyPressed(KeyEvent e) {
- if (e.keyCode == SWT.CR) {
- // 讓按鍵原有的功能失效
- e.doit = false;
- // 執行你本身的事件
- // MessageBox box = new MessageBox(new Shell(),
- // SWT.ICON_INFORMATION | SWT.OK);
- // box.setText("提示信息");
- // box.setMessage("按回車鍵了");
- // box.open();
- addString(sendText.getText());
- }
- }
- public void keyReleased(KeyEvent e) {
- }
- });
- }
- public void open() {
- shell.open();
- while (!shell.isDisposed()) {
- if (!display.readAndDispatch()) {
- display.sleep();
- }
- }
- }
- /**
- * 設置窗口的標題、位置、大小、圖標
- *
- * @return Shell
- */
- public Shell initShell() {
- shell.setText("交談");
- shell.setSize(400, 350);
- shell.setLayout(new GridLayout());
- return shell;
- }
- /**
- * 向聊天區域添加信息
- *
- * @param msg
- */
- public void addString(String mes) {
- mesText.append(mes + SWT.LF);
- }
- public static void main(String[] args) {
- new keyWindow().open();
- }
- }
完整示例shell
Color red = display.getSystemColor(SWT.COLOR_RED);
Font font = display.getSystemFont();
control.setFont(font)
app
Styleeclipse |
Descriptionide |
---|---|
SWT.WRAPspa |
Wrap the text to fit the visible area.net |
SWT.LEFT3d |
Left-align the labelorm |
SWT.CENTERxml |
Center-align the label |
SWT.RIGHT |
Right-align the label |
SWT.SEPARATOR |
Draw a separator instead of text or an p_w_picpath |
SWT.HORIZONTAL |
Draw the separator horizontally |
SWT.VERTICAL |
Draw the separator vertically |
SWT.SHADOW_IN |
Draw the separator with a "shadow in" effect |
SWT.SHADOW_OUT` |
Draw the separator |
KeyEvent |
KeyListener (and KeyAdapter) |
keyPressed(KeyEvent) keyReleased(KeyEvent) |