1 回答
TA贡献2041条经验 获得超4个赞
我相信您对 a 的使用StackPane可能会阻止鼠标事件在您的 RadioButtons 上注册。
虽然我通常不这样做,但下面是一个重新配置的布局,您可以将其用作代码的起点。我已经大大简化了布局,所有 RadioButtons 都按要求运行:
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextField;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class scratch_1 extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
VBox root = new VBox(10);
root.setAlignment(Pos.CENTER);
root.setPadding(new Insets(10));
Label lblTitle = new Label("Gib die Basis und die Zahl ein!");
root.getChildren().add(lblTitle);
// The input TextField
TextField txtInput = new TextField();
root.getChildren().add(txtInput);
// HBox to hold our three columns
HBox hBoxSelections = new HBox(20);
hBoxSelections.setAlignment(Pos.CENTER);
// Use a GridPane for our RadioButton grid
GridPane gridSelections = new GridPane();
gridSelections.setAlignment(Pos.CENTER);
gridSelections.setHgap(10);
gridSelections.setVgap(5);
// Our ToggleGroup to which all RadioButtons will belong
ToggleGroup groupSelections = new ToggleGroup();
// We need 11 rows of 3 RadioButtons, let's use a couple of for loops to populate the GridPane
int counter = 2;
for (int i = 0; i < 11; i++) {
for (int j = 0; j < 3; j++) {
RadioButton radioButton = new RadioButton("" + counter);
radioButton.setToggleGroup(groupSelections);
gridSelections.add(radioButton, j, i);
counter++;
}
}
// Let's add the GridPane to our root layout
root.getChildren().add(gridSelections);
primaryStage.setScene(new Scene(root, 400, 400));
primaryStage.show();
}
}
结果:
请注意,我从您之前的代码发布中工作,因此上述示例不包括您的update()
方法(或任何逻辑,就此而言)。
添加回答
举报