简体   繁体   English

如何使用 HTML 和 CSS 作为 Java 应用程序 GUI?

[英]How to use HTML and CSS as a Java application GUI?

I want to design new Git client with a clean GUI.我想用干净的 GUI 设计新的 Git 客户端。

Is it possible to use the power of HTML, CSS and JavaScript in a java application?是否可以在 Java 应用程序中使用 HTML、CSS 和 JavaScript 的强大功能?

I would like to use Java + JGit for models, Java for controllers and HTML + CSS + JavaScript for views.我想对模型使用 Java + JGit,对控制器使用 Java,对视图使用 HTML + CSS + JavaScript。

I don't want a client-server model.我不想要客户端 - 服务器模型。 I would like to integrate Java and HTML nicely.我想很好地集成 Java 和 HTML。 A DOM event would fire events directly to a Java controller. DOM 事件会直接向 Java 控制器触发事件​​。 This way it would be possible to create rich offline application.这样就可以创建丰富的离线应用程序。

You can embed web browser component into your Java Swing/JavaFX Desktop application that displays GUI built with HTML5+CSS+JavaScript.您可以将 Web 浏览器组件嵌入到您的 Java Swing/JavaFX 桌面应用程序中,该应用程序显示使用 HTML5+CSS+JavaScript 构建的 GUI。 You can see an article that describes how to do this at https://jxbrowser-support.teamdev.com/docs/tutorials/cross-desktop-apps.html您可以在https://jxbrowser-support.teamdev.com/docs/tutorials/cross-desktop-apps.html看到一篇介绍如何执行此操作的文章

One of the Java Swing/JavaFX libraries that allows embedding Chromium into Java applications is JxBrowser .允许将 Chromium 嵌入 Java 应用程序的 Java Swing/JavaFX 库之一是JxBrowser Using JxBrowser API you can load any web page and work with its DOM and JavaScript.使用 JxBrowser API,您可以加载任何网页并使用其 DOM 和 JavaScript。 You can even call Java methods from JavaScript code and vice versa.您甚至可以从 JavaScript 代码调用 Java 方法,反之亦然。 For example:例如:

import com.teamdev.jxbrowser.chromium.Browser;
import com.teamdev.jxbrowser.chromium.JSFunctionCallback;
import com.teamdev.jxbrowser.chromium.JSObject;
import com.teamdev.jxbrowser.chromium.JSValue;
import com.teamdev.jxbrowser.chromium.events.FinishLoadingEvent;
import com.teamdev.jxbrowser.chromium.events.LoadAdapter;

public class JavaScriptJavaSample {
    public static void main(String[] args) {
        Browser browser = new Browser();
        browser.addLoadListener(new LoadAdapter() {
            @Override
            public void onFinishLoadingFrame(FinishLoadingEvent event) {
                if (event.isMainFrame()) {
                    Browser browser = event.getBrowser();
                    JSObject window = (JSObject)
                            browser.executeJavaScriptAndReturnValue("window");
                    window.setProperty("MyFunction", new JSFunctionCallback() {
                        @Override
                        public Object invoke(Object... args) {
                            for (Object arg : args) {
                                System.out.println("arg = " + arg);
                            }
                            return "Hello!";
                        }
                    });
                    JSValue returnValue = browser.executeJavaScriptAndReturnValue(
                            "MyFunction('Hello JxBrowser!', 1, 2, 3, true);");
                    System.out.println("return value = " + returnValue);
                }
            }
        });
        browser.loadURL("about:blank");
    }
}

It's not really feasible.这不是真的可行。 Rich clients in Java are done using Swing or SWT. Java 中的富客户端是使用 Swing 或 SWT 完成的。

If you want to use HTML/CSS for your user interface, you need to use the server/client model.如果你想在你的用户界面中使用 HTML/CSS,你需要使用服务器/客户端模型。 It can be as simple as creating a local server and launching a browser that connects to it, but it would still be that model.它可以像创建一个本地服务器并启动一个连接到它的浏览器一样简单,但它仍然是那个模型。

If you absolutely need to have HTML/CSS as your UI framework and can't go to a server/client model, your best bet is probably looking at something like Google Native Client, but that uses C/C++ bindings on the backend.如果您绝对需要将 HTML/CSS 作为您的 UI 框架并且无法使用服务器/客户端模型,那么您最好的选择可能是查看诸如 Google Native Client 之类的东西,但它在后端使用 C/C++ 绑定。 I haven't used Native Client so I can't personally give much more information on that front.我没有使用过 Native Client,所以我个人无法在这方面提供更多信息。

Edit to add:编辑添加:

One option is to embed a native browser into your Swing app using something like: http://djproject.sourceforge.net/ns/一种选择是使用以下内容将本机浏览器嵌入到您的 Swing 应用程序中: http : //djproject.sourceforge.net/ns/

There are some pure Java HTML renderers, however, they most likely won't be fully HTML5/CSS3 compliant, let alone possibly have Javascript bugs as well.有一些纯 Java HTML 渲染器,但是,它们很可能不会完全符合 HTML5/CSS3,更不用说可能还有 Javascript 错误了。

See here for some of those options: Pure Java HTML viewer/renderer for use in a Scrollable pane请参阅此处了解其中的一些选项: Pure Java HTML viewer/renderer for use in a Scrollable pane

Like @Reverand Gonzo says, you will need some form of server/client.就像@Reverand Gonzo 所说的,您将需要某种形式的服务器/客户端。 But you could easily embed a Jetty server into a Java app and then use GWT for your client code .但是您可以轻松地将 Jetty 服务器嵌入到 Java 应用程序中,然后将 GWT 用于您的客户端代码

You can bring in the power of HTML,CSS,JavaScript into your Swing app using JFXPanel to embed JavaFX WebView.您可以使用JFXPanel嵌入 JavaFX WebView,将 HTML、CSS、JavaScript 的强大功能引入您的 Swing 应用程序。 Have a look at the SimpleSwingBrowser demo in this link: https://docs.oracle.com/javase/8/javafx/interoperability-tutorial/swing-fx-interoperability.htm在此链接中查看 SimpleSwingBrowser 演示: https ://docs.oracle.com/javase/8/javafx/interoperability-tutorial/swing-fx-interoperability.htm

WebView allows to call JavaScript functions from Java and vice versa. WebView 允许从 Java 调用 JavaScript 函数,反之亦然。 It is also a nice way to enhance your legacy Java app with web technologies.这也是使用 Web 技术增强遗留 Java 应用程序的好方法。

JavaFX 2.2 brought this functionality to providing a user interface component (GUI) that has web view and full browsing functionality. JavaFX 2.2 带来了此功能,以提供具有 Web 视图和完整浏览功能的用户界面组件 (GUI)。

For more details, see Adding HTML Content to JavaFX Applications .有关更多详细信息,请参阅向 JavaFX 应用程序添加 HTML 内容

Use Angular.js with HTML and rest of the things as same in Java, just use classes for business logic, no need to write code for awt/swing. Angular.js 和 HTML 一起使用,其余的东西和 Java 一样,只用类来处理业务逻辑,不需要为 awt/swing 编写代码。 Angular with spring boot are rapid development in Java for webapp with less code in Java without swing use to create best webapp . Angular with spring boot 是用 Java 快速开发 webapp 的,Java 中的代码更少,无需使用 Swing 来创建最好的 webapp。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM