【问题标题】:cannot instantiate a class using a button无法使用按钮实例化类
【发布时间】:2013-06-17 17:00:43
【问题描述】:

我正在尝试制作屏幕捕获程序。

我拥有的是一个透明窗口,它将提供要捕获的区域,上面有一个按钮 capture,我正在尝试实例化一个类 captureScreen,当 captureScreen 单独执行时效果很好使用命令提示符

当按下按钮 capture 时,我正在尝试实例化这个 captureScreen 类。

我尝试将这个class 保留在我的screenrecord.java 上,并将代码也放入event listener。在这两种情况下,我都会收到这些错误

AWTException,must be caught or declared

在

 Robot robot = new Robot();

和BufferedImage image 行中的 IOException。

并且将captureScreen.java 分开没有任何作用。System.out.println("Start"); 甚至不会打印任何内容。

这是我的screenrecord.java 代码

public class screenrecord extends JFrame implements ActionListener{
    public screenrecord() {...
    }
    public void actionPerformed(ActionEvent e){
        if ("record".equals(e.getActionCommand())) {
            captureScreen a = new captureScreen();
            } 
    }   
}

而captureScreen.java,单独工作也很好。

public class captureScreen extends Object{

    public static void main(String args[]){
        ...
        Robot robot = new Robot();
        BufferedImage image = robot.createScreenCapture(screenRectangle);
        ImageIO.write(image, "png", new File(filename));
        System.out.println("Done");
    }

}

欢迎和赞赏您的所有建议、cmets 和建议。 请帮我解决这个问题。谢谢。

【问题讨论】:

    标签: java swing actionlistener instantiation awtrobot


    【解决方案1】:

    致力于将captureScreen.java 编辑为,

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

    使用 main 实例化另一个函数。

    【讨论】:

      【解决方案2】:

      您需要使用 try/catch。这些与其说是错误不如说是警告。例如,在有 AWTException 的代码周围插入这个:

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

      还有:

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

      或者两者结合:

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

      为了进一步阅读,这可能有助于澄清例外情况:

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

      【讨论】:

      • 我用谷歌搜索并使用了 try-catch。但是我的问题没有解决。我无法实例化我的课程。即使captureScreen.java 中的"Start" 和"Done" 也不会被打印出来。屏幕也没有被捕获。 Donesss 从screenrecord.java 打印出来
      • 而且在捕获异常时,try 部分根本不起作用。这不是故意的,因为这部分是最重要的。
      • 那我不能告诉你出了什么问题。但是您描述的错误显然是因为您没有 try/catch 语句。如果还有其他错误,它们与这些错误不对应,您没有给我们任何继续帮助您的信息。
      • 哦,我搞定了。并且没有其他错误。我已经发布了该问题的解决方案。然后删除了不必要的代码。对不起,如果这让你感到困惑。但是感谢您帮助捕获异常。:)
      猜你喜欢
      • 1970-01-01
      • 2019-11-22
      • 2014-10-08
      • 2022-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-14
      • 2018-09-05
      相关资源
      最近更新 更多