javafx - 如何使用FXML创建Integer微调器

标签 javafx spinner fxml

我正在尝试创建Integer Spinner,但是相反,创建了一个Double

测试类

package com.neonorb.test;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Insets;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

public class Test extends Application{
    @Override
    public void start(Stage stage) throws Exception {
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("Demo.fxml"));
        Parent root = fxmlLoader.load();
        Scene scene = new Scene(root);
        stage.setScene(scene);
        stage.show();
    }

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

DemoController.class
package com.neonorb.test;

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Spinner;

import java.net.URL;
import java.util.ResourceBundle;

public class DemoController implements Initializable{
    @FXML
    private Spinner<Integer> spinner;

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        System.out.println(spinner.getValue());
    }
}

Demo.fxml
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.control.Spinner?>
<?import java.lang.Integer?>
<BorderPane xmlns:fx="http://javafx.com/fxml" fx:controller="com.neonorb.test.DemoController">
    <Spinner fx:id="spinner" min="1" initialValue="1" amountToStepBy="1">
        <max>
            <Integer fx:constant="MAX_VALUE"/>
        </max>
    </Spinner>
</BorderPane>

当我执行它时,它会输出1.0,这就是我所知道的,它是Double微调器。

我猜这是怎么回事FXMLLoaderSpinner选择了错误的构造函数。我该如何选择Integer

最佳答案

问题

您需要为微调器设置一个值工厂。否则,您将面临强制类型。如果无法在此处进行设置,则可以定义将由静态valueOf()调用的Integer值。

FFX的JavaFX简介关于Type Coercion的说明:

Type Coercion

FXML uses "type coercion" to convert property values to the appropriate type as needed. Type coercion is required because the only data types supported by XML are elements, text, and attributes (whose values are also text). However, Java supports a number of different data types including built-in primitive value types as well as extensible reference types.

The FXML loader uses the coerce() method of BeanAdapter to perform any required type conversions. This method is capable of performing basic primitive type conversions such as String to boolean or int to double, and will also convert String to Class or String to Enum. Additional conversions can be implemented by defining a static valueOf() method on the target type.



工厂解决方案

已经存在一个IntegerSpinnerValueFactory。因为它是SpinnerValueFactory的嵌套类,所以必须在标记名称中使用点。

共有三个构造函数,您可以设置min/max和min/max/initialValue和min/max/initialValue/amountToStepBy。这是通过将其设置为属性来完成的。

Demo.fxml
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.util.* ?>
<?import javafx.scene.*?>
<?import javafx.scene.control.* ?>
<?import javafx.scene.layout.* ?>


<BorderPane xmlns:fx="http://javafx.com/fxml/1" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.40" fx:controller="demoapp.DemoController">
  <center>
    <Spinner fx:id="spinner" BorderPane.alignment="CENTER" >
      <valueFactory>
        <SpinnerValueFactory.IntegerSpinnerValueFactory min="0" max="10"/>
      </valueFactory>
    </Spinner>
  </center>
</BorderPane>

没有工厂的解决方案

您还可以定义两个变量并将它们用作静态valueOf()。如上面引用静态staticOfOf()方法所述。因此,您的FXMLLoader不必猜测您可能表示的类型。它使用int值调用构造函数。

Demo.fxml
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.util.* ?>
<?import javafx.scene.*?>
<?import javafx.scene.control.* ?>
<?import javafx.scene.layout.* ?>


<BorderPane xmlns:fx="http://javafx.com/fxml/1" fx:controller="demoapp.DemoController" xmlns="http://javafx.com/javafx/8.0_40" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0"  >
  <center>
    <fx:define>
      <Integer fx:id="min" fx:value="0"/>
      <Integer fx:id="max" fx:value="10"/>
    </fx:define>
    <Spinner fx:id="spinner" BorderPane.alignment="CENTER" min="$min" max="$max">
    </Spinner>
  </center>
</BorderPane>

关于javafx - 如何使用FXML创建Integer微调器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31248983/

相关文章:

mysql - javafx scenebuilder中带有tableview的分页

java - 缺少 JavaFX 应用程序类示例.Main

android - 自定义微调器字体

安卓。什么是android :popupPromptView?

java - 如何使 FXML 生成的按钮起作用

java - 如何从 java 类获取 FXML 文件中定义的按钮位置

JavaFX 8 FXML TextField(可能是其他变量)始终为 null

JavaFX 对话框通信

android - 将 Spinner 的每个选择添加到 TextView

java - 在JavaFX中在矩形边框上画圆