2

我要做的是用鼠标在四个标签上绘制,这些标签通过paintListner与盒子布局组合在一起,添加到每个标签上。此外,每个标签都有一个 MouseMoveListener,它将每个鼠标点添加到一个 ArrayList。这是一个标签 l 的代码:

l.addMouseMoveListener(new MouseMoveListener() {
    public void mouseMove(MouseEvent e) {
        compLocation.setLocation(l.getLocation().x, l.getLocation().y);
        pointsToDraw1.get(n).add(new Point(e.x, e.y));
        l.redraw();
     }

});


l.addPaintListener(new PaintListener(){
    @Override
    public void paintControl(PaintEvent e) {
     Device device = Display.getCurrent ();
     Color red = new Color (device, 255, 0, 0);
     e.gc.setBackground(red);
     for(Point p : pointsToDraw1.get(n)){
        e.gc.fillRectangle(p.x, p.y, 4, 4);
     }

    }

});

当我用鼠标移动标签时,一切正常(参见示例图像的顶部)。一旦我按下鼠标左键并在绘图时保持按下状态,我只在开始按下按钮的标签上绘图(参见示例图像的底部)。这是因为我通过单击它会自动选择标签。是否有可能以某种方式禁用此自动选择并仅检查是否按下了鼠标左键?我只想在按下鼠标左键时绘制。

图片:

在此处输入图像描述

4

1 回答 1

0

这是工作样本。它应该做你正在寻找的

    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setSize(400, 400);
    final Point p = new Point(0, 0);
    shell.addMouseMoveListener(new MouseMoveListener() {

      @Override
      public void mouseMove(MouseEvent e) {

         p.x = e.x;
         p.y = e.y;

         shell.redraw(p.x,p.y,2,2,true);

         for(Control c: shell.getChildren())
         {
           if(c.getBounds().contains(p))
           {
             Point t = e.display.map(shell, c, p);
             p.x = t.x;
             p.y = t.y;
             c.redraw(p.x,p.y,2,2,true);
           }
         }

      }
    });
    PaintListener painter = new PaintListener() {

      @Override
      public void paintControl(PaintEvent e) {

        e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_BLUE));
        e.gc.fillRectangle(p.x, p.y, 2, 2);

      }
    };
    shell.addPaintListener(painter);
    final Label l = new Label(shell, SWT.NONE);
    l.setBounds(10, 10, 60, 40);
    l.setBackground(display.getSystemColor(SWT.COLOR_CYAN));
    l.setText("Label1");
    l.addPaintListener(painter);
    l.addMouseMoveListener(new MouseMoveListener() {

      @Override
      public void mouseMove(MouseEvent e) {


        p.x = e.x;
        p.y = e.y;

        Point t = e.display.map(l, shell, p);

        Rectangle bounds = l.getBounds();
        if(bounds.contains(t))
        {
          l.redraw(p.x,p.y,2,2,true);
        }
        else
        {
          p.x = t.x;
          p.y = t.y;
          shell.redraw(p.x,p.y,2,2,true);
        }
      }
    });

    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch()) {
            display.sleep();
        }
    }
    display.dispose();
于 2012-11-28T20:33:10.647 回答