如何在微调器中设置3个值

时间:2015-06-17 08:53:33

标签: spinner javafx-8 fxml netbeans-8 jspinner

我希望时间像上午02:25:12一小时,分钟,秒一个旋转器。我怎样才能做到这一点?我只做了一个像

这样的值
<Spinner fx:id="spinner" layoutX="350.0" layoutY="10.0" initialValue="60"        
         max="120"  prefHeight="25.0" prefWidth="60.0" />

显示错误,如

  

jaavaFx.scene.control.Spinner不支持min / max / intial propery

但它运作正常。现在我想做3个值。

1 个答案:

答案 0 :(得分:3)

正如@UlukBiy在评论中所说,你可能想在这里使用三个微调器。你可能还想实现“环绕”,这样如果你增加超过59的秒数,它会重置为0并且分钟增量等等。

另外,要设置最小值和最大值,请将其设置在SpinnerValueFactory上,而不是Spinner本身。请参阅SpinnerSpinnerValueFactorySpinnerValueFactory.IntegerSpinnerValueFactory的Javadoc。

这是一个完整的例子(在FXML中):

TimeSpinner.fxml:

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

<?import javafx.scene.layout.StackPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.control.Spinner?>
<?import javafx.scene.control.SpinnerValueFactory.IntegerSpinnerValueFactory ?>

<HBox  xmlns:fx="http://javafx.com/fxml/1" fx:controller="TimeSpinnerController">
    <Spinner fx:id="hourSpinner" prefWidth="60">
        <valueFactory>
            <javafx.scene.control.SpinnerValueFactory.IntegerSpinnerValueFactory fx:id="hourValueFactory">
                <min>0</min>
                <max>23</max>
                <wrapAround>true</wrapAround>
            </javafx.scene.control.SpinnerValueFactory.IntegerSpinnerValueFactory>
        </valueFactory>
    </Spinner>
    <Spinner fx:id="minuteSpinner" prefWidth="60">
        <valueFactory>
            <javafx.scene.control.SpinnerValueFactory.IntegerSpinnerValueFactory fx:id="minuteValueFactory">
                <min>0</min>
                <max>59</max>
                <wrapAround>true</wrapAround>
            </javafx.scene.control.SpinnerValueFactory.IntegerSpinnerValueFactory>>
        </valueFactory>
    </Spinner>
    <Spinner fx:id="secondSpinner" prefWidth="60">
        <valueFactory>
            <javafx.scene.control.SpinnerValueFactory.IntegerSpinnerValueFactory fx:id="secondValueFactory">
                <min>0</min>
                <max>59</max>
                <wrapAround>true</wrapAround>
            </javafx.scene.control.SpinnerValueFactory.IntegerSpinnerValueFactory>>
        </valueFactory>
    </Spinner>
</HBox>

TimeSpinnerController:

import java.time.Duration;

import javafx.beans.binding.Bindings;
import javafx.beans.property.ReadOnlyObjectProperty;
import javafx.beans.property.ReadOnlyObjectWrapper;
import javafx.fxml.FXML;
import javafx.scene.control.Spinner;



public class TimeSpinnerController {

    @FXML
    private Spinner<Integer> hourSpinner ;
    @FXML
    private Spinner<Integer> minuteSpinner ;
    @FXML
    private Spinner<Integer> secondSpinner ;

    private ReadOnlyObjectWrapper<Duration> time = new ReadOnlyObjectWrapper<>();

    public void initialize() {
        minuteSpinner.valueProperty().addListener((obs, oldValue, newValue) -> {
            if (oldValue.intValue() == 59 && newValue.intValue() == 0) {
                hourSpinner.increment();
            }
            if (oldValue.intValue() == 0 && newValue.intValue() == 59) {
                hourSpinner.decrement();
            }
        });

        secondSpinner.valueProperty().addListener((obs, oldValue, newValue) -> {
            if (oldValue.intValue() == 59 && newValue.intValue() == 0) {
                minuteSpinner.increment();
            }
            if (oldValue.intValue() == 0 && newValue.intValue() == 59) {
                minuteSpinner.decrement();
            }
        });

        time.bind(Bindings.createObjectBinding(this::computeTime, hourSpinner.valueProperty(),
                minuteSpinner.valueProperty(), secondSpinner.valueProperty()));
    }

    public ReadOnlyObjectProperty<Duration> timeProperty() {
        return time.getReadOnlyProperty() ;
    }

    public Duration getTime() {
        return timeProperty().get();
    }

    private Duration computeTime() {
        int seconds = secondSpinner.getValue();
        int minutes = minuteSpinner.getValue();
        int hours = hourSpinner.getValue();
        int totalSeconds = (hours * 60 + minutes) * 60 + seconds ;
        return Duration.ofSeconds(totalSeconds);
    }
}

测试代码:

import java.io.IOException;

import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class TimeSpinnerTest extends Application {

    @Override
    public void start(Stage primaryStage) throws IOException {
        FXMLLoader loader = new FXMLLoader(getClass().getResource("TimeSpinner.fxml"));
        HBox timeSpinner = loader.load();
        TimeSpinnerController timeController = loader.getController() ;
        Label label = new Label();

        label.textProperty().bind(Bindings.createStringBinding(() -> {
            long s = timeController.getTime().getSeconds() ;
            return String.format("%02d:%02d:%02d", s / 3600, (s / 60) % 60, s % 60);
        }, timeController.timeProperty()));

        VBox root = new VBox(10, timeSpinner, label);
        root.setAlignment(Pos.CENTER);
        root.setPadding(new Insets(24));
        primaryStage.setScene(new Scene(root));
        primaryStage.show();
    }

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