简体   繁体   English

JavaFX RadioButtons 不起作用(无法选择)

[英]JavaFX RadioButtons don‘t work(can‘t select)

I want to make a project that convert different bases but my Radiobuttons are not working.我想制作一个转换不同基数的项目,但我的单选按钮不起作用。 I'm making three different VBoxes to add the Buttons but only the third column is working.我正在制作三个不同的 VBox 来添加按钮,但只有第三列有效。 I'm quite new into JavaFX so I would appreciate if anyone could help me.我对 JavaFX 很陌生,所以如果有人能帮助我,我将不胜感激。 thanks!谢谢! Here's my Code这是我的代码

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);
        }
    }
    output = new TextField();
    txtfield = new TextField();
    txtfield.setTranslateY(50);
    txtfield.setTranslateX(100);
    txtfield.setOnKeyTyped(keyEvent -> {
        update();
    });
    input.getChildren().add(txtfield);
    center.getChildren().add(input);
    center.getChildren().add(base1);
    center.getChildren().add(base2);
    center.getChildren().add(base3);
    root.setCenter(center);
    root.setTop(title);
    root.setBottom(output);
    primaryStage.setScene(new Scene(root, 400, 400));
    primaryStage.show();
}

private void update() {

    try {
        output.setText(String.valueOf(new BigInteger(txtfield.getText(), base)));
    } catch (NumberFormatException e) {
        output.setText("Nan");
    }
}
}

Here's how the program should (and does) look like这是程序应该(并且确实)的样子应用程序

I believe your use of a StackPane may be blocking mouse events from registering on your RadioButtons.我相信您使用StackPane可能会阻止鼠标事件在您的 RadioButton 上注册。

While I don't usually do this, below is a reconfigured layout that you might use as a jumping off point for your code.虽然我通常不这样做,但下面是一个重新配置的布局,您可以将其用作代码的起点。 I've simplified the layout quite a bit and all RadioButtons are functioning as desired:我已经大大简化了布局,所有 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();
    }
}

Result:结果:

截屏


Note that I worked from your prior code posting as therefore the above sample does not include your update() method (or any logic, for that matter).请注意,我根据您之前发布的代码进行了工作,因此上述示例不包括您的update()方法(或任何逻辑,就此而言)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM