我正在做一个 Java 项目,我需要通过点击 JButton
打开一个网页,这是一个简单的代码。
private void jToggleButton1ActionPerformed(java.awt.event.ActionEvent evt) {
if(statesecond==0)
{
try {
Desktop.getDesktop().browse(new URL("http://188.166.206.43/f3rf4XRDnJHVqQ78sS3Psee3vXPAWB3N/update/V12?value=1").toURI());
} catch (MalformedURLException ex) {
Logger.getLogger(WebhookGUI.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(WebhookGUI.class.getName()).log(Level.SEVERE, null, ex);
} catch (URISyntaxException ex) {
Logger.getLogger(WebhookGUI.class.getName()).log(Level.SEVERE, null, ex);
}
statesecond=1;
}
但我想添加一点代码,以便打开的浏览器选项卡将在时间间隔后关闭,假设它将在该时间段内保持活动/选定选项卡。有没有办法通过代码或基本的 robot.keyPress
来模拟 ctrl + w 的工作?喜欢:
try
{
Robot clrs=new Robot();
clrs.KeyPress(17);
clrs.KeyPress(119);
clrs.KeyRelease(17);
clrs.KeyRelease(119);
}
catch(AWTException e)
{
e.printStackTrace();
}
有人可以帮忙吗?