0

このダイアログを別のウィンドウ解像度でサイズ変更する方法を教えてください。

protected Object openDialogBox(Control cellEditorWindow)
    {
        dialog = new DialogHead();

        dialog.setShell(550, 280);
        dialog.setShellOnCenter();
        dialog.setShellText("Properties");
        dialog.setShellImage(BPMNUtils.getAbsolutePath()+ "icons/module.gif");


        composite = dialog.getComposite(0, 0, 550, 435);
        GridLayout layout = new GridLayout();
        composite.setLayout(layout);

        Group propertyGroup = dialog.getGroup(composite, "Properties", new Rectangle(5, 2, 530, 237));


        //getPackage();

        final Table table = dialog.getTable(propertyGroup, new Rectangle(10, 20, 422, 38));

        table.setLinesVisible(true);
        table.setHeaderVisible(true);
        GridData gd_table = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 3);
        gd_table.heightHint = 160;
        table.setLayoutData(gd_table);


        dialog.getTableColumn(table, 0, "Name", 100);
        dialog.getTableColumn(table, 1, "Type", 125);
        dialog.getTableColumn(table, 2, "Value", 100);
        dialog.getTableColumn(table, 3, "Correlation", 100);
        dialog.getTableColumn(table, 4, "Mode", 68);

        createInitialContents(table);
        dialog.measureItem(table);

        final TableEditor editor = new TableEditor(table);


        final Button addButton = dialog.getPushButton(propertyGroup,"Add", new Rectangle(150, 210, 60, 24));
        final Button deleteButton =dialog.getPushButton(propertyGroup,"Delete", new Rectangle(210, 210, 60, 24));
        final Button okButton = dialog.getPushButton(propertyGroup,"OK", new Rectangle(270, 210, 60, 24));
        final Button cancelButton =dialog.getPushButton(propertyGroup,"Cancel", new Rectangle(330, 210, 60, 24));
        }
4

2 に答える 2

2

次のコードは、イベントをリッスンして、ウィンドウが常に画面の幅と高さの 80% のサイズを維持するように強制しますResize。このアプローチにより、手動でのサイズ変更が妨げられることに注意してください。

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

        shell.addListener(SWT.Resize, new Listener()
        {
            @Override
            public void handleEvent(Event arg0)
            {
                int[] res = getResolution();
                shell.setSize((int) (res[0] * 0.8), (int) (res[1] * 0.8));
            }
        });

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

    private static int[] getResolution() {
        Toolkit toolkit = Toolkit.getDefaultToolkit();
        Dimension dim = toolkit.getScreenSize();

        int[] result = new int[2];
        result[0] = dim.width;
        result[1] = dim.height;

        return result;
    }
}

このアプローチでは、AWT を使用して解像度を取得します。解決策を取得するためのすべての SWT アプローチは次のようになります。

private static int[] getResolution() {
    Rectangle monitor = Display.getDefault().getPrimaryMonitor().getBounds();

    int[] result = new int[2];
    result[0] = monitor.width;
    result[1] = monitor.height;

    return result;
}

このアプローチでは、デスクトップ全体のサイズではなく、現在の画面の画面解像度が考慮されることに注意してください (マルチスクリーン設定を使用している場合)。

于 2012-11-28T09:18:24.780 に答える
1

私が正しく理解している場合、必要なのは現在の画面解像度を知る方法であり、ダイアログのサイズを比例的に変更できます (たとえば、幅 33%、高さ 50% など)。

Display#getBounds()それを達成するために画面解像度を取得するために使用できます。

Rectangle screenSize = Display.getCurrent().getBounds();
dialog.setShell(screenSize.width / 3, screenSize.height / 2);
于 2012-11-28T08:03:13.003 に答える