我正在尝试设置呈现的内容与所选的 RadioButton 的值相关。问题在于,它对选择按钮没有反应。 也许我的实现有问题。
当我设置一个 onAction 事件时,有时它可以工作,但这引入了一些不同的问题,因此,我想为此问题找到解决方案。
Label title = new Label("USER REGISTRATION");
title.setId("title");
Label pickUserLabel = new Label("User type");
pickUserLabel.getStyleClass().add("labels");
RadioButton pickStudent = new RadioButton("Student");
RadioButton pickProfessor = new RadioButton("Professor");
/*
* Selection of user type -> adds additional fields if needed
*/
ToggleGroup userTypeGroup = new ToggleGroup();
pickStudent.setToggleGroup(userTypeGroup);
pickStudent.setSelected(true);
pickProfessor.setToggleGroup(userTypeGroup);
pickStudent.getStyleClass().add("radio");
pickProfessor.getStyleClass().add("radio");
/*
* text fields and labels below are used whenever additional ones are needed
*/
TextField textField1 = new TextField();
TextField textField2 = new TextField();
TextField textField3 = new TextField();
Label label1 = new Label();
Label label2 = new Label();
Label label3 = new Label();
ChoiceBox<AcademicTitles> academicTitleChoice = new ChoiceBox<AcademicTitles>();
academicTitleChoice.getSelectionModel().selectedItemProperty().addListener((v, oldValue, newValue) -> System.out.println(newValue));
if(pickStudent.isSelected()) {
label1.setText("Email");
label1.getStyleClass().add("labels");
textField1.setPromptText("E-mail");
label2.setText("Phone Number");
label2.getStyleClass().add("labels");
textField2.setPromptText("Phone number");
label3.setText("Recordbook Number");
label3.getStyleClass().add("labels");
textField3.setPromptText("Recordbook number");
academicTitleChoice.hide();
};
if(pickProfessor.isSelected()) {
label1.setText("Email");
label1.getStyleClass().add("labels");
textField1.setPromptText("E-mail");
layout.getChildren().remove(label2);
layout.getChildren().remove(textField2);
layout.getChildren().remove(label3);
layout.getChildren().remove(textField3);
academicTitleChoice.getItems().addAll(AcademicTitles.PROFESSOR, AcademicTitles.ASSOCIATE_PROFESSOR,
AcademicTitles.ASSISTANT_PROFESSOR, AcademicTitles.RESEARCH_PROFESSOR,
AcademicTitles.TEACHING_ASSISTANT);
academicTitleChoice.setValue(AcademicTitles.PROFESSOR);
academicTitleChoice.setMaxWidth(150);
academicTitleChoice.setId("academicTitleChoice");
};
Separator separator = new Separator();
/*
* First Name
*/
Label firstNameLabel = new Label("Enter First Name");
firstNameLabel.getStyleClass().add("labels");
TextField firstName = new TextField();
firstName.setPromptText("First name");
firstName.setMaxWidth(200);
/*
* Last Name
*/
Label lastNameLabel = new Label("Enter Last Name");
lastNameLabel.getStyleClass().add("labels");
TextField lastName = new TextField();
lastName.setPromptText("Last name");
lastName.setMaxWidth(200);
/*
* Username
*/
Label usernameLabel = new Label("Enter Username");
usernameLabel.getStyleClass().add("labels");
Label usernameInfo = new Label("(length must be >= 4)");
usernameInfo.getStyleClass().add("info");
TextField username = new TextField();
username.setPromptText("Username");
username.setMaxWidth(200);
/*
* Password
*/
Label passwordLabel = new Label("Enter Password");
passwordLabel.getStyleClass().add("labels");
Label passwordInfo = new Label("(length must be >= 6)");
passwordInfo.getStyleClass().add("info");
PasswordField password = new PasswordField();
password.setPromptText("Password");
password.setMaxWidth(200);
Button registrationButton = new Button("Register User");
registrationButton.setOnAction(e -> register());
GridPane.setConstraints(title, 0, 0);
GridPane.setConstraints(pickUserLabel, 0, 1);
GridPane.setConstraints(pickStudent, 0, 2);
GridPane.setConstraints(pickProfessor, 0, 3);
GridPane.setConstraints(separator, 0, 5);
GridPane.setConstraints(firstNameLabel, 0, 6);
GridPane.setConstraints(firstName, 1, 6);
GridPane.setConstraints(lastNameLabel, 0, 7);
GridPane.setConstraints(lastName, 1, 7);
GridPane.setConstraints(academicTitleChoice, 2, 7);
GridPane.setConstraints(usernameLabel, 0, 8);
GridPane.setConstraints(username, 1, 8);
GridPane.setConstraints(usernameInfo, 2, 8);
GridPane.setConstraints(passwordLabel, 0, 9);
GridPane.setConstraints(password, 1, 9);
GridPane.setConstraints(passwordInfo, 2, 9);
GridPane.setConstraints(label1, 0, 10);
GridPane.setConstraints(textField1, 1, 10);
GridPane.setConstraints(label2, 0, 11);
GridPane.setConstraints(textField2, 1, 11);
GridPane.setConstraints(label3, 0, 12);
GridPane.setConstraints(textField3, 1, 12);
GridPane.setConstraints(registrationButton, 1, 13);
GridPane.setHalignment(registrationButton, HPos.RIGHT);
layout.getChildren().addAll(title, pickUserLabel, pickStudent, pickProfessor, separator, firstNameLabel, firstName, lastNameLabel, lastName, usernameLabel, username, passwordLabel, password, passwordInfo, academicTitleChoice, label1, label2, textField2, label3, textField3, registrationButton);
layout.setAlignment(Pos.CENTER);
layout.setVgap(10);
layout.setHgap(10);
layout.setId("rightSide");
layout.setPrefSize(620, 550);
return layout;
结果应该是,如果选择“ RadioButton Professor”字段,则该字段将更改,对于“学生”字段也是如此。
答案 0 :(得分:4)
在加载类/方法时,您的if语句仅被调用一次。您需要“收听” RadioButton.selectedProperty
或ToggleGroup.selectedToggleProperty
并采取相应的行动。
下面是一个简单的示例,说明如何听ToggleGroup
并根据选择的RadioButton
执行代码:
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.RadioButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class RadioButtonListener extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
// Simple Interface
VBox root = new VBox(10);
root.setAlignment(Pos.CENTER);
root.setPadding(new Insets(10));
// Create the toggle group
ToggleGroup group = new ToggleGroup();
// Create two radio buttons
RadioButton rdo1 = new RadioButton("One");
RadioButton rdo2 = new RadioButton("Two");
// Add them to the ToggleGroup
group.getToggles().addAll(rdo1, rdo2);
// Add a listener to the ToggleGroup to determine when a selection has changed
group.selectedToggleProperty().addListener((observable, oldValue, newValue) -> {
if (newValue.equals(rdo1)) {
System.out.println("ONE selected");
} else if (newValue.equals(rdo2)) {
System.out.println("TWO selected");
}
});
// Add the RadioButtons to the stage
root.getChildren().addAll(rdo1, rdo2);
// Show the stage
primaryStage.setScene(new Scene(root));
primaryStage.setTitle("Sample");
primaryStage.show();
}
}
您可以通过向每个RadioButton
中添加一个侦听器来做类似的事情:
rdo1.selectedProperty().addListener((observable, oldValue, newValue) -> {
if (newValue) {
System.out.println("ONE Selected!");
} else {
System.out.println("ONE deselected!");
}
});