2017-10-11 13 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. 그들은 예, 완료 JUnit 테스트 후 JUnit 테스트
  2. 를 실행하면 테스트를
  3. 를 시작하려면 사용자에게 질문 그래서이 내가하고 싶은 것입니다. 테스트를 다시 반복 할 것인지 묻는 메시지를 사용자에게 묻습니다.

이 코드에서는 모든 것이 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 ... 생성에 의해 발생하므로이 같은 시도했다
+1

하나의 '디스플레이' –

+0

만 만들어야합니다. 하나의 디스플레이를 공유한다는 의미입니까? –

+1

일반적인 SWT 애플리케이션은 모든 것에 사용되는 단일 'Display'객체 만 작성합니다. 같은 스레드에서 두 번째 표시 객체를 만들 수없고 일부 플랫폼 (예 : macOS)에서는 두 번째 표시 객체를 전혀 만들 수 없습니다. –

답변

0

(BTW, 사용되지 않았다.) :

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로 변경 한 후에 만 ​​작동합니다. 그렇지 않으면 두 번째 메시지 상자가 나오지 않습니다. –