初学SWT,不甚了解。请问各位达人。我在Composite上画一个圆(方),然后设置圆(方)的背景图片为1.jpg。然后添加事件,当我在窗口上点击鼠标,如果鼠标落在圆(方)里,则System.out.println("点中:圆(方)");如上所述我应该怎么做啊!
非常感谢各位达人的帮助!!!

解决方案 »

  1.   

    添加一个MouseListener然后通过他来的到鼠标的坐标,在和圆的坐标比较,然后打出输出的东西
    你先看看
    不行的话我给你一段代码
    网上相关知识还是很多的
      

  2.   

    判断鼠标当前的位置与你画的方的范围一比就ok了,代码如下:package csdn;import org.eclipse.swt.SWT;
    import org.eclipse.swt.events.MouseEvent;
    import org.eclipse.swt.events.MouseListener;
    import org.eclipse.swt.events.PaintEvent;
    import org.eclipse.swt.events.PaintListener;
    import org.eclipse.swt.layout.FillLayout;
    import org.eclipse.swt.widgets.Canvas;
    import org.eclipse.swt.widgets.Display;
    import org.eclipse.swt.widgets.Group;
    import org.eclipse.swt.widgets.Shell;public class GetLocationOnJPG {
    // private static Image image; private static int x; private static int y; private static int width; private static int height; /**
     * @param args
     */
    public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout()); // Create image object
    // image = new Image(display,
    // "C:\\Documents and Settings\\qingkangxu\\デスクトップ\\umlGraph\\classGraph.jpg"); Group group = new Group(shell, SWT.SHELL_TRIM);
    group.setLayout(new FillLayout());
    group.setText("a square"); x = 20;
    y = 20;
    width = 50;
    height = 50;
    Canvas canvas = new Canvas(group, SWT.NONE);
    canvas.addPaintListener(new PaintListener() {
    public void paintControl(PaintEvent e) {
    e.gc.drawRectangle(x, y, width, height);
    }
    });
    canvas.addMouseListener(new MouseListener() { public void mouseDoubleClick(MouseEvent event) { } public void mouseDown(MouseEvent event) {
    int clickX = event.x;
    int clickY = event.y;
    if ((clickX >= x) && (clickX <= x + width) && (clickY >= y)
    && (clickY <= y + height)) {
    System.out.println("You have arive Rectangle");
    }
    System.out.println("x:" + event.x);
    System.out.println("y:" + event.y);
    } public void mouseUp(MouseEvent event) {
    } }); shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
    if (!display.readAndDispatch())
    display.sleep();
    }
    display.dispose();
    }}