我知道很多人都遇到过这个问题,但是我在网上找到的解决方案似乎并没有解决我的问题。我有一个具有三个按钮的组合。我想要的是以下内容: 当我单击一个按钮时,我希望其他按钮变灰 ( setEnabled(false) ) 并在一段时间后(在执行方法后),我希望再次启用该按钮。
很多这样的问题都是通过在父容器上调用 layout() 方法来解决的,或者 this very similar one通过调用 Display.getCurrent().update() 解决;
简单来说,我的代码可以总结如下:
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
public class app1 {
protected Shell shell;
/**
* Launch the application.
* @param args
*/
public static void main(String[] args) {
try {
app1 window = new app1();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Open the window.
*/
public void open() {
Display display = Display.getDefault();
createContents();
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
/**
* Create contents of the window.
*/
Button button1 , button2 , button3;
Label label;
protected void createContents() {
shell = new Shell();
shell.setSize(450, 300);
shell.setText("SWT Application");
shell.setLayout(new GridLayout(1,false));
{
final Composite composite = new Composite(shell, SWT.NONE);
composite.setLayout(new GridLayout(3,false));
GridData gd_composite = new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL);
gd_composite.grabExcessHorizontalSpace = true;
gd_composite.horizontalSpan = 10; //?
gd_composite.verticalIndent = 5;
composite.setLayoutData(gd_composite);
GridData gd_button;
{
button1 = new Button(composite, SWT.NONE);
button1.setText("Button 1");
gd_button = new GridData(SWT.FILL, GridData.BEGINNING, false, false);
gd_button.horizontalSpan = 1;
button1.setLayoutData(gd_button);
button1.addSelectionListener(new SelectionListener(){
public void widgetSelected(SelectionEvent e){
try{
button2.setEnabled(false);
button2.redraw();
button2.update();
//composite.redraw();
//composite.update();
//composite.layout();
shell.redraw();
shell.update();
shell.layout();
Display.getCurrent().update();
} catch (Exception e2) {
System.err.println("exception e : " + e2.toString());
}
System.out.println("basla");
try {
System.out.println("sleep1");
Thread.sleep(100);
} catch (InterruptedException e1) {
e1.printStackTrace();
} catch (Throwable th) {
System.err.println("th: " + th.toString());
}
try {
System.out.println("sleep2");
Thread.sleep(100);
} catch (InterruptedException e1) {
e1.printStackTrace();
} catch (Throwable th) {
System.err.println("th: " + th.toString());
}
try {
System.out.println("sleep3");
Thread.sleep(100);
} catch (InterruptedException e1) {
e1.printStackTrace();
} catch (Throwable th) {
System.err.println("th: " + th.toString());
}
for(int i=0 ; i < 10000 ; i++)
{
System.out.println(i);
}
}
public void widgetDefaultSelected(SelectionEvent e) {
System.err.println("widgetDefault !");
}
});
}
{
button2 = new Button(composite, SWT.NONE);
button2.setText("Button 2");
gd_button = new GridData(SWT.FILL, GridData.CENTER, false, false);
gd_button.horizontalSpan = 1;
button2.setLayoutData(gd_button);
button2.addSelectionListener(new SelectionListener(){
public void widgetSelected(SelectionEvent e){
button1.setEnabled(false);
composite.layout();
for (int i=1; i<=100; i++) {
try {
Thread.sleep(10);
} catch (Throwable th) {}
label.setText(i + " %");
label.update();
}
}
public void widgetDefaultSelected(SelectionEvent e) {}
});
}
{
label = new Label(composite , SWT.NONE);
label.setText("0 %");
label.update();
}
}
}
}
发生的事情是,在到达 widgetSelected() 方法结束后按钮被禁用。然而,标签经常更新没有任何问题(即使没有 label.update() 方法)
附加信息:比如说,我禁用了按钮,然后放了一个 Thread.sleep() 然后启用了按钮;它先休眠,然后快速禁用和启用按钮。所以我相信所有这样的绘画请求都会排队,并在执行结束时得到处理。
有用信息:我意识到,当我在显示更改后立即创建并显示 MessageBox 时,显示会发生更改。因此,如果我在 widgetSelected 方法中进行以下更改:
button2.setEnabled(false)
MessageBox mBox = new MessageBox(Display.getCurrent().getActiveShell(), SWT.ICON_INFORMATION | SWT.OK);
mBox.setText("Information");
mBox.setMessage("Buttons updated!");
mBox.open();
一旦调用了 widgetSelected() 方法,按钮就会变灰。这让我相信我的解决方案在于 Display.getCurrent() 方法。不过,我试过了
Display.getCurrent().getActiveShell().redraw() Display.getCurrent().getActiveShell().update() Display.getCurrent().getActiveShell().layout()
方法并没有解决我的问题。
谢谢, 埃格
最佳答案
好的,我已经更正了 ginu 的答案:
New Runnable().run() 其实做的不多,但思路是对的:
您需要一个新线程来完成您的工作。问题是,您不能从那个线程调用按钮上的 setEnabled,因为那只能从 SWT-Event 线程中完成。
所以你需要另一个可运行的来重置按钮。第二个 runnable 被传递给 Display.callAsync 并在实际执行之前返回,但这在这里无关紧要。您还可以使用 Display.callSync(Runnable),该调用会阻塞您的调用线程,直到可运行对象返回。
在 Eclipse 中测试过,到目前为止看起来不错。
编辑:顺便说一句,调用 layout() 或 Display.update() 不起作用的原因是您当前正在用您的工作阻塞 SWT-Thread,因此对 layout/update 的调用被排队并且只被执行当您离开事件处理程序时。永远不要阻止事件处理程序做长时间的工作。 :)
package test;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class Test {
public static void main(final String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setLayout(new GridLayout(3, false));
final Button button1 = new Button(shell, SWT.PUSH);
button1.setText("Click");
final Button button2 = new Button(shell, SWT.PUSH);
button2.setText("Me");
final Button button3 = new Button(shell, SWT.PUSH);
button3.setText("Dude");
button1.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false));
button2.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false));
button3.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false));
button2.setEnabled(false);
button3.setEnabled(false);
button1.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(final SelectionEvent e) {
button1.setEnabled(false);
button2.setEnabled(true);
button3.setEnabled(true);
new Thread( new Runnable() {
public void run() {
try {
// Do your operation here.
//
// Dummy sleep performed here instead.
Thread.sleep(1000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
shell.getDisplay().asyncExec( new Runnable() {
public void run() {
button1.setEnabled(true);
button2.setEnabled(false);
button3.setEnabled(false);
}
});
}
} ).start();
}
});
shell.open();
shell.pack();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
关于layout - SWT更新/重绘/布局问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1180086/