JavaFX 单选按钮不起作用(无法选择)

我想制作一个转换不同基础的项目,但我的单选按钮不起作用。我正在制作三个不同的 VBox 来添加按钮,但只有第三列有效。我对 JavaFX 很陌生,所以如果有人能帮助我,我将不胜感激。谢谢!这是我的代码


import javafx.application.Application;

import javafx.scene.Scene;

import javafx.scene.control.RadioButton;

import javafx.scene.control.TextField;

import javafx.scene.control.ToggleGroup;

import javafx.scene.layout.*;

import javafx.scene.text.Text;

import javafx.scene.text.TextAlignment;

import javafx.stage.Stage;


import java.math.BigInteger;


public class Main extends Application {


private ToggleGroup group;

private TextField txtfield;

private TextField output;

private int base = 10;


public static void main(String[] args) {

    launch(args);

}


@Override

public void start(Stage primaryStage) {

    BorderPane root = new BorderPane();

    StackPane title = new StackPane();

    Text t = new Text("Gib die Basis und die Zahl ein!");

    t.setTextAlignment(TextAlignment.CENTER);

    title.getChildren().add(t);

    FlowPane center = new FlowPane();

    center.setPrefSize(400, 300);

    HBox input = new HBox();

    VBox base1 = new VBox();

    VBox base2 = new VBox();

    VBox base3 = new VBox();

    input.setPrefSize(400, 100);

    base1.setPrefSize(50, 150);

    base2.setPrefSize(50, 150);

    base3.setPrefSize(50, 150);

    group = new ToggleGroup();

    for (int i = 2; i < 35; i++) {

        RadioButton b = new RadioButton(String.valueOf(i));

        if (i == 10) {

            b.setSelected(true);

        }

        b.setToggleGroup(group);

        b.setOnMouseClicked(mouseEvent -> {

            base = Integer.valueOf(b.getText());

            update();

        });


        if (i % 3 == 2) {

            base1.getChildren().add(b);

            b.setTranslateX(110);

        } else if (i % 3 == 0) {

            base2.getChildren().add(b);

            b.setTranslateX(120);

        } else {

            base3.getChildren().add(b);

            b.setTranslateX(130);

        }

    }


这是程序应该(并且确实)看起来的样子

http://img1.mukewang.com/62b3d0890001409c07980797.jpg

米琪卡哇伊
浏览 190回答 1
1回答

缥缈止盈

我相信您对 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 {&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; launch(args);&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void start(Stage primaryStage) throws Exception {&nbsp; &nbsp; &nbsp; &nbsp; VBox root = new VBox(10);&nbsp; &nbsp; &nbsp; &nbsp; root.setAlignment(Pos.CENTER);&nbsp; &nbsp; &nbsp; &nbsp; root.setPadding(new Insets(10));&nbsp; &nbsp; &nbsp; &nbsp; Label lblTitle = new Label("Gib die Basis und die Zahl ein!");&nbsp; &nbsp; &nbsp; &nbsp; root.getChildren().add(lblTitle);&nbsp; &nbsp; &nbsp; &nbsp; // The input TextField&nbsp; &nbsp; &nbsp; &nbsp; TextField txtInput = new TextField();&nbsp; &nbsp; &nbsp; &nbsp; root.getChildren().add(txtInput);&nbsp; &nbsp; &nbsp; &nbsp; // HBox to hold our three columns&nbsp; &nbsp; &nbsp; &nbsp; HBox hBoxSelections = new HBox(20);&nbsp; &nbsp; &nbsp; &nbsp; hBoxSelections.setAlignment(Pos.CENTER);&nbsp; &nbsp; &nbsp; &nbsp; // Use a GridPane for our RadioButton grid&nbsp; &nbsp; &nbsp; &nbsp; GridPane gridSelections = new GridPane();&nbsp; &nbsp; &nbsp; &nbsp; gridSelections.setAlignment(Pos.CENTER);&nbsp; &nbsp; &nbsp; &nbsp; gridSelections.setHgap(10);&nbsp; &nbsp; &nbsp; &nbsp; gridSelections.setVgap(5);&nbsp; &nbsp; &nbsp; &nbsp; // Our ToggleGroup to which all RadioButtons will belong&nbsp; &nbsp; &nbsp; &nbsp; ToggleGroup groupSelections = new ToggleGroup();&nbsp; &nbsp; &nbsp; &nbsp; // We need 11 rows of 3 RadioButtons, let's use a couple of for loops to populate the GridPane&nbsp; &nbsp; &nbsp; &nbsp; int counter = 2;&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < 11; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int j = 0; j < 3; j++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; RadioButton radioButton = new RadioButton("" + counter);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; radioButton.setToggleGroup(groupSelections);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; gridSelections.add(radioButton, j, i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; counter++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; // Let's add the GridPane to our root layout&nbsp; &nbsp; &nbsp; &nbsp; root.getChildren().add(gridSelections);&nbsp; &nbsp; &nbsp; &nbsp; primaryStage.setScene(new Scene(root, 400, 400));&nbsp; &nbsp; &nbsp; &nbsp; primaryStage.show();&nbsp; &nbsp; }}结果:请注意,我从您之前的代码发布中工作,因此上述示例不包括您的update()方法(或任何逻辑,就此而言)。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java