添加输入对话框时没有发生动作

时间:2013-10-29 04:25:07

标签: java input

我正在研究我的截图方法。我已经添加了命名截图,我弹出了框,你可以输入名称,但一旦输入名称 然后按确定我没有错误。但它没有保存 这是我的新代码

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

1 个答案:

答案 0 :(得分:0)

你遗漏了ImageIO.write(bufferedimage, "png", file);