FXML:替代用途 [英] FXML: Alternate Uses

查看:58
本文介绍了FXML:替代用途的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用FXML将非GUI对象加载到内存中?例如,我正在为学校创建一个简单的投票"软件.它所需要的只是选修职位"列表和相应的候选人,以及诸如这些职位和候选人的属性"集之类的其他东西.

It is possible to use FXML to load non-GUI objects into memory? For example, I am creating a simple "voting" software for my school. All it needs is the list of "elective-posts" and the corresponding candidates along with other things like sets of "properties" of the posts and candidates.

我要做的是,将数据写入FXML文件,然后使用FXMLLoader加载.

What I want to do is, that I write the data in a FXML file and then load it using a FXMLLoader.

推荐答案

是的,FXML可用于创建任意对象.您可以像定义任何GUI对象一样定义对象.您只需要确保:

Yes, FXML can be used to create arbitrary objects. You'd define the objects just like you would any GUI object. You just have to make sure that:

  • 您遵循Java getter/setter命名约定
  • 如果您有一个名为setField的二传手,则在FXML中该属性将为field="value"
  • 除非您使用JavaFX属性,否则绑定语法将不起作用
  • 如果没有设置方法,但可以通过构造函数设置字段(或者没有默认构造函数),则必须使用
  • You follow Java getter/setter naming conventions
  • If you have a setter named setField then in the FXML the attribute would be field="value"
  • Unless you are using JavaFX properties the binding syntax won't work
  • If you don't have a setter but you can set the field via a constructor (or you don't have a default constructor) then you have to annotate the constructor parameters with NamedArg

这是一个小例子.

Animal.java

package com.example;

import javafx.beans.NamedArg;

public class Animal {

    private final String name;
    private boolean housePet;

    public Animal(@NamedArg("name") String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public boolean isHousePet() {
        return housePet;
    }

    public void setHousePet(boolean housePet) {
        this.housePet = housePet;
    }

    @Override
    public String toString() {
        return "Animal[name=" + name + ", housePet=" + housePet + "]";
    }

}

Main.java

package com.example;

import java.io.IOException;
import java.util.List;
import javafx.fxml.FXMLLoader;

public class Main {

    public static void main(String[] args) throws IOException {
        List<Animal> list = FXMLLoader.load(Main.class.getResource("Main.fxml"));
        list.forEach(System.out::println);
    }

}

Main.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import com.example.Animal?>
<?import java.util.ArrayList?>

<ArrayList xmlns="http://javafx.com/javafx/10.0.2" xmlns:fx="http://javafx.com/fxml/1">

    <Animal name="Cat" housePet="true"/>
    <Animal name="Dog" housePet="true"/>
    <Animal name="Bear" housePet="false"/>
    <Animal name="Wolf" housePet="false"/>

    <!-- Another way of declaring an Animal -->

    <Animal>
        <name>Snake</name>
        <housePet>true</housePet>
    </Animal>

</ArrayList>

运行Main会显示以下内容:

Animal[name=Cat, housePet=true]
Animal[name=Dog, housePet=true]
Animal[name=Bear, housePet=false]
Animal[name=Wolf, housePet=false]
Animal[name=Snake, housePet=true]

这篇关于FXML:替代用途的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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