0

请任何人帮助我如何更改此对话框以使用不同的 Windows 分辨率调整大小。

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

以下代码将通过监听Resize事件来强制窗口始终保持屏幕宽度和高度的 80% 的大小。请记住,这种方法将阻止手动调整大小:

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

If I understand you correctly what you need is a way to know what the current screen resolution is, so that you can proportionally resize the dialog (like 33% width and 50% height, for example).

You can use Display#getBounds() to fetch the screen resolution to achieve that:

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