使用 JavaFX 引导程序 [英] Bootstrap with JavaFX

查看:34
本文介绍了使用 JavaFX 引导程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 netbeans 在 java fxml 项目中创建 GUI.我想使用引导程序来设置 gui 的样式,但我注意到 javafx 中的所有内容都以 fx- 为前缀.有没有办法让引导程序无论如何都能为我的项目工作?bootstrap 甚至可以与 javafx 一起使用吗?

解决方案

在 JavaFX WebView 中渲染 Bootstrap

Bootstrap 是一个基于 HTML 的框架.

所以要在JavaFX中使用Bootstrap,使用JavaFX的HTML渲染组件.

On Fextile(自举寻找原生 JavaFX 控件)

<块引用>

为什么不直接移植 bootstrap.css 以符合 javafx 命名约定?

因为:

  • 引导程序不仅仅是 css,它是一个完全响应式的 UI 框架,具有基于 JavaScript 的主动控件和用户扩展机制.
  • 在 WebView 中呈现将在 JavaFX 中呈现与在 Web 浏览器中完全相同的引导程序 html,那么当您已经拥有无需额外工作即可完美运行的内容时,为什么还要移植?
  • 这是一个不断变化的目标,bootstrap.css 主干项目得到了数百名开发人员的许多贡献,但如果您只选择了较小的 bootstrap 功能子集,则很难用自己开发的 JavaFX 端口跟上它的步伐保持同步会更容易.

仍然可以进行移植(如 Philippe 的回答中所链接),这就是 Takayuki Okazaki 在他的 Fextile 项目:类似于 JavaFX 的 UI 框架的 Twitter Bootstrap.通过 JavaFX CSS 将主题应用到您的应用程序,就像 Twitter Bootstrap 一样.".不要期望与 HTML 中的 bootstrap 完全匹配,但它应该允许不使用 HTML 的 JavaFX 控件与使用 HTML 中的 bootstrap 实现的效果非常接近.

您还可以创建一个混合应用程序,其中 UI 的某些部分来自带有引导程序的 HTML,而另一些部分则使用 Fextile 从 JavaFX 控件呈现.如果您将此方法应用于此答案中的示例应用程序,那么 JavaFX 按钮progress"、jumbotron"等将看起来像它们的 HTML bootstrap 对应物,使整个应用程序具有更加一致的外观和感觉.

另外,请注意JavaFX的基础样式有一个类似的项目,如本Oracle JavaFX 论坛帖子.该项目模仿了原生 JavaFX 控件的基本Foundation 外观.对于某些用途,采用 Foundation 样式可能比 Bootstrap 样式更合适,因为项目的范围比 Bootstrap 小(据我所知).

这是关于如何创建 Foundation 样式的问答(来自 Oracle JavaFX 论坛帖子)(因此有人可以相对了解扩展 Fextile 以获取其他 Bootstrap 样式功能所涉及的内容).请注意,问答有点旧,从那以后添加了 CSS 分析器到 SceneBuilder:

<块引用>

1) 这项工作有多难?

完全没有:整个体验非常愉快,而且很容易做到.这是我的第一个 JavaFX 应用程序(具有实时功能的地图样式编辑器)预览)

2) 是否非常耗时?

否:使用预览版 ScenceBuilder 1.1,样式会在fly - SceneBuilder 可以使用内置的 CSS 编辑器,但仅此而已只是次要:反正工作流程很简单

3) 对于一个简单的移植,你认为你需要任何设计技巧吗?全部,或者任何知道一点的人都可以真正做到这一点css/html/javafx?

任何人都可以这样做:我的背景是服务器端代码 - 我不这样做前端的方式很多 - 我非常了解 JS 和 HTML,但我的CSS给我留下了很多想要的东西:所以基本上如果我能做到......

4) javafx css 语法和 html css 的区别语法是一个主要的问题还是不是一个真正的问题?

一旦我习惯了,虽然我一直保持忘记添加 '-fx-' 和 -fx-text-fill 我总是输入-fx-text-color ...

5) 同样,html 之间缺乏一对一的对应关系文档标签(例如标题标签)和 JavaFX 使这复杂化了吗?

没有

6) JavaFX 8 中即将推出的富文本支持是否会简化(或使可能)更多这些类型的端口?

我需要看看这个:正如我所说,我是一个完整的初学者JavaFX,所以我仍在追赶当前的实现.

bootstrap 样式会非常好:很多我已经当我告诉他们它是 Java 而不是 Java 时,显示该应用程序非常惊讶一个嵌入式网络应用.

I am creating a GUI in a java fxml project using netbeans. I wanted to use bootstrap to style the gui but I have noticed that everything in javafx is prefixed with fx-. Is there still a way to get bootstrap to work anyway for my project? Does bootstrap even work with javafx?

解决方案

Rendering Bootstrap inside a JavaFX WebView

Bootstrap is an HTML based framework.

So to use Bootstrap in JavaFX, use JavaFX's HTML rendering component WebView to render Bootstrap HTML/CSS and JavaScript.

Sample Application

Sample application performing a basic integration of Bootstrap and a JavaFX UI.

The JavaFX buttons on the top of the screen navigate around a WebView page to render different kinds of Bootstrap components.

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ToolBar;
import javafx.scene.layout.VBox;
import javafx.scene.web.WebView;
import javafx.stage.Stage;

