我正在编写一个程序来搜索互联网中的单词含义。它将从文件中读取单词,打开默认浏览器,在谷歌中搜索单词,拍摄它的快照。把它保存在某个地方。
问题是当我以编程方式搜索25个单词时,在浏览器中打开了25个标签。因此计算机变得很慢。为了解决这个问题,我想在搜索10个单词后关闭浏览器。有没有办法以编程方式关闭它。我正在使用java.awt。操作系统是Windows 8.这是我在循环中调用单词的函数。
public static void takeSnapshot(String word, String folderLocation) {
if (Desktop.isDesktopSupported()) {// For windows
try {
Desktop.getDesktop().browse(new URI("https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q="
+ word + "%20meaning"));
} catch (IOException e) {
e.printStackTrace();
} catch (URISyntaxException e) {
e.printStackTrace();
}
}
/* For linux
* Runtime runtime = Runtime.getRuntime(); try { runtime.exec(
* "/usr/bin/google-chrome -new-window " +
* "https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q="
* + word + "%20meaning"); } catch (IOException e1) { // TODO
* Auto-generated catch block e1.printStackTrace(); }
*/
try {
TimeUnit.SECONDS.sleep(10);
} catch (InterruptedException e2) {
e2.printStackTrace();
}
Rectangle screenRect = new Rectangle();
screenRect.setBounds(100, 150, 700, 700);
BufferedImage capture;
try {
capture = new Robot().createScreenCapture(screenRect);
ImageIO.write(capture, "bmp", new File(folderLocation, word + ".bmp"));
System.out.println("Image is written");
} catch (AWTException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}