嵌套的SWT消息框给出错误
public class MesssageBoxQuestionIconYESNOButton {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
//This is added to be able to run JUnit in this java class
JUnitCore junit = new JUnitCore();
Result result;
int style = SWT.ICON_WARNING | SWT.YES | SWT.NO;
MessageBox messageBox = new MessageBox(shell, style);
messageBox.setMessage("Would you like to start the test?");
int rc = messageBox.open();
if(rc == SWT.YES)
{
//Must add in the .class else it will not work
result = junit.run(testMyCode.class);
//This part is to ask if the user want to repeat the test again
Display display1 = new Display();
Shell shell1 = new Shell(display);
int style1 = SWT.ICON_WARNING | SWT.YES | SWT.NO;
MessageBox messageBox1 = new MessageBox(shell, style);
messageBox1.setMessage("Would you like to repeat the test?");
int rc1 = messageBox.open();
if(rc1 == SWT.YES)
{
result = junit.run(testMyCode.class);
}
else
{
MessageBox messageBox2 = new MessageBox(shell, style);
messageBox2.setMessage("Thank You For Using");
display1.dispose();
}
}
else
{
display.dispose();
}
}
}
这是我目前拥有的代码。 原来这就是我想要做的:嵌套的SWT消息框给出错误
- 询问用户是否要开始测试
- 如果是的话,运行JUnit测试
- JUnit测试完成后。询问用户是否要再次重复测试
因此,在此代码中,一切正常,直到第3步。
这是我收到的错误:
Exception in thread "main" org.eclipse.swt.SWTException: Invalid thread access
at org.eclipse.swt.SWT.error(Unknown Source)
at org.eclipse.swt.SWT.error(Unknown Source)
at org.eclipse.swt.SWT.error(Unknown Source)
at org.eclipse.swt.widgets.Display.checkDisplay(Unknown Source)
at org.eclipse.swt.widgets.Display.create(Unknown Source)
at org.eclipse.swt.graphics.Device.<init>(Unknown Source)
at org.eclipse.swt.widgets.Display.<init>(Unknown Source)
at org.eclipse.swt.widgets.Display.<init>(Unknown Source)
谁能帮我看看有什么不好?谢谢
错误是通过创建一个新Display
......造成
我做了一些修改,也因此尝试这样的事情(这是从来没有使用过,顺便说一句。):
public static void main(final String[] args)
{
final Display display = new Display();
final Shell shell = new Shell(display);
// This is added to be able to run JUnit in this java class
final JUnitCore junit = new JUnitCore();
Result result;
final int style = SWT.ICON_WARNING | SWT.YES | SWT.NO;
final MessageBox messageBox = new MessageBox(shell, style);
messageBox.setMessage("Would you like to start the test?");
int rc = messageBox.open();
if (rc == SWT.YES)
{
// Must add in the .class else it will not work
result = junit.run(testMyCode.class);
// This part is to ask if the user want to repeat the test again
final MessageBox messageBox1 = new MessageBox(shell, style);
messageBox1.setMessage("Would you like to repeat the test?");
rc = messageBox1.open();
if (rc == SWT.YES)
{
result = junit.run(testMyCode.class);
}
int style1 = SWT.ICON_WARNING | SWT.OK;
final MessageBox messageBox2 = new MessageBox(shell, style1);
messageBox2.setMessage("Thank You For Using");
messageBox2.open();
}
display.dispose();
}
我刚试过,但似乎没有工作。它只显示第一个消息框,第二个消息框未显示。 –
哎呀,一个错字,纠正... –
同样的事情,仍然没有显示第二个消息框 –
public static void main(final String[] args)
{
final Display display = new Display();
final Shell shell = new Shell(display);
// This is added to be able to run JUnit in this java class
final JUnitCore junit = new JUnitCore();
Result result;
final int style = SWT.ICON_WARNING | SWT.YES | SWT.NO;
final MessageBox messageBox = new MessageBox(shell, style);
messageBox.setMessage("Would you like to start the test?");
int rc = messageBox.open();
if (rc == SWT.YES)
{
// Must add in the .class else it will not work
result = junit.run(testMyCode.class);
// This part is to ask if the user want to repeat the test again
final MessageBox messageBox1 = new MessageBox(shell, style);
messageBox1.setMessage("Would you like to repeat the test?");
int rc2 = messageBox1.open();
if (rc2 == SWT.YES)
{
result = junit.run(testMyCode.class);
}
else
{
int style1 = SWT.ICON_WARNING | SWT.OK;
final MessageBox messageBox2 = new MessageBox(shell, style1);
messageBox2.setMessage("Thank You For Using");
messageBox2.open();
}
}
display.dispose();
}
您还需要更改rc
不,您不需要... –
但它只有在将第二部分更改为rc2后才有效。否则,第二个消息框不会出现。 –
您应该只创建一个'Display' –
这意味着它共享一个单独的显示? –
一个正常的SWT应用程序只创建一个用于一切的“Display”对象。你永远不能在同一个线程和某些平台上创建第二个显示对象(例如macOS),所以根本无法创建第二个显示对象。 –