1

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

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

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

4

1 回答 1

0

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

例如,您可以将侦听器移动到画布上:

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

}

}
于 2012-12-13T12:07:52.160 回答