gpt4 book ai didi

java - 子图上的 MouseMotionListener 事件 - GEF

转载 作者:行者123 更新时间:2023-12-01 04:59:48 29 4
gpt4 key购买 nike

我正在使用一个父draw2d图形,其中包含几个子图形(按钮。使用工具栏布局),这些子图形注册到MouseMotionListener并在鼠标进入时执行一些操作(假设'enterAction')在鼠标退出时执行其他操作('exitAction ')。

由于某种原因,在指向子按钮之一时会调用“exitAction”。
我尝试将父图形的所有子级递归注册到 MouseMotionListener 事件,尽管它“解决了我的问题”(“exitAction”被触发,但我设法在按钮的“enterAction”中捕获它并处理它)-另一个问题发生:按钮的“actionPerformed”事件(我已将按钮添加到 ActionListener)未触发(或者是,但在我找不到的地方处理...)。

有什么帮助吗? :\
谢谢!

最佳答案

这通常很难解决,因为在draw2d中鼠标事件不会像在html中那样通过元素传播。

例如,您可以将监听器移动到 Canvas 上:

import org.eclipse.draw2d.ActionEvent;
import org.eclipse.draw2d.ActionListener;
import org.eclipse.draw2d.Border;
import org.eclipse.draw2d.Button;
import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.FigureCanvas;
import org.eclipse.draw2d.Layer;
import org.eclipse.draw2d.LineBorder;
import org.eclipse.draw2d.ScalableLayeredPane;
import org.eclipse.draw2d.XYLayout;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;

public class HHH {

public static void main(String[] args) {
final Display display = new Display();
final Shell shell = new Shell();
shell.setLayout(new FillLayout());

final FigureCanvas canvas = new FigureCanvas(shell);
final ScalableLayeredPane pane = new ScalableLayeredPane();
pane.setScale(1.5f);

canvas.setContents(pane);

final Layer layer = new Layer();
layer.setLayoutManager(new XYLayout());
pane.add(layer);

final Figure container = new Figure();
container.setOpaque(true);
container.setBackgroundColor(ColorConstants.lightBlue);
container.setLayoutManager(new XYLayout());
layer.add(container, new Rectangle(100, 100, 100, 100));

final Button b = new Button("Push");
b.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent event) {
MessageBox box = new MessageBox(shell);
box.setText("button pressed");
box.open();
}
});
container.add(b, new Rectangle(10, 10, -1, -1));

canvas.addMouseMoveListener(new MouseMoveListener() {

Border oldBorder = null;
boolean over = false;

@Override
public void mouseMove(org.eclipse.swt.events.MouseEvent e) {
final Rectangle containerBounds = container.getBounds().getCopy();
container.translateToAbsolute(containerBounds);

if (containerBounds.contains(e.x, e.y)) {
// simulate enter container figure
if (!over) {
oldBorder = container.getBorder();
container.setBorder(new LineBorder());
over = true;
}
} else {
// simulate exit container figure
if (over) {
container.setBorder(oldBorder);
oldBorder = null;
over = false;
}
}
}

});

shell.setSize(500, 500);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}

}

}

关于java - 子图上的 MouseMotionListener 事件 - GEF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13540535/

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