简体   繁体   中英

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. I did not use Maven or any other similar tools, I just manually pointed the JavaFX lib folder to my project. 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"). However, I do not see fxml as an option in the language drop down like I did with css (I just see xml). How can I create a .fxml file? Every tutorial I can find already has a blank .fxml file in the project folder ready to be edited.

Scene Builder

The easiest solution is to download and use Scene Builder , which is a WYSIWYG editor for FXML files. It can create new FXML files for you, and you rarely, if ever, have to deal with editing the FXML file yourself. You just create the UI that you want, and Scene Builder will write the FXML file for you.


Create & Write FXML File Manually

There may be an VS Code extension for this, but I'm not currently aware of one. Though it is not hard to "manually" create an FXML file. Just go to create a new file, type in the name of the file, and then add a .fxml extension. You'll end up with a completely blank FXML file. Then you just need to add two things to make it a minimally valid FXML file:

  1. An XML header.
  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.

And then you can continue to modify the FXML file as needed. It may help to read Introduction to FXML in order to understand what you're doing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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