当我希望它保持打开时,java awt窗口保持关闭状态

时间:2015-10-28 09:17:37

标签: java modal-dialog awt

大家。我的java.awt应用程序有问题。

我正在使用代码制作轮廓图。 当我在构造代码中运行main函数时,它运行正常而没有关闭,但每当我想测试它构成Junit测试用例时,它会保持关闭,而我希望它保持打开状态。

以下是轮廓的原始代码

import java.awt.*;

public class ColorContourPlot
extends Frame
implements Runnable
{

public ColorContourPlot()
{
    // {{INIT_CONTROLS
    setLayout( null );
    addNotify();
    resize( insets().left + insets().right + 545, insets().top + insets().bottom + 346 );
    BackButton = new java.awt.Button( "<<" );
    BackButton.reshape( insets().left + 40, insets().top + 288, 50, 29 );
    add( BackButton );
    ForButton = new java.awt.Button( ">>" );
    ForButton.reshape( insets().left + 116, insets().top + 288, 50, 29 );
    add( ForButton );

    setTitle( "Color Contour Plot" );

    menuBar1 = new java.awt.MenuBar();

    menu1 = new java.awt.Menu( "Options" );
    menu1.add( "Contour Resolution" );
    menuBar1.add( menu1 );
    setMenuBar( menuBar1 );

    show();

    captureMode = true;
    javaVersion = new String( getJavaVersion() );

    Rectangle R = bounds();
    contourCanvas = new ColorContourCanvas( this );
    int yoffset = insets().top + insets().bottom;
    int xoffset = insets().left + insets().right;

    if ( !captureMode )
    {
        contourCanvas.reshape( insets().left + 5, insets().top + 5, R.width - xoffset - 10, R.height - yoffset - 10 );
        BackButton.hide();
        ForButton.hide();

    }
    else
    {
        contourCanvas.reshape( insets().left + 5, insets().top + 5, R.width - xoffset - 10, R.height - yoffset - 60 );
        if ( javaVersion.equals( "1.1" ) )
        {
            yoffset = 5;
        }
        BackButton.reshape( insets().left + 40, R.height - yoffset - 35, 50, 29 );
        ForButton.reshape( insets().left + 116, R.height - yoffset - 35, 50, 29 );

    }

    add( contourCanvas );

    resDialog = new ContourResolutionDialog( this, this, false );
    resDialog.hide();

    contourData = new java.util.Vector( 10 );
    currentFrame = 0;
    RunningAsThread = false;
}

java.awt.Button BackButton;

java.awt.Button ForButton;

java.awt.MenuBar menuBar1;

java.awt.Menu menu1;

public synchronized void show()
{
    move( 50, 50 );
    super.show();
}

public void repaint()
{
    Rectangle R = bounds();
    int yoffset = insets().top + insets().bottom;
    int xoffset = insets().left + insets().right;

    if ( !captureMode )
    {
        if ( contourCanvas != null )
        {
            contourCanvas.reshape( insets().left + 5, insets().top + 5, R.width - xoffset - 10, R.height - yoffset
                - 10 );
        }
    }
    else
    {
        if ( contourCanvas != null )
        {
            contourCanvas.reshape( insets().left + 5, insets().top + 5, R.width - xoffset - 10, R.height - yoffset
                - 60 );
            if ( javaVersion.equals( "1.1" ) )
            {
                yoffset = 5;
            }
            BackButton.reshape( insets().left + 40, R.height - yoffset - 35, 50, 29 );
            ForButton.reshape( insets().left + 116, R.height - yoffset - 35, 50, 29 );

        }
    }
    super.repaint();
}

public boolean handleEvent( Event event )
{
    if ( event.id == Event.WINDOW_DESTROY )
    {
        if ( !RunningAsThread )
        {
            hide(); // hide the Frame
            dispose(); // free the system resources
            System.exit( 0 ); // close the application
            return true;
        }
        else
        {
            hide();
            return true;
        }
    }

    if ( event.target == BackButton && event.id == Event.ACTION_EVENT )
    {
        BackButton_Clicked( event );
        return true;
    }
    if ( event.target == ForButton && event.id == Event.ACTION_EVENT )
    {
        ForButton_Clicked( event );
        return true;
    }
    return super.handleEvent( event );
}

public boolean action( Event event, Object arg )
{
    if ( event.target instanceof MenuItem )
    {
        String label = (String) arg;
        if ( label.equalsIgnoreCase( "Contour Resolution" ) )
        {
            ContourResolution_Action( event );
            return true;
        }
    }
    return super.action( event, arg );
}

void ForButton_Clicked( Event event )
{
    if ( currentFrame < contourData.size() )
    {
        currentFrame++;
        contourCanvas.setData( (double[][]) ( contourData.elementAt( currentFrame - 1 ) ) );

    }
}

void BackButton_Clicked( Event event )
{
    if ( currentFrame > 1 )
    {
        currentFrame--;
        contourCanvas.setData( (double[][]) ( contourData.elementAt( currentFrame - 1 ) ) );

    }
}

void ContourResolution_Action( Event event )
{
    resDialog.setResolution( contourCanvas.getResolution() );
    resDialog.show();
}

public void dialogDismissed( Dialog d )
{
    if ( d == resDialog )
    {
        int panelCount = resDialog.getResolution();
        contourCanvas.setResolution( panelCount );
        contourCanvas.repaint();
    }
}

public void setCaptureMode( boolean M )
{
    captureMode = M;
    contourData.removeAllElements();
    currentFrame = 0;
}

public void reset()
{
    contourData.removeAllElements();
    currentFrame = 0;
}

public void setDataRange( double dMin, double dMax )
{
    contourCanvas.setDataRange( dMin, dMax );
}

public void setData( double[][] inputData )
{
    if ( captureMode )
    {
        contourData.addElement( inputData );
        if ( contourData.size() == 1 )
        {
            contourCanvas.setData( inputData );

            currentFrame = 1;
        }
    }
    else
    {
        contourCanvas.setData( inputData );
    }
}

public void setData( double[] inputData, int xDataCount, int yDataCount )
{
    double[][] data = new double[xDataCount][yDataCount];
    int i;
    int j;
    for ( i = 0; i < xDataCount; i++ )
    {
        for ( j = 0; j < yDataCount; j++ )
        {
            data[i][j] = inputData[j + i * yDataCount];
        }
    }
    setData( data );
}

public void run()
{
    RunningAsThread = true;
    show();
}

public String getJavaVersion()
{
    java.util.Properties P = System.getProperties();

    java.util.StringTokenizer Tok = new java.util.StringTokenizer( P.getProperty( "java.version", null ), "." );

    String version;
    String p1, p2;
    if ( Tok.countTokens() > 1 )
    {
        p1 = Tok.nextToken();
        p2 = Tok.nextToken();
        version = p1 + "." + p2;
    }
    else
    {
        version = "1.0";
    }
    return version;
}

static public void main( String args[] )
{
    double[][] data = new double[5][5];
    for ( int i = 0; i < 5; i++ )
    {

        for ( int j = 0; j < 5; j++ )
        {
            data[i][j] = ( i * 40 ) + j;
        }
    }

    data[3][4] = 200000;
    data[2][4] = 200000;
    data[1][4] = 200000;
    data[4][4] = 200000;
    data[0][4] = 200000;

    ColorContourPlot M = new ColorContourPlot();
    M.setVisible( true );
    M.setData( M.setup2Ddata2( data ) );
}

private double[][] setup2Ddata( int n )
{
    double[][] data = new double[n][n];
    int i;
    int j;
    double x;
    double y;

    double a = -3.0; // create data for surface plot
    double b = 3.0;
    double c = -7.0;
    double d = 9.0;

    double hx = ( b - a ) / ( (double) n );
    double hy = ( d - c ) / ( (double) n );

    for ( i = 0; i < n; i++ )
    {

        for ( j = 0; j < n; j++ )
        {
            data[i][j] = ( i * 40 ) + j;
        }
    }

    data[3][10] = 0;
    data[20][30] = 85000;

    return data;

}

public double[][] setup2Ddata2( double[][] data )
{
    double[][] data2Paint = new double[data.length][data[0].length];
    int i;
    int j;
    double x;
    double y;

    double a = -3.0; // create data for surface plot
    double b = 3.0;
    double c = -7.0;
    double d = 9.0;

    for ( i = 0; i < data.length; i++ )
    {

        for ( j = 0; j < data.length; j++ )
        {
            data2Paint[i][j] = data[i][j];
        }
    }

    return data2Paint;

}

boolean RunningAsThread;

boolean captureMode;

public ColorContourCanvas contourCanvas;

ContourResolutionDialog resDialog;

String javaVersion = new String();

java.util.Vector contourData;

int currentFrame;
}

以下是我的jUnit测试代码

public class TestGraph
{

@BeforeClass
public static void setUpBeforeClass()
    throws Exception
{
}

@Test
public void test()
    throws IOException
{
    ColorContourPlot M = new ColorContourPlot();

    double[][]  data = new double[5][5];
    for(int i =0; i < 5; i++)
    {
        for(int j = 0; j < 5; j++)
        {
            data[i][j] = (i*40) + j;
        }
    }

    data[3][4] = 200000;
    data[2][4] = 200000;
    data[1][4] = 200000;
    data[4][4] = 200000;
    data[0][4] = 200000;

    M.setData(M.setup2Ddata2(data));
    M.setVisible(true);
    M.setAlwaysOnTop( true );
}
}

有什么想法吗?

0 个答案:

没有答案