gpt4 book ai didi

java - 公历 JavaFX

转载 作者:行者123 更新时间:2023-12-02 10:15:16 24 4
gpt4 key购买 nike

我一直在试图弄清楚这个问题。这是关于JavaFX控件在按下回车键时选择月份、数字日和年的名称。我什至尝试使用面向对象来解决这个问题。问题在图片链接中。 Here is the question in this link.

新的编辑和代码:我为了获取星期几而休息了一天,例如 2012 年 1 月 1 日应该是第 1 天,作为控制台中的答案:星期几:1 而不是 2。

 public class DayOfWeek extends Application {

private String[] months = { "JANUARY", "FEBRUARY", "MARCH", "APRIL", "MAY", "JUNE", "JULY", "AUGUST", "SEPTEMBER",
"OCTOBER", "NOVEMBER", "DECEMBER" };

private String[] days = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16",
"17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31" };

String s1 = "";
int k = 0;

String s2 = "";
int x = 0;

@Override
public void start(Stage primaryStage) {

// add a gridpane
GridPane grid = new GridPane();
grid.setPadding(new Insets(10, 10, 10, 10));

// add a label for original message top left side
Label label1 = new Label("Month: ");
GridPane.setConstraints(label1, 0, 0);

ListView<String> lv = new ListView<>(FXCollections.observableArrayList(months));
lv.setPrefSize(100, 100);
lv.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
GridPane.setConstraints(lv, 1, 0);

Label label2 = new Label("Day: ");
GridPane.setConstraints(label2, 2, 0);

ListView<String> lvdays = new ListView<>(FXCollections.observableArrayList(days));
lvdays.setPrefWidth(50);
lvdays.setPrefHeight(75);
lv.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
GridPane.setConstraints(lvdays, 3, 0);

Label label3 = new Label("Year: ");
GridPane.setConstraints(label3, 4, 0);

TextField textfield3 = new TextField();
GridPane.setConstraints(textfield3, 5, 0);

lv.setOnMouseClicked(e -> {
s1 = lv.getSelectionModel().getSelectedItem();
k = lv.getSelectionModel().getSelectedIndex();
System.out.println(lv.getSelectionModel().getSelectedItem());
});

lvdays.setOnMouseClicked(e2 -> {
s2 = lvdays.getSelectionModel().getSelectedItem();
x = lvdays.getSelectionModel().getSelectedIndex();
System.out.println(lvdays.getSelectionModel().getSelectedItem());
});

textfield3.setOnKeyPressed(e3 -> {
if (e3.getCode() == KeyCode.ENTER) {
GregorianCalendar cal2 = new GregorianCalendar(textfield3.getLength(), k, x);
System.out.println("Day of Week: " + cal2.get(Calendar.DAY_OF_WEEK));
}
});

// add it to the parent
grid.getChildren().addAll(label1, lv, label2, lvdays, label3, textfield3);

// set a scene and place show it
Scene scene = new Scene(grid, 600, 200);
primaryStage.setTitle("Day of Week");
primaryStage.setScene(scene);
primaryStage.show();

}

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

最佳答案

首先,不要将字符串填充到 ListView 中。使用 Month.values() 填充月份 ListView ,并使用 Integer 对象填充月份中的天数。当取出所选项目时,这会更方便,并且可以节省您在类顶部声明数组的时间。

在下面,我假设您有一个名为 m 的变量,而不是 s1,其类型为 Month ,表示月份, s2 的类型为 Integer(您应该找到一个更好的名称)。

            String yearText = textfield3.getText();
try {
int year = Integer.parseInt(yearText);
LocalDate date = LocalDate.of(year, m, s2);
System.out.println("Day of Week: " + date.getDayOfWeek());
} catch (NullPointerException | NumberFormatException | DateTimeException e) {
System.out.format("Not a valid date: %s %s %d", yearText, m, s2);
}

避免使用GregorianCalendar。该类设计得很差,幸运的是早已过时了。而是使用现代 Java 日期和时间 API java.time 中的 LocalDate

GregorianCalendar 构造函数相反,如果您向其传递无效值,例如 2019 年 2 月 29 日,LocalDate.of 方法将引发异常。这将有所帮助您按照要求向用户报告无效日期。

您的代码出了什么问题?

  • 您当年使用的是 textfield3.getLength()。假设输入了四位数的年份,这将为您提供第 4 年(那是几千年前的事了)。
  • 当您使用 lvdays.getSelectionModel().getSelectedIndex() 表示月份中的某一天时,您会在 ListView 的列表中获得从 0 开始的索引。因此,当用户选择 1 时,您会得到 0 等。

其他提示:学习发明好的变量名称。它确实区分了有值(value)的代码和无值(value)的代码。此外,当在 Stack Overflow 上提问时,如果人们可以轻松阅读您的代码,他们将更有可能帮助您,而变量名 s1s2 则无法做到这一点。 kx

链接

Oracle tutorial: Date Time解释如何使用 java.time。

关于java - 公历 JavaFX,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54729024/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com