按下按钮时,应显示一个数字并在日期选择器上计数

时间:2018-05-27 15:09:24

标签: java javafx datepicker fxml

如果我按下“已选择”按钮,则应在Datepicker的今天日期显示一个数字,并且应该按照“选择”按钮进行计算。

例如:

今天我们有了27.05,所以如果按下“选中”按钮那么一个,如果我再次按下按钮那么如果按下按钮那么第三次就应该是三个等等。< / p>

enter image description here

我希望有人可以提前帮助。

我的代码:

Main.java

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
        primaryStage.setTitle("Hello World");
        primaryStage.setScene(new Scene(root, 600, 400));
        primaryStage.show();
    }

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

Controller.java

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.DateCell;
import javafx.scene.control.DatePicker;
import javafx.scene.control.Label;
import javax.security.auth.callback.Callback;
import java.net.URL;
import java.time.LocalDate;
import java.util.Date;
import java.util.ResourceBundle;

public class Controller {

    @FXML
    private Button button1;

    @FXML
    private DatePicker datePicker;       
}

sample.fxml

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.DatePicker?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
   <children>
      <Button fx:id="button1" layoutX="344.0" layoutY="68.0" mnemonicParsing="false" text="selected" />
      <DatePicker fx:id="datePicker" layoutX="45.0" layoutY="56.0" />
   </children>
</AnchorPane>

修改

Controller.java

package sample;


    import javafx.event.ActionEvent;
    import javafx.event.EventHandler;
    import javafx.fxml.FXML;
    import javafx.fxml.Initializable;
    import javafx.scene.control.Button;
    import javafx.scene.control.DateCell;
    import javafx.scene.control.DatePicker;
    import javafx.scene.control.Label;

    import javax.security.auth.callback.Callback;
    import java.net.URL;
    import java.time.LocalDate;
    import java.util.Date;
    import java.util.ResourceBundle;

    public class Controller implements Initializable {

        private int clicksCounter = 0;
        private String date = String.valueOf(LocalDate.now().getDayOfMonth());

        @FXML
        private Button button2;

        @FXML
        private DatePicker datePicker;

        @Override
        public void initialize(URL location, ResourceBundle resources) {
            button2.setOnAction(new EventHandler<ActionEvent>() {
                @Override
                public void handle(ActionEvent event) {
                    clicksCounter++;
                }
            });

            javafx.util.Callback<DatePicker, DateCell> set = new javafx.util.Callback<DatePicker, DateCell>() {
                @Override
                public DateCell call(final DatePicker datePicker) {
                    return new DateCell() {
                        @Override public void updateItem(LocalDate item, boolean empty) {
                            super.updateItem(item, empty);
                            //if today, change text and style
                            if (item.equals(LocalDate.now())) {
                                setText(date +"/" + clicksCounter);
                                setStyle("-fx-background-color: #ffc0cb;");
                            }
                        }
                    };
                }
            };
            //datePicker.setDayCellFactory(dayCellFactory);


        }
    }

2 个答案:

答案 0 :(得分:1)

您可以使用DateCell控制和操纵单个(或多个)cell factory。注意评论:

import java.time.LocalDate;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.DateCell;
import javafx.scene.control.DatePicker;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.Callback;

public class FxTest extends Application {

    private int clicksCounter = 0; //count clicks
    //todays date as string
    private String date = String.valueOf(LocalDate.now().getDayOfMonth());

    @Override
    public void start(Stage primaryStage) throws Exception {

        DatePicker datePicker = new DatePicker();
        //construct a cell factory
        final Callback<DatePicker, DateCell> dayCellFactory = new Callback<DatePicker, DateCell>() {
            @Override
            public DateCell call(final DatePicker datePicker) {
                return new DateCell() {
                    @Override public void updateItem(LocalDate item, boolean empty) {
                        super.updateItem(item, empty);
                        //if today, change text and style
                        if (item.equals(LocalDate.now())) {
                            setText(date +"/" + clicksCounter);
                            setStyle("-fx-background-color: #ffc0cb;");
                        }
                    }
                };
            }
        };
        datePicker.setDayCellFactory(dayCellFactory); //assign cell factory to picker

        Button button = new Button("Selected");
        button.setOnAction(e -> clicksCounter++); //update counter

        VBox vBox = new VBox( button, datePicker);
        Scene scene = new Scene(vBox, 400, 400);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

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

enter image description here

答案 1 :(得分:0)

回答编辑过的问题: 有2个错误:

fxml中使用

Button fx:id="button1"

在控制器中:

@FXML
private Button button2; 

这是错误的。需要与fxml中的名称相同:button1。
(由于button {未在fxml中定义使用它必须抛出一个NullPointerException,不应该被忽略)

第二个是:您更改了单元工厂的名称以设置
javafx.util.Callback<DatePicker, DateCell> set

也应该在这里改变:
datePicker.setDayCellFactory(dayCellFactory); 否则它将无法编译。