公历日历JavaFX

问题描述 投票:0回答:1

我被困在试图找出这个问题。它是关于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);
}
}
java javafx gregorian-calendar
1个回答
1
投票

首先,不要在列表视图中填充字符串。使用Month.values()对象填写Integer的月份列表视图和月份的日期。将所选项目取出时,这将更加方便,并且可以节省您在类顶部声明数组的过程。

在下面我假设你有一个名为s1m变量而不是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.time中的LocalDate,即现代Java日期和时间API。

GregorianCalendar构造函数相反,如果向其传递无效值,LocalDate.of方法将抛出异常,例如2019年2月29日。这将帮助您根据需要向用户报告无效日期。

您的代码出了什么问题?

  • 你今年使用的是textfield3.getLength()。假设输入了一个四位数的年份,这将给你第4年(现在是几千年前)。
  • 当您在月中使用lvdays.getSelectionModel().getSelectedIndex()时,您在列表视图的列表中获得了基于0的索引。因此,当用户选择1时,你得到0,等等。

其他提示:学习创造好的变量名称。它真正区分了有价值的代码和无用的代码。另外,当在Stack Overflow上提问时,如果他们能够轻松地阅读您的代码,人们将更有可能帮助您,而他们不能使用变量名称s1s2kx

链接

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

© www.soinside.com 2019 - 2024. All rights reserved.