[英]How do I turn off a program with a java code?
Hi guys I open in my java application I open a browser page with this code: 嗨,大家好,我在Java应用程序中打开了一个带有以下代码的浏览器页面:
String URL = "https://www.google.com/";
try {
java.awt.Desktop.getDesktop().browse(java.net.URI.create(URL));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Now I have to turn off the browser, Is there a code to do that? 现在我必须关闭浏览器,是否有执行此操作的代码?
不,没有Java API可以关闭浏览器。
There sort-of is but it's not very reliable. 虽然有点,但不是很可靠。 But if you have to, you can try with keyboard shortcuts and mouse clicks using Java Robot API.
但是,如果需要,您可以尝试使用Java Robot API使用键盘快捷键和鼠标单击。
public class RobotTest {
public static void main(String[] args) {
String URL = "https://www.google.com/";
try {
java.awt.Desktop.getDesktop().browse(java.net.URI.create(URL));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
Thread.sleep(3*1000);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Robot javaRobot;
try {
javaRobot = new Robot();
javaRobot.keyPress(KeyEvent.VK_ALT);
javaRobot.keyPress(KeyEvent.VK_F4);
javaRobot.keyRelease(KeyEvent.VK_ALT);
javaRobot.keyRelease(KeyEvent.VK_F4);
} catch (AWTException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.