gpt4 book ai didi

java - 在 SWT 中禁用组件选择

转载 作者:行者123 更新时间:2023-11-29 09:08:29 27 4
gpt4 key购买 nike

我想做的是用我的鼠标在四个标签上绘制,这些标签通过 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);
}

}

});

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

图片:

enter image description here

最佳答案

这是工作示例。它应该做你正在寻找的东西

    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();

关于java - 在 SWT 中禁用组件选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13601992/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com