2

我尝试使用表单的 addmouselistener 方法并检查点击是否在标题栏区域,但鼠标侦听器不起作用。我试图在 form 和 form.getform() 和 form.getBody() 上添加鼠标侦听器,两者都不起作用。

这是我创建表单并尝试在其上添加鼠标侦听器的代码:

        toolKit = new FormToolkit(parent.getDisplay());
    form = toolKit.createScrolledForm(parent);

    FillLayout layout = new FillLayout();
    layout.type = SWT.VERTICAL;
    layout.marginHeight = 10;
    layout.marginWidth = 4;

    canvas = new FigureCanvas(form.getBody(), SWT.DOUBLE_BUFFERED );
    canvas.setViewport(new FreeformViewport());
    canvas.setBackground(ColorConstants.white);
    canvas.setContents(root);
    form.getBody().setLayout(layout);
    form.setText("Data Transactions View");

    createHeaderRegion(form);


    toolKit.decorateFormHeading(form.getForm());
    form.getToolBarManager().add(new Action("This") { });

    form.getForm().addMouseListener(new MouseListener(){

        @Override
        public void mouseDoubleClick(MouseEvent e) {
            // TODO Auto-generated method stub

        }

        @Override
        public void mouseDown(MouseEvent e) {

            if(e.x<10 && e.y<10){
                form.getToolBarManager().add(new Action("This is the toolbar") { });    // NEW LINE
                form.getToolBarManager().update(true);  // NEW LINE
            }
        }

        @Override
        public void mouseUp(MouseEvent e) {
            // TODO Auto-generated method stub

            }
        }});

有什么建议么?

4

1 回答 1

2

您必须将侦听器添加到表单的头部。

public void createPartControl(Composite parent) {

    ScrolledForm scrolledForm = formToolkit
            .createScrolledForm(parent);
    formToolkit.paintBordersFor(scrolledForm);
    scrolledForm.setText("New ScrolledForm");
    formToolkit.decorateFormHeading(scrolledForm.getForm());

    scrolledForm.getForm().getHead().addMouseListener(new MouseListener() {

        @Override
        public void mouseDoubleClick(MouseEvent e) {
            System.out.println("mouseDoubleClick");
        }

        @Override
        public void mouseDown(MouseEvent e) {
            System.out.println("mouseDown");
        }

        @Override
        public void mouseUp(MouseEvent e) {
            System.out.println("mouseUp");
        }

    });

}
于 2012-11-28T16:27:16.027 回答