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



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

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));
}

以下代码将通过侦听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;
}

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

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

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

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

最新更新