亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

JavaFX 單選按鈕不起作用(無法選擇)

JavaFX 單選按鈕不起作用(無法選擇)

米琪卡哇伊 2022-06-23 10:31:02
我想制作一個轉換不同基礎的項目,但我的單選按鈕不起作用。我正在制作三個不同的 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);}@Overridepublic 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);        }    }這是程序應該(并且確實)看起來的樣子
查看完整描述

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();

    }

}

結果:

http://img1.sycdn.imooc.com//62b3d0990001410206150657.jpg

請注意,我從您之前的代碼發布中工作,因此上述示例不包括您的update()方法(或任何邏輯,就此而言)。



查看完整回答
反對 回復 2022-06-23
  • 1 回答
  • 0 關注
  • 212 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號