我想根据它们的可见性属性重绘组合中的按钮。我根据其中存在的按钮进行合成以调整大小,并且我正在使用以下代码来刷新合成。 问题:下面的代码工作正常,但按钮永远不会在复合中重新定位 请帮助。代码中是否缺少要重新定位的东西?
public void redraw_buttons() {
int offsetButton = 5;
int buttonHeight =28*3;
for (Control kid : compositeButtons.getChildren()) {
if (kid.getClass() == Button.class) {
if(kid.getVisible() == true){
kid.setLocation(0, 0);//layout(true, true);
kid.setSize(50,60);
kid.redraw();
kid.pack();
compositeButtons.redraw();
kid.setRedraw(true);
kid.setBounds(10, offsetButton, 259, buttonHeight);
kid.redraw();
offsetButton = offsetButton + buttonHeight;
}}
}
compositeButtons.redraw();
}
同样为了在复合材料上创建按钮,我正在使用以下代码
// 初始化复合---
compositeButtons = new Composite(shell, SWT.BORDER);
compositeButtons.setLayout(null);
FormData fd_compositeButtons = new FormData();
fd_compositeButtons.top = new FormAttachment(lblUserLoggedinOnServer, 6);
fd_compositeButtons.bottom = new FormAttachment(textRecentLog, -6);
fd_compositeButtons.right = new FormAttachment(textRecentLog, 0, SWT.RIGHT);
fd_compositeButtons.left = new FormAttachment(0, 30);
compositeButtons.setLayoutData(fd_compositeButtons);
compositeButtons.layout(true, true);
getShell().layout(true, true);
// 添加按钮到复合 -
int offsetButton = 5;
int buttonHeight =28*3;
MakeAppointmentRequestButton = new Button(compositeButtons, SWT.NONE);
MakeAppointmentRequestButton.setBounds(10, offsetButton, 259, buttonHeight);
//MakeAppointmentRequestButton.layout(true, true);
MakeAppointmentRequestButton.setRedraw(true);
MakeAppointmentRequestButton.setText("Make Appointment");
offsetButton = offsetButton + buttonHeight;
GotoAppointmentRequestButton = new Button(compositeButtons, SWT.NONE);
GotoAppointmentRequestButton.setBounds(10, offsetButton, 259, buttonHeight);//(10, offsetButton, 259, 28*3);
GotoAppointmentRequestButton.setRedraw(true);
GotoAppointmentRequestButton.setText("Goto Appointment");
offsetButton = offsetButton + buttonHeight;
CancelAppointmentRequestButton= new Button(compositeButtons, SWT.NONE);
CancelAppointmentRequestButton.setBounds(10, offsetButton, 259, buttonHeight);//(10, 28*3+5, 259, 28*3);
CancelAppointmentRequestButton.setRedraw(true);
CancelAppointmentRequestButton.setText("Cancel Appointment");
offsetButton = offsetButton + buttonHeight;
我已经看到各种链接也尝试了 pack()、redraw() 属性。但是问题:即使在调用自定义函数 redraw_buttons(); 时,按钮也永远不会重新定位。
请帮忙。代码中是否缺少要重新定位的东西?请建议这样做的方法。我是 SWT 的新手。
谢谢..
******* 编辑 新 更新 ********
错误是由于
1.)redraw_buttons()
函数未在主线程上调用。
_appLoader.display.getDefault().asyncExec(new Runnable() {
public void run() {
_appLoader.MyObj.redraw_buttons();
}
});
// This gave access to main thread.
2.) 复合框架没有在 redraw_buttons() 中调整大小;
// Code: On Which I am still working upon.
我应该调整外壳的大小吗?请建议。
谢谢你的帮助。