我正在尝试在 javafx 中创建一个调查问卷面板。我有一个带有问题的标签和三个用于答案的按钮。我已经设法将它们全部放在一起,但是我不确定如何构建整个事情。此外,在同一个 BorderPane 中,我想添加更多问题。我怎样才能这样做?
BorderPane questionaires = new BorderPane();
questionaires.setTop(questionnairesPane);
questionaires.setCenter(questionsPane);
//QUESTIONS Panel
questionnairesPane = new GridPane();
questionnairesPane.setHgap(0);
questionnairesPane.setVgap(0);
questionnairesPane.setPadding(new Insets(0, 0, 0, 200));
results = new MyTitles("Questionnaires");
questionnairesPane.setStyle("-fx-font: 150 hamster; -fx-text-fill: white;");
questionnairesPane.add(results,1,1);
questionsPane = new GridPane();
questionsPane.setHgap(15);
questionsPane.setVgap(30);
questionsPane.setPadding(new Insets(0, 50, 0, 80));
SsubjectLabel = new Label("What was your affective response during the game?");
SsubjectLabel.setPrefWidth(1000.0);
SsubjectLabel.setPrefHeight(70.0);
SsubjectLabel.setStyle("-fx-font: 30 cornerstone; -fx-text-fill: black;");
boredom = new Button("Boredom");
boredom.setStyle("-fx-font: 30 cornerstone; -fx-text-fill: black;");
boredom.addEventHandler(ActionEvent.ACTION, (e)-> {
});
engagement = new Button("Boredom");
engagement.setStyle("-fx-font: 30 cornerstone; -fx-text-fill: black;");
engagement.addEventHandler(ActionEvent.ACTION, (e)-> {
});
frustration = new Button("Boredom");
frustration.setStyle("-fx-font: 30 cornerstone; -fx-text-fill: black;");
frustration.addEventHandler(ActionEvent.ACTION, (e)-> {
});
questions.add(SsubjectLabel,1,1);
questions.add(boredom,1,2);
questions.add(engagement,1,3);
questions.add(frustration,1,4);
questions.add(next,2,4);
功能是正确的,但是我如何构建正确的所有按钮,这是我正在努力做的事情。我的面板现在如下图所示:
最佳答案
在创建布局的类中,您可以使用一种接受 Question
类作为参数的方法。
Question
类应该包含实际问题、可能的答案和正确的答案。
当您想要更改或转到上一个或下一个时,只需调用上面的方法(changeQuestion(Question newOne);
)
此方法将更改您使用的 javaFX 元素的值。因此您不会每次都创建新的 FX 元素。
如果您想保存很多问题,请使用数组,并在类问题中提供一个已回答的值(如果已回答或未回答)。
以上都是一个简单的想法。
Edit
阅读here关于 javaFX 中的内置布局和对齐
你能做的是:
HBox box = new HBox();
Button confirm = new Button();
box.getChildren().add(confirm);
borderPane.setBottom(box);
确认将在窗口的左侧。这称为对齐。
您可以使用方法setAlignment();
来更改Hbox
和Button
的位置。
不过还有更好的方法。请参阅有关 fxml 和 javaFX 的完整教程 here .
关于java - javafx 中问卷面板的结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38354298/