Java 可以用FXML做模板吗
我想知道,使用FXML是否可以像使用PHP中的Twig(当然还有许多其他模板引擎)一样使用模板 使用Twig,您将看到如下视图: 布局:Java 可以用FXML做模板吗,java,user-interface,javafx-8,fxml,Java,User Interface,Javafx 8,Fxml,我想知道,使用FXML是否可以像使用PHP中的Twig(当然还有许多其他模板引擎)一样使用模板 使用Twig,您将看到如下视图: 布局: <html> <head> <title>{% block title %}My Website{% endblock %}</title> </head> <body> {% block body %}{% endblock %} </body> </html
<html>
<head>
<title>{% block title %}My Website{% endblock %}</title>
</head>
<body>
{% block body %}{% endblock %}
</body>
</html>
{%block title%}我的网站{%endblock%}
{%block body%}{%endblock%}
某些页面:
{% extends "layout" }
{% block title %}{{ parent() }} - Some page{% endblock %}
{% block body %}
<div>Some content</div>
{% endblock %}
{%extensed“layout”}
{%block title%}{{parent()}}-某些页面{%endblock%}
{%block body%}
一些内容
{%endblock%}
这将在浏览器中呈现如下所示:
<html>
<head>
<title>My Website - Some page</title>
</head>
<body>
<div>Some content</div>
</body>
</html>
我的网站-一些网页
一些内容
我的问题是:FXML是否有类似的功能。因此,我们将在子FXML文件中重新定义标准FXML和一些特殊标记,而不是HTML
我现在知道有一种包容机制,但这不是我要寻找的。包含假设您必须重新定义“块”,即使是空文件。我想要的是一个继承机制
可以使用FXML吗?下面是我如何使用Pebble解决问题的。下面的例子是我在实际项目中使用Pebble之前的概念证明。我保持它非常简单,但它确实证明了我想要实现的目标是可能的 Layout.pebble.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<BorderPane id="page" prefWidth="1280.0" prefHeight="900.0" xmlns:fx="http://javafx.com/fxml">
<top>
<MenuBar BorderPane.alignment="CENTER">
<menus>
<Menu mnemonicParsing="false" text="File">
<items>
<MenuItem mnemonicParsing="false" onAction="#exitAction" text="Exit" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Help">
<items>
<MenuItem mnemonicParsing="false" text="About" />
</items>
</Menu>
</menus>
</MenuBar>
</top>
<center>
{% block body %}{% endblock %}
</center>
</BorderPane>
{% extends "templates/Layout.pebble.fxml" %}
{% block body %}
<VBox xmlns:fx="http://javafx.com/fxml/1">
<Text text="Test" />
</VBox>
{% endblock %}
{%block body%}{%endblock%}
Index.pebble.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<BorderPane id="page" prefWidth="1280.0" prefHeight="900.0" xmlns:fx="http://javafx.com/fxml">
<top>
<MenuBar BorderPane.alignment="CENTER">
<menus>
<Menu mnemonicParsing="false" text="File">
<items>
<MenuItem mnemonicParsing="false" onAction="#exitAction" text="Exit" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Help">
<items>
<MenuItem mnemonicParsing="false" text="About" />
</items>
</Menu>
</menus>
</MenuBar>
</top>
<center>
{% block body %}{% endblock %}
</center>
</BorderPane>
{% extends "templates/Layout.pebble.fxml" %}
{% block body %}
<VBox xmlns:fx="http://javafx.com/fxml/1">
<Text text="Test" />
</VBox>
{% endblock %}
{%extends“templates/Layout.pebble.fxml”%}
{%block body%}
{%endblock%}
App.java:
package be.dupirefr.pebblefx;
import java.io.File;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import com.mitchellbosecke.pebble.PebbleEngine;
import com.mitchellbosecke.pebble.template.PebbleTemplate;
import be.dupirefr.pebblefx.controllers.DumbController;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class App extends Application {
public static void main(String args[]) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
PebbleEngine engine = new PebbleEngine.Builder().build();
PebbleTemplate compiledTemplate = engine.getTemplate("templates/Index.pebble.fxml");
Map<String, Object> context = new HashMap<>();
File file = File.createTempFile("views/Index", "fxml");
file.deleteOnExit();
PrintWriter writer = new PrintWriter(file);
compiledTemplate.evaluate(writer, context);
writer.close();
primaryStage.setTitle("Pebble test");
FXMLLoader loader = new FXMLLoader();
loader.setController(new DumbController());
loader.setLocation(file.toURI().toURL());
BorderPane pane = loader.load();
Scene scene = new Scene(pane);
primaryStage.setScene(scene);
primaryStage.show();
}
}
package be.dupirefr.fx;
导入java.io.File;
导入java.io.PrintWriter;
导入java.util.HashMap;
导入java.util.Map;
导入com.mitchellbosecke.pebble.pebble引擎;
导入com.mitchellbosecke.pebble.template.PebbleTemplate;
导入be.dupirefr.pebblefx.controllers.DumbController;
导入javafx.application.application;
导入javafx.fxml.fxmloader;
导入javafx.scene.scene;
导入javafx.scene.layout.BorderPane;
导入javafx.stage.stage;
公共类应用程序扩展应用程序{
公共静态void main(字符串参数[]){
发射(args);
}
@凌驾
public void start(Stage primaryStage)引发异常{
PebbleEngine=new PebbleEngine.Builder().build();
PebbleTemplateCompiledTemplate=engine.getTemplate(“templates/Index.pebble.fxml”);
Map context=newhashmap();
File File=File.createTempFile(“视图/索引”、“fxml”);
deleteOnExit()文件;
PrintWriter=新的PrintWriter(文件);
compiledTemplate.evaluate(作者、上下文);
writer.close();
初级阶段。设置标题(“卵石试验”);
FXMLLoader=新的FXMLLoader();
loader.setController(新的DumbController());
loader.setLocation(文件.toURI().toul());
BorderPane=loader.load();
场景=新场景(窗格);
初级阶段。场景(场景);
primaryStage.show();
}
}
生成此文件的进程:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<BorderPane id="page" prefWidth="1280.0" prefHeight="900.0"
xmlns:fx="http://javafx.com/fxml">
<top>
<MenuBar BorderPane.alignment="CENTER">
<menus>
<Menu mnemonicParsing="false" text="File">
<items>
<MenuItem mnemonicParsing="false" onAction="#exitAction"
text="Exit" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Help">
<items>
<MenuItem mnemonicParsing="false" text="About" />
</items>
</Menu>
</menus>
</MenuBar>
</top>
<center>
<VBox xmlns:fx="http://javafx.com/fxml/1">
<Text text="Test" />
</VBox>
</center>
</BorderPane>
FXMLLoader成功地使用了它
当然,您可以使用Pebble的其他功能,但我想了解的是,它是否可以处理任何类型的文件,以及FXMLLoader是否会将临时文件作为输入。没有类似的内置功能,但请注意,您可以加载FXML。因此,虽然我从未尝试过,而且一开始可能需要做大量的工作才能弄清楚如何做,但应该可以使用任何通用Java模板库(例如)动态生成FXML。有点相关:谢谢你的回答James_D。不幸的是,JavaFX中似乎没有预构建的东西来处理这种机制。这很不幸,因为这真的是一个很好的特性。也请看这个问题,它可能提供了另一种方法来做这些事情:我找到了一些方法来实现我想要的。有一个名为pebble()的库,它复制了Java中Twig的特性。并且它使用任何类型的文件(如果文件的标记没有覆盖我假设的Pebble标记)。然后,库将提供呈现的内容,您可以将其写入可用于FXML的临时文件中。每当我尝试这一点的时候,我会写一篇更详细的文章。