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' –
這意味着它共享一個單獨的顯示? –
一個正常的SWT應用程序只創建一個用於一切的「Display」對象。你永遠不能在同一個線程和某些平臺上創建第二個顯示對象(例如macOS),所以根本無法創建第二個顯示對象。 –