嵌套SWT消息框错误

时间:2017-10-11 01:59:43

标签: java junit swt messagebox

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();
    }
}
}

这是我目前拥有的代码。 所以这就是我想要做的事情:

  1. 询问用户是否要开始测试
  2. 如果是,请运行JUnit测试
  3. JUnit测试完成后。询问用户是否要再次重复测试
  4. 所以在这段代码中,一切正常,直到第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)
    

    任何人都可以帮我看看有什么问题吗?谢谢

2 个答案:

答案 0 :(得分:0)

错误是由创建新的Display ...(从未使用过,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();
}

答案 1 :(得分: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