gpt4 book ai didi

java - 多次截图程序

转载 作者:行者123 更新时间:2023-12-01 16:45:28 25 4
gpt4 key购买 nike

我创建了一个小程序,用于在单击按钮时截取屏幕截图。但是,一旦我单击它并截取屏幕截图并将其另存为“屏幕截图”,我就无法单击它并截取另一张而不覆盖我刚刚截取的屏幕截图。我希望它能够在不关闭程序的情况下获取多个并保存它们。

Screenshot of my code that I took with my program.

谢谢!

编辑:

    package screenshot;

import java.awt.AWTException;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class NewJFrame extends javax.swing.JFrame {


public NewJFrame() {
initComponents();
}

// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jButton1 = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jButton1.setIcon(new javax.swing.ImageIcon("C:\\Users\\manwe\\Desktop\\java stuff\\6inch_1.png")); // NOI18N
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton1)
);

pack();
}// </editor-fold>

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
try {
Robot robot = new Robot();
String format = "png";
String fileName = "screenshot." + format;

Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
BufferedImage screenFullImage;
BufferedImage BufferedImage = screenFullImage = robot.createScreenCapture(screenRect);
ImageIO.write(screenFullImage, format, new File(fileName));


} catch (AWTException | IOException ex) {

}
}


public static void main(String args[]) {
try {
Robot robot = new Robot();
String format = "png";
String fileName = "screenshot." + format;

Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
BufferedImage screenFullImage;
BufferedImage BufferedImage = screenFullImage = robot.createScreenCapture(screenRect);
ImageIO.write(screenFullImage, format, new File(fileName));


} catch (AWTException | IOException ex) {

}



java.awt.EventQueue.invokeLater(() -> {
new NewJFrame().setVisible(true);
});
}

// Variables declaration - do not modify
private javax.swing.JButton jButton1;
// End of variables declaration

}

最佳答案

为该文件使用另一个名称。您使用相同的名称,这就是为什么它会覆盖以前的文件。

您可以将时间戳附加到您的名称上。这将确保图像具有不同的名称。

您可以使用此代码来获取时间戳:

String timeStamp = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").format(new Date());

ImageIO.write(screenFullImage, format, new File(fileName+timeStamp));

关于java - 多次截图程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52369674/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com