I do not know whether you have such an experience: In fact, the software user requirements are very basic, and now the software is becoming more and more complex, a large part of the software developers put their attention to some additional functions (these features may surprise users, but if not their users are not dissatisfied) on, But the real user's request is not satisfied. So when we design the program, we must understand that sometimes simplicity is a kind of beauty, spend time to really valuable place to go.
OK, back to our topic. In this section, I'll give you an introduction to SWT's event patterns. As we mentioned before, writing an SWT program is just a few steps away. One of the more things to worry about is the layout of the user interface and handling of various events.
Hello,world! with Event handling added
In fact, SWT handling events is very simple, corresponding to various events have a corresponding listener class, if an event is called XYZ, then the corresponding listener class is Xyzlistener. For example, corresponding to the mouse event has mouselistener, corresponding to the keyboard event is keylistener. In each widget, there is a Addxyzlistener method for the events it can handle, so just pass the corresponding listener instance as a parameter to it.
For a clearer explanation, let's take a look at the following procedure:
1 public class Eventdemo {
2
3 private Shell _shell
4
5 public Eventdemo () {
6 Display displ ay = new Display ();
7 Shell shell = new Shell (DISPLAY,SWT. Shell_trim);
8 Setshell (shell);
9 rowlayout layout=new rowlayout ();
Ten shell.setlayout (layout);
One Shell.settext ("Event demo"); The
button button=new button (SHELL,SWT. PUSH | Swt. CENTER);
Button.settext ("click me!");
Button.addselectionlistener (New Selectionlistener () {
M
/public void widgetselected ( Selectionevent event) {
Handleselectionevent ();
public void widgetdefaultselected (Selection Event event) {
}
24});
Shell.setbounds (200,300,100,100);
Shell.open ();
while (!shell.isdisposed ()) {
if (!display.readanddispatch ()) {
Display.sleep ();
to}
Display.dispose ()
()
protected void Handleselectionevent () {
MessageBoxDialog=new MessageBox (Getshell (), SWT. ok| Swt. Icon_information);
Dialog.settext ("Hello");
Dialog.setmessage ("hello,world!");
Dialog.open ();
42}
$
/**
@param args
*/
public static void Main (string[] args) {
+
-Eventdemo Eventdemo=new Eventdemo ();
50}
Wuyi
/**
* @return Returns the _shell.
*/
The public Shell Getshell () {
_shell;}
/**
* @param _shell the _shell to set.
* */
"public void Setshell" (shell shell) { This._shell =shell;
64}
65}
$