我正在研究我的截图方法。我现在添加了命名屏幕截图,我弹出了这个框,你可以在其中输入名称,但是一旦我输入名称并按确定,我就没有错误。但它不保存这是我的新代码
public void takeScreenshot() {
try {
Window window = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusedWindow();
Point point = window.getLocationOnScreen();
int x = (int)point.getX();
int y = (int)point.getY();
int w = window.getWidth();
int h = window.getHeight();
Robot robot = new Robot(window.getGraphicsConfiguration().getDevice());
Rectangle captureSize = new Rectangle(x, y, w, h);
java.awt.image.BufferedImage bufferedimage = robot.createScreenCapture(captureSize);
String sn;
sn = JOptionPane.showInputDialog("Name Yout Screenshot");
File file = new File((new StringBuilder()).append(SignLink.getCacheDirectory() + "Screenshots/" + sn + " ").append(".png").toString());
} catch(Exception e) {
e.printStackTrace();
}
}
这是我工作时的代码
public void takeScreenshot() {
try {
Window window = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusedWindow();
Point point = window.getLocationOnScreen();
int x = (int)point.getX();
int y = (int)point.getY();
int w = window.getWidth();
int h = window.getHeight();
Robot robot = new Robot(window.getGraphicsConfiguration().getDevice());
Rectangle captureSize = new Rectangle(x, y, w, h);
java.awt.image.BufferedImage bufferedimage = robot.createScreenCapture(captureSize);
int picNumber = random(100);
String fileExtension = "The Iron Door";
File file = new File((new StringBuilder()).append(SignLink.getCacheDirectory() + "Screenshots/" + fileExtension + " ").append(picNumber).append(".png").toString());
ImageIO.write(bufferedimage, "png", file);
} catch(Exception e) {
e.printStackTrace();
}
}
我唯一更改的代码行是这一行
File file = new File((new StringBuilder()).append(SignLink.getCacheDirectory() + "Screenshots/" + fileExtension + " ").append(picNumber).append(".png").toString());