0

オブジェクトにいくつかのオブジェクトを追加したいと思いCompositeます。オブジェクトはさまざまな形(長方形、円、楕円、さらには奇妙な形(多角形で表される)を持つことができます)。そこで私は次のようなクラスを定義しました。

public class Circle extends Canvas {
}

public class Rectangle extends Canvas {
}

...

を描画して目的の形状を取得する方法は知ってCanvasいますが、ユーザーがキャンバス領域内でマウスをクリックした場合にのみポップアップメニューが各キャンバスに表示されることも期待していたため、これらのコードを複合クラスで使用すると、次のようになります。

Menu aSampleMenu = new Menu(this);

Circle circle = new Circle(parent, style);
circle.setMenu(aSampleMenu);

ユーザーがキャンバスの内側、またはシェイプ領域の外側でもマウスの右ボタンをクリックすると、メニューが表示されます。この問題を解決するにはどうすればよいですか?

4

2 に答える 2

0

1つの解決策はRegionを使用することですが、それをサークルケースに適用する方法がわかりません。

    Region region = new Region();
    region.add(new int[] {3, 3, 20, 20, 3, 20});

    Canvas c = new Canvas(this, SWT.NONE);
    c.setBounds(35, 35, 60, 60);
    c.setRegion(region);

    Menu menu = new Menu(this);
    c.setMenu(menu);

    MenuItem mntmProperties = new MenuItem(menu, SWT.NONE);
    mntmProperties.setText("Properties");

    MenuItem mntmDelete = new MenuItem(menu, SWT.NONE);
    mntmDelete.setText("Delete");
于 2012-12-23T11:03:41.610 に答える
0

以下のコード スニペットをご覧ください。トリックは、最初のものを定義してから、メニュー検出を許可する必要がある sMenuのみに設定することです:Control

public class StackOverflow
{
    public static void main(String[] args)
    {
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setLayout(new GridLayout(2, true));

        Composite c1 = new Composite(shell, SWT.BORDER);
        c1.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

        Composite c2 = new Composite(shell, SWT.BORDER);
        c2.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

        Menu menu = new Menu(shell, SWT.POP_UP);
        MenuItem item = new MenuItem(menu, SWT.PUSH);
        item.setText("Popup");

        new Label(shell, SWT.BORDER).setText("No menu here");
        new Label(shell, SWT.BORDER).setText("No menu here");

        // Add menu only to c1 and c2, not to the shell and not to the labels
        c1.setMenu(menu);
        c2.setMenu(menu);

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

スクリーンショットは次のとおりです。

ここに画像の説明を入力

于 2012-12-23T10:39:24.840 に答える