这个应用程序并没有真正的用途,但我习惯于作为练习,因为我是新手哈哈。我已经成功地将屏幕截图保存到设定位置,但现在当我尝试添加一个功能以使用控制台设置输出位置时,应用程序似乎在我输入 1 或 0 后就结束了。
public class Mainclass {
public static void main(String[] args) throws IOException,
HeadlessException, AWTException {
Scanner scan = new Scanner(System.in);
String login = System.getenv().get("USERNAME");
String desk = "/Desktop/lol/";
int num = 0;
int numd = 100000;
int dd = 10000000;
int dl = 999999;
String start = null;
String location = "C:/users/" + login + desk + num + ".png";
int loc = 1;
System.out.println("Enter a new custom location to print image");
// the if statment is used to make it possible to change the image
// location output
loc = scan.nextInt();
if (loc == (1)) {
location = scan.nextLine();
start = scan.nextLine();
} else {
location = "C:/users/" + login + desk + num + ".png";
start = scan.nextLine();
}
while (start.equals("start")) {
num++;
numd--;
dd++;
dl--;
BufferedImage image = new Robot()
.createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit()
.getScreenSize()));
ImageIO.write(image, "png", new File(location));
ImageIO.write(image, "png", new File("C:/users/" + login + desk
+ num + ".png"));
ImageIO.write(image, "png", new File("C:/users/" + login + desk
+ numd + ".png"));
ImageIO.write(image, "png", new File("C:/users/" + login + desk
+ dd + ".png"));
ImageIO.write(image, "png", new File("C:/users/" + login + desk
+ dl + ".png"));
JFrame window = new JFrame();
window.pack();
window.setSize(1920, 1080);
window.setResizable(false);
window.setLocationRelativeTo(null);
window.setVisible(true);
}
}
}
最佳答案
那么您眼前的问题可以通过以下方式解决:
loc = scan.nextInt();
String discard = scan.nextLine(); // add this as the new line stays in input
if (loc == 1) {
location = scan.nextLine();
start = scan.nextLine();
} else {
location = "C:/users/" + login + desk + num + ".png";
start = scan.nextLine();
}
现在您这样做的方式会向无限窗口发送垃圾邮件 - 这就是您想要的吗?
关于java图像输出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34106989/