我要做的是用鼠标在四个标签上绘制,这四个标签是通过paintListner在盒子布局中复合的,添加到每个标签上。此外,每个标签都有一个MouseMoveListener,它将每个鼠标点添加到ArrayList。这是一个Label 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);
}
}
});
当我用鼠标移动标签时,一切正常(请参见示例图像的顶部)。一旦我按下鼠标左键并在绘图时保持按下,我只会在标签上画出我按下按钮(参见示例图像的下半部分)。这是因为我通过点击它自动选择标签。有可能以某种方式禁用此自动选择,只是检查是否按下了鼠标左键?我只想在按下鼠标左键时画画。
图像:
答案 0 :(得分: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();