public class BaseJump extends Application {
    private static final String BOOTSTRAP_PREFIX = "http://getbootstrap.com/components/#";

    private enum Anchor { progress, jumbotron, badges, pagination }

    @Override public void start(Stage stage) throws Exception {
        final WebView webview = new WebView();

        final ToolBar nav = new ToolBar();
        for (Anchor anchor : Anchor.values()) {
            nav.getItems().add(
                new NavButton(
                    anchor,
                    webview
                )
            );
        }

        VBox layout = new VBox();
        layout.getChildren().addAll(
            nav,
            webview
        );

        Scene scene = new Scene(layout);
        stage.setScene(scene);
        stage.show();
    }

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

    private class NavButton extends Button {
        public NavButton(final Anchor anchor, final WebView webview) {
            setText(anchor.toString());

            setOnAction(new EventHandler<ActionEvent>() {
                @Override
                public void handle(ActionEvent event) {
                    webview.getEngine().load(BOOTSTRAP_PREFIX + anchor.toString());
                }
            });
        }
    }
}

Additional, slightly unrelated question

Is it possible to intercept button click events from a web view?

Yes. You can attach click handlers in Java code through the w3c DOM API access WebEngine provides. See the WebEngine documentation for details:

Access to Document Model

The WebEngine objects create and manage a Document Object Model (DOM) for their Web pages. The model can be accessed and modified using Java DOM Core classes. The getDocument() method provides access to the root of the model. Additionally DOM Event specification is supported to define event handlers in Java code.

The following example attaches a Java event listener to an element of a Web page. Clicking on the element causes the application to exit:

EventListener listener = new EventListener() {
    public void handleEvent(Event ev) {
        Platform.exit();
    }
};

Document doc = webEngine.getDocument();
Element el = doc.getElementById("exit-app");
((EventTarget) el).addEventListener("click", listener, false);

However, for me often is is easier to handle interfacing with w3c documents using JavaScript (specifically jQuery), rather than Java. Here is an example of issuing from Java code, a jQuery call to provide click handlers in a WebView.

On Fextile (Bootstrap look for native JavaFX controls)

Why not just port bootstrap.css to conform to the javafx naming conventions?

Because:

  • There is more to bootstrap than just the css, it's a full responsive UI framework with JavaScript based active controls and a user extension mechanism.
  • Rendering in WebView will render the bootstrap html in JavaFX exactly the same as if it were in a web browser, so why port when you already have something which will work perfectly with no extra effort?
  • It's a moving target, the bootstrap.css trunk project gets many contributions from hundreds of developers, it would be difficult to keep up with that with a home-grown JavaFX port, though if you selected just a smaller subset of bootstrap features keeping in sync would be easier.

Still, it is possible to do the port (as linked in Philippe's answer), and that is what Takayuki Okazaki has created in his Fextile project: "Twitter Bootstrap like UI framework for JavaFX. Apply themes to your application just like Twitter Bootstrap via JavaFX CSS.". Don't expect an exact match with bootstrap in HTML, but it should allow your JavaFX controls which don't use HTML to have a pretty close look to what you would achieve with bootstrap in HTML.

You could also create a hybrid application, where some parts of the UI are from HTML with bootstrap and some are rendered from JavaFX controls using Fextile. If you applied such an approach to the sample application in this answer, then the JavaFX buttons "progress", "jumbotron", etc. would look like their HTML bootstrap counterparts, giving the whole application a more consistent look and feel.

Also, note that there is a similar project for Foundation styles for JavaFX, as announced in this Oracle JavaFX forum post. This project mimics the basic Foundation look for native JavaFX controls. For some usages, adopting Foundation styles may be more appropriate than Bootstrap styles as the project is smaller in scope than Bootstrap (as far as I know).

Here is a Q&A (from the Oracle JavaFX forum post) on how to creating the Foundation style (so somebody can get a relative idea of what is involved for extending Fextile for additional Bootstrap style features). Note that the Q&A is a little old and since then a CSS analyzer has been added to SceneBuilder:

1) How difficult was this work?

No at all: the whole experience was very pleasant and very easy to do. This is my first JavaFX app (a map style editor with real time preview)

2) Was it very time consuming?

No: using the preview ScenceBuilder 1.1 the styles are updated on the fly - the SceneBuilder could do with a built in CSS editor, but that's only minor: the workflow was quite simple anyway

3) For a simple port do you think that you need any design skills at all, or could anybody have really done this who knows a bit of css/html/javafx?

Anyone can do this: my background is server side code - I don't do much in the way of front ends - I know JS and HTML very well but my CSS leaves a lot to me desired: so basically if I can do it ...

4) Was the difference between the javafx css syntax and the html css syntax a major pain or not really an issue?

Once I got used to it, made no difference although I do keep forgetting to add '-fx-' and the -fx-text-fill I always type as -fx-text-color ...

5) Similarly did a lack of a one-to-one correspondence between html document tags (e.g. the header tags) and JavaFX complicate this?

No

6) Will the upcoming rich text support in JavaFX 8 simplify (or make possible) more of these kinds of ports?

I need to have a look at this: as I said I'm a complete beginner with JavaFX so I'm still catching up on the current implementation.

The bootstrap styles would be really nice: a lot of people who I've showed the app to are quite amazed when I tell them its Java and not an embedded web app.

这篇关于使用 JavaFX 引导程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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