关闭浏览器窗口中使用Java code [英] Close browser window using java code

查看:212
本文介绍了关闭浏览器窗口中使用Java code的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我应该如何关闭使用Java code打开的浏览器窗口。我已经找到一种方法,先找到流程然后结束该进程。有没有更好的办法?我已初步使用下面的code打开浏览器。我工作在CentOS的。

 字符串的URL =htt​​p://192.168.40.174/test15.html;
运行运行时间=调用Runtime.getRuntime();
的Runtime.exec(在/ usr /斌/火狐-new窗口+网址);


解决方案

我是想达到类似的事情,而不关心太多这将打开浏览器。我来进行的跨基于Java FX解决方案:

 公共类MyBrowser扩展应用{私人字符串URL =htt​​p://stackoverflow.com/questions/29842930/close-browser-window-using-java-$c$c;公共静态无效的主要(字串[] args){
    启动(参数);
}@覆盖
公共无效启动(阶段阶段)抛出异常{    的WebView的WebView =新的WebView();
    webview.getEngine()负载(URL)。
    webview.set prefSize(1800,1000);    stage.setScene(新场景(的WebView));
    stage.show();    //stage.close();}

}

当然,如果你调用的close()这样一来,你就不会真正看到了嵌入式浏览器窗口。它应该在code,例如另一部分被称为响应于按钮按下。

How should i close an opened browser window using java code. I have found a way to first find the process then end that process. Is there any better way? I have opened the browser initially using the below code. I am working in CentOS.

String url = "http://192.168.40.174/test15.html";
Runtime runtime = Runtime.getRuntime();
runtime.exec("/usr/bin/firefox -new-window " + url);

解决方案

I was trying to achieve a similar thing, without caring too much which browser will open. I come accross a solution based on Java FX:

public class MyBrowser extends Application {

private String url = "http://stackoverflow.com/questions/29842930/close-browser-window-using-java-code";

public static void main(String[] args) {
    launch(args);
}

@Override
public void start(Stage stage) throws Exception {

    WebView webview = new WebView();
    webview.getEngine().load(url);
    webview.setPrefSize(1800, 1000);

    stage.setScene(new Scene(webview));
    stage.show();

    //stage.close();

}

}

Of course if you call close() this way, you will not really see the embedded browser window. It should be called in another part of the code, e.g. in response to a button push.

这篇关于关闭浏览器窗口中使用Java code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