2017-10-11 87 views
-2
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消息框給出錯誤

  1. 詢問用戶是否要開始測試
  2. 如果是的話,運行JUnit測試
  3. 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) 

誰能幫我看看有什麼不好?謝謝

+1

您應該只創建一個'Display' –

+0

這意味着它共享一個單獨的顯示? –

+1

一個正常的SWT應用程序只創建一個用於一切的「Display」對象。你永遠不能在同一個線程和某些平臺上創建第二個顯示對象(例如macOS),所以根本無法創建第二個顯示對象。 –

回答

0

錯誤是通過創建一個新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(); 
} 
+0

我剛試過,但似乎沒有工作。它只顯示第一個消息框,第二個消息框未顯示。 –

+0

哎呀,一個錯字,糾正... –

+0

同樣的事情,仍然沒有顯示第二個消息框 –

0
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

+0

不,您不需要... –

+0

但它只有在將第二部分更改爲rc2後纔有效。否則,第二個消息框不會出現。 –