【问题标题】:When pressing the button, a number should be displayed and counted up at the date picker按下按钮时,应在日期选择器上显示并计数一个数字
【发布时间】:2018-05-27 15:09:23
【问题描述】:

如果我按下“已选择”按钮,日期选择器应在今天的日期显示一个数字,并且当我按下“已选择”按钮的次数越多时,该数字就越多。

例如:

今天我们有 27.05,所以如果我按下按钮“选择”那里的一个,如果我再次按下按钮应该那里是一个两个如果我第三次按下按钮应该是一个三个那里等等。

希望有人能提供帮助,在此先感谢。

我的代码:

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);


        }
    }

【问题讨论】:

  • 不明白点击次数(1,2,3...)显示在哪里
  • 对不起。在图片中,您可以看到带有灰色边缘的 27。然后应该有 1,2,3 所以今天的日期。你明白了吗?
  • 所以你想用点击次数替换日期显示(27)。我想知道为什么。无论如何,首先让你按钮做某事。我没有看到它有任何与之关联的处理程序。
  • 不,我不想替换 27,但它仍然是 27,数字也应该在那里。
  • 编辑实际上是一个不同的问题,需要不同的帖子/

标签: java javafx datepicker fxml


【解决方案1】:

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

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);}
}

【讨论】:

  • 再次打扰,但我使用的是 SceneBuilder。我使用第三类(Main、Controller 和 sample.fxml)。如果我像上面一样离开 main 并且只更改控制器(当然是 sample.fxml),代码会是什么样子?
  • 尝试在控制器中创建和设置单元工厂。
  • 我试过了,不幸的是,没有显示数字和红色,当点击按钮时,没有任何变化。
  • 很难看到你做了什么。请使用修改后的代码的minimal reproducible example 发布一个新问题。在这里给我发消息,我会尽力提供帮助。
  • 感谢您的帮助。我已经编辑了我的帖子,这就是我的 Controller.Java 的样子,不得不注释掉一些东西。 datePicker.setDayCellFactory (dayCellFactory);必须在哪里,有什么问题?
【解决方案2】:

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

fxml你使用

Button fx:id="button1"

在控制器中:

@FXML
private Button button2; 

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

第二个是这样的:你把cell factory的名字改成set
javafx.util.Callback&lt;DatePicker, DateCell&gt; set

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

【讨论】:

  • 我们将不胜感激
猜你喜欢
  • 2013-04-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-07
  • 2012-09-01
相关资源
最近更新 更多