简体   繁体   English

你如何在 vscode 中创建一个 .fxml 文件?

[英]How do you create a .fxml file in vscode?

(I am very new to coding so let me know if any wording is too vague). (我对编码很陌生,所以如果有任何措辞太模糊,请告诉我)。 I am using Visual Studio Code 1.67.2.我正在使用 Visual Studio Code 1.67.2。 I did not use Maven or any other similar tools, I just manually pointed the JavaFX lib folder to my project.我没有使用 Maven 或任何其他类似工具,我只是手动将 JavaFX lib 文件夹指向我的项目。 I successfully was able to "connect" a css file to my main.java application by creating a .css file and then using scene.getStylesheets().addAll("Main.css").通过创建一个 .css 文件然后使用 scene.getStylesheets().addAll("Main.css"),我成功地将一个 css 文件“连接”到我的 main.java 应用程序。 However, I do not see fxml as an option in the language drop down like I did with css (I just see xml).但是,我没有将 fxml 视为语言下拉菜单中的一个选项,就像我对 css 所做的那样(我只看到 xml)。 How can I create a .fxml file?如何创建 .fxml 文件? Every tutorial I can find already has a blank .fxml file in the project folder ready to be edited.我能找到的每个教程在项目文件夹中都有一个空白的 .fxml 文件可供编辑。

Scene Builder场景生成器

The easiest solution is to download and use Scene Builder , which is a WYSIWYG editor for FXML files.最简单的解决方案是下载并使用Scene Builder ,它是 FXML 文件的所见即所得编辑器 It can create new FXML files for you, and you rarely, if ever, have to deal with editing the FXML file yourself.它可以为您创建新的 FXML 文件,而且您很少(如果有的话)必须自己处理编辑 FXML 文件。 You just create the UI that you want, and Scene Builder will write the FXML file for you.您只需创建所需的 UI,Scene Builder 就会为您编写 FXML 文件。


Create & Write FXML File Manually手动创建和写入 FXML 文件

There may be an VS Code extension for this, but I'm not currently aware of one.可能有一个 VS Code 扩展,但我目前不知道。 Though it is not hard to "manually" create an FXML file.尽管“手动”创建 FXML 文件并不难。 Just go to create a new file, type in the name of the file, and then add a .fxml extension.只需创建一个新文件,输入文件名,然后添加.fxml扩展名。 You'll end up with a completely blank FXML file.您将得到一个完全空白的 FXML 文件。 Then you just need to add two things to make it a minimally valid FXML file:然后你只需要添加两件事使它成为一个最低限度有效的 FXML 文件:

  1. An XML header. XML 标头。
  2. A root element (with namespaces + any needed import instructions).一个根元素(带有命名空间 + 任何需要的导入指令)。
<!-- XML header -->
<?xml version="1.0" encoding="UTF-8"?>

<!-- Add/remove/change import instructions as needed -->
<?import javafx.scene.layout.StackPane?>

<!-- Change root element type as needed -->
<StackPane xmlns="http://javafx.com/javafx" xmlns:fx="http://javafx.com/fxml">
  <!-- Define properties and rest of scene graph -->
</StackPane>

Note: Keep in mind that an FXML file is syntactically just an XML file.注意:请记住,FXML 文件在语法上只是一个 XML 文件。

And then you can continue to modify the FXML file as needed.然后您可以根据需要继续修改 FXML 文件。 It may help to read Introduction to FXML in order to understand what you're doing.阅读FXML 简介可能有助于了解您在做什么。

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

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