无法使用按钮实例化类 [英] cannot instantiate a class using a button
问题描述
我正在尝试制作一个屏幕截图程序。
I am trying to make a screen capturing program.
我所拥有的是一个透明窗口,它将使用按钮<$来捕获区域c $ c>捕获就可以了,我试图实例化一个 captureScreen
这个 captureScreen
是使用命令提示符单独执行的
What I have is a transparent window, which will give the area to be captured, with a button capture
on it, and I am trying to instantiate a class captureScreen
that works good when captureScreen
is individually executed using a command prompt
我试图在按钮时实例化这个 captureScreen
类点击捕获
。
I am trying to instantiate this captureScreen
class when button capture
is hit.
我试过保留这个类
在我的 screenrecord.java
上,将代码放在事件监听器
中。在这两种情况下,我都会收到这些错误
I have tried keeping this class
on my screenrecord.java
, putting the code in event listener
also. In both these cases,I get these errors
AWTException,must be caught or declared
in
Robot robot = new Robot();
和中的IOException> BufferedImage图像
行。
保持 captureScreen.java
分开不做任何事。 System.out.println(开始);
甚至不会打印任何内容。
And keeping the captureScreen.java
separate does nothing.System.out.println("Start");
would even not print anything.
这是我的 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");
}
}
您的所有建议,意见,建议欢迎和赞赏。
请帮我解决这个问题。谢谢。
All your suggestions, comments, advices are welcome and appreciated. Please help me sort this problem. Thanks.
推荐答案
用于编辑 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();
}
}
使用main来实例化另一个函数。
Use main to instantiate another function.
这篇关于无法使用按钮实例化类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!