cannot instantiate a class using a button

人走茶凉 提交于 2019-11-28 00:29:30

You need to be using try/catches. Those aren't errors so much as warnings. For instance, insert this around the code that has the AWTException:

try
{
    //code causing AWTException
    Robot robot = new Robot();
}
catch(AWTException e)
{
    System.out.println("Error"+e);
}

And:

try
{
    //code causing IOException
    BufferedImage image = robot.createScreenCapture(screenRectangle);
}
catch(IOException e)
{
    System.out.println("Error"+e);
}

Or combine both:

try
{
    //code causing AWTException or IOException
    Robot robot = new Robot();
    BufferedImage image = robot.createScreenCapture(screenRectangle);
}
catch(IOException e)
{
    System.out.println("Error"+e);
}
catch(AWTException e)
{
    System.out.println("Error"+e);
}

For further reading, this may help clarify exceptions:

http://docs.oracle.com/javase/tutorial/essential/exceptions/definition.html

Works on editing the captureScreen.java as,

public class captureScreen extends Object{

    public captureScreen() {
        ....
        filename = ".\\out.png";
        try{Robot robot = new Robot();
             ............ }
        catch(Exception e)  /* Catch Exceptions too  */
        {
            System.out.println("Error"+e);
        }
    }

    public static void main(String args[]){
        new captureScreen();
    }
}

Use main to instantiate another function.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!