如何更改对话框以使用不同的Windows分辨率调整大小

时间:2012-11-28 06:16:38

标签: java swt screen-resolution

任何人都可以帮我解决如何更改此对话框以使用不同的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));
        }

2 个答案:

答案 0 :(得分: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;
}

请注意,此方法会考虑当前屏幕的屏幕分辨率,而不是桌面的整体尺寸(使用多屏设置时)。

答案 1 :(得分:1)

如果我理解你所需要的是一种了解当前屏幕分辨率的方法,那么你可以按比例调整对话框的大小(例如33%宽度和50%高度)。

您可以使用Display#getBounds()获取屏幕分辨率来实现:

Rectangle screenSize = Display.getCurrent().getBounds();
dialog.setShell(screenSize.width / 3, screenSize.height / 2);