GWT QQ 群号:28310588
用过gwt的都知道,对一个按钮button做点击事件很简单,但要做鼠标事件,比如over,up,leave就没办法了,但extends Button就能做到,以下就是这个例子:我自己写的
package mypack.client;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.ClickListenerCollection;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.LoadListener;
import com.google.gwt.user.client.ui.LoadListenerCollection;
import com.google.gwt.user.client.ui.MouseListener;
import com.google.gwt.user.client.ui.MouseListenerCollection;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.SourcesClickEvents;
import com.google.gwt.user.client.ui.SourcesLoadEvents;
import com.google.gwt.user.client.ui.SourcesMouseEvents;
import com.google.gwt.user.client.ui.Widget;
/** *//**
* Entry point classes define <code>onModuleLoad()</code>.
*/
public class MouseTest implements EntryPoint {
/** *//**
* This is the entry point method.
*/
public void onModuleLoad() {
final MyButton button = new MyButton("Super Button");
final Label label = new Label();
button.addMouseListener(new MouseListener() {
public void onMouseDown(Widget sender, int x, int y) {
// TODO Auto-generated method stub
}
public void onMouseEnter(Widget sender) {
// TODO Auto-generated method stub
}
public void onMouseLeave(Widget sender) {
// TODO Auto-generated method stub
}
public void onMouseMove(Widget sender, int x, int y) {
label.setText("Hello World!");
}
public void onMouseUp(Widget sender, int x, int y) {
}
});
// Assume that the host HTML has elements defined whose
// IDs are "slot1", "slot2". In a real app, you probably would not want
// to hard-code IDs. Instead, you could, for example, search for all
// elements with a particular CSS class and replace them with widgets.
//
RootPanel.get("slot1").add(button);
RootPanel.get("slot2").add(label);
}
}
class MyButton extends Button implements SourcesClickEvents,
SourcesMouseEvents, SourcesLoadEvents {
public MyButton(String text){
super(text);
sinkEvents(Event.ONCLICK | Event.MOUSEEVENTS | Event.ONLOAD | Event.ONERROR);
}
private ClickListenerCollection clickListeners;
private LoadListenerCollection loadListeners;
private MouseListenerCollection mouseListeners;
public void addClickListener(ClickListener listener) {
if (clickListeners == null)
clickListeners = new ClickListenerCollection();
clickListeners.add(listener);
}
public void addLoadListener(LoadListener listener) {
if (loadListeners == null)
loadListeners = new LoadListenerCollection();
loadListeners.add(listener);
}
public void addMouseListener(MouseListener listener) {
if (mouseListeners == null)
mouseListeners = new MouseListenerCollection();
mouseListeners.add(listener);
}
public void onBrowserEvent(Event event) {
switch (DOM.eventGetType(event)) {
case Event.ONCLICK: {
if (clickListeners != null)
clickListeners.fireClick(this);
break;
}
case Event.ONMOUSEDOWN:
case Event.ONMOUSEUP:
case Event.ONMOUSEMOVE:
case Event.ONMOUSEOVER:
case Event.ONMOUSEOUT: {
if (mouseListeners != null)
mouseListeners.fireMouseEvent(this, event);
break;
}
case Event.ONLOAD: {
if (loadListeners != null)
loadListeners.fireLoad(this);
break;
}
case Event.ONERROR: {
if (loadListeners != null)
loadListeners.fireError(this);
break;
}
}
}
public void removeClickListener(ClickListener listener) {
if (clickListeners != null)
clickListeners.remove(listener);
}
public void removeLoadListener(LoadListener listener) {
if (loadListeners != null)
loadListeners.remove(listener);
}
public void removeMouseListener(MouseListener listener) {
if (mouseListeners != null)
mouseListeners.remove(listener);
}
}