我有一个应用程序,我已经创建了一个框格,以便用户可以选择调整样式文本对象的大小。
我有一个错误,但如果应用程序最大化,窗扇在应用程序上移动得非常低,然后用户取消最大化应用程序,窗扇仍然超出了屏幕的范围。
如何使用当用户将应用程序从最大化更改为更小的大小时,窗扇自动在应用程序大小的范围内移动;如第一张照片?
另外,是否有更好(更动态)的方法来控制窗扇的位置限制?
separator.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
double height = shlPstmKnowledgeCatalogue.getBounds().height;
double qpBtnHeight = btnQuickParts.getBounds().height;
double workLblHeight = lblWorkInstructions.getBounds().height;
if (!shlPstmKnowledgeCatalogue.getMaximized()) {
if (event.y < workLblHeight + 30) {
event.y = (int) workLblHeight + 30;
}
else if (event.y > height - qpBtnHeight - 90) {
event.y = (int) (height - qpBtnHeight - 90);
}
}
else {
if (event.y < workLblHeight + 30) {
event.y = (int) (workLblHeight + 30);
}
else if (event.y > height - qpBtnHeight - 90) {
event.y = (int) (height - qpBtnHeight - 90);
}
}
separator.setBounds(event.x, event.y, event.width, event.height);
FormData formData = new FormData();
formData.top = new FormAttachment(0, event.y);
formData.left = new FormAttachment(lblScript, 6);
formData.right = new FormAttachment(script, 0, SWT.RIGHT);
formData.height = 5;
separator.setLayoutData(formData);
/*
* Used to move the script label with the movement of the script
* text box. Otherwise, the label gets lost behind the text boxes.
*/
FormData lblScriptData = new FormData();
lblScriptData.top = new FormAttachment(0, event.y - 5);
lblScriptData.bottom = new FormAttachment(0, event.y + 12);
lblScriptData.left = new FormAttachment (workInstructions,2, SWT.LEFT);
lblScript.setLayoutData(lblScriptData);
shlPstmKnowledgeCatalogue.layout(true);
}
});
/*
* Attaches the Work Instuction text box to the sash for dynamic resizing
* The resizing is done in relation to the Script text box
*/
FormData workInstForm = new FormData();
workInstForm.left = new FormAttachment(0, 194);
workInstForm.right = new FormAttachment(100, -6);
workInstForm.bottom = new FormAttachment(separator, -15);
workInstForm.top = new FormAttachment(lblWorkInstructions, 7);
workInstructions.setLayoutData(workInstForm);
formToolkit.adapt(workInstructions, true, true);
/*
* Attaches the Script text box to the sash for dynamic resizing
* The resizing is done in relation to the work instruction text box
*/
FormData scriptForm = new FormData();
scriptForm.top = new FormAttachment(separator, 15);
scriptForm.right = new FormAttachment(workInstructions, 0, SWT.RIGHT);
scriptForm.left = new FormAttachment(workInstructions, 0, SWT.LEFT);
scriptForm.bottom = new FormAttachment(btnQuickParts, -6);
script.setLayoutData(scriptForm);
formToolkit.adapt(script, true, true);
答案 0 :(得分:0)
我最后写了一个单独的方法来不断检查窗扇的位置。也许这不是最有效的方式,但确实有效!
/**
* Check the sash position to verify that it is within the bounds of the shell.
* This is primarily used when the user maxmimizes the application, moves the
* sash, then minimizes the application.
*/
private void checkSashPosition() {
if (!shlPstmKnowledgeCatalogue.isDisposed() && separator.getBounds().y > shlPstmKnowledgeCatalogue.getBounds().height) {
separator.setLocation(235, shlPstmKnowledgeCatalogue.getBounds().height - 248);
workInstructions.setSize(455, 100);
script.setSize(455, 130);
script.setLocation(194, 167);
lblScript.setLocation(194, 143);
}
}
该方法在while循环内部调用,并且还调整了与窗扇相关的对象。