无法使用按钮实例化类

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

我所拥有的是一个透明窗口,它将捕获区域,并在其上capture一个按钮,我试图实例化一个类captureScreen ,当使用命令提示符单独执行captureScreen ,该类可以正常工作

我试图在按下capture按钮时实例化此captureScreen类。

我试过在screenrecord.java上保留这个class ,也把代码放在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"); } } 

欢迎您提出所有建议,意见,建议。 请帮我解决这个问题。 谢谢。

你需要使用try / catches。 这些不是错误,而是警告。 例如,在具有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

适用于编辑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来实例化另一个函数。