按下按钮时,应在日期选择器中显示并计算一个数字
When pressing the button, a number should be displayed and counted up at the date picker
如果我按下 "selected" 按钮,日期选择器中应显示今天的日期,并且我按下 "selected" 按钮的次数越多,该数字就应该被计算在内。
例如:
今天我们有 27.05,所以如果我按下按钮 "selected" 那里有一个,如果我再次按下按钮应该有一个二如果我第三次按下按钮应该有一个三并且等等。
希望有人能帮忙,在此先感谢。
我的代码:
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);
}
}
您可以控制和操纵单个(或多个)DateCell using 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);}
}
已编辑问题的答案:
有 2 个错误:
在 fxml
你使用
Button fx:id="button1"
在控制器中:
@FXML
private Button button2;
这是错误的。需要与 fxml
中的名称相同:button1。
(因为 button2 没有在 fxml
中定义,所以使用它一定会抛出一个 NullPointerException
,这不应该被忽略)
第二个是这样的:你把细胞工厂的名字改成了set
javafx.util.Callback<DatePicker, DateCell> set
此处也应更改:
datePicker.setDayCellFactory(dayCellFactory);
否则它不会编译。
如果我按下 "selected" 按钮,日期选择器中应显示今天的日期,并且我按下 "selected" 按钮的次数越多,该数字就应该被计算在内。
例如:
今天我们有 27.05,所以如果我按下按钮 "selected" 那里有一个,如果我再次按下按钮应该有一个二如果我第三次按下按钮应该有一个三并且等等。
希望有人能帮忙,在此先感谢。
我的代码:
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);
}
}
您可以控制和操纵单个(或多个)DateCell using 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);}
}
已编辑问题的答案: 有 2 个错误:
在 fxml
你使用
Button fx:id="button1"
在控制器中:
@FXML
private Button button2;
这是错误的。需要与 fxml
中的名称相同:button1。
(因为 button2 没有在 fxml
中定义,所以使用它一定会抛出一个 NullPointerException
,这不应该被忽略)
第二个是这样的:你把细胞工厂的名字改成了set
javafx.util.Callback<DatePicker, DateCell> set
此处也应更改:
datePicker.setDayCellFactory(dayCellFactory);
否则它不会编译。