【问题标题】:How do i access the text of the selected radio button in JavaFX?如何访问Javafx中选定的广播按钮的文本?
【发布时间】:2022-11-04 04:35:44
【问题描述】:

我正在为我对 JavaFX GUI 的新理解创建一个项目。我只是无法让文件写入“小蛋糕”、“中蛋糕”或“大蛋糕”,具体取决于选择了哪个单选按钮。我知道我的大部分逻辑都是有效的,它归结为

writer.write(cakeSize.getSelectedToggle().selectedProperty().getValue().toString());

无论我查看的文档或什么 .我选择的选择器我似乎只能访问布尔值,说明选定是否为“真”,如果不是,则它将名称值返回为“选定”

它不需要使用 setText(value) 如果需要我可以摆脱那些 setter 我只是想找出哪里出错了。如果没有这些,它只会根据选择在对象中返回“小”“中”或“大”。那些自己写到文件中的我也很好。

谢谢!


import javafx.application.Application;
import javafx.application.Platform;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;

import java.io.*;

public class BakeryApplication extends Application {
    @Override
    public void start(Stage stage) {
        //Create a pane
        //the pane is the layout container that contains other JavaFX components
        GridPane gridPane = new GridPane();
        //Create a label and pass it through the pane layout
        Label orderTitle = new Label("Place your Cake Order Below");
        Label nameTitle = new Label("First and Last Name");
        Label numberTitle = new Label("Please enter your phone number:");
        //Create a text field area
        TextField name = new TextField();
        TextField number = new TextField();

        Label cakeSizeTtl = new Label("Please select your Cake Size:");
        RadioButton cakeSm = new RadioButton("Small");
        cakeSm.setText("Small Cake");
        RadioButton cakeMd = new RadioButton("Medium");
        cakeMd.setText("Medium Cake");
        RadioButton cakeLg = new RadioButton("Large");
        cakeLg.setText("Large Cake");

        ToggleGroup cakeSize = new ToggleGroup();
        cakeSm.setToggleGroup(cakeSize);
        cakeMd.setToggleGroup(cakeSize);
        cakeLg.setToggleGroup(cakeSize);




        Label cakeTypeTtl = new Label("Please select your Cake Type:");
        //Combo Box
        ComboBox<String> cakeSelection = new ComboBox<>();
        cakeSelection.getItems().addAll("Apple","Carrot", "Cheesecake","Chocolate", "Coffee", "Opera", "Tiramisu");
        cakeSelection.setValue("Cake Type");

        //create a save and quit button
        Button saveBtn = new Button("Save");
        Button quitBtn = new Button("Quit");

        //Events for buttons
        saveBtn.setOnAction(e -> {

                    try {
                        BufferedWriter writer = new BufferedWriter( new FileWriter("Order.txt"));
                        writer.write(number.getText());
                        writer.newLine();
                        writer.write(name.getText());
                        writer.newLine();
                        //add cakeType selection
//                        writer.write(cakeSize.getSelectedToggle().selectedProperty().toString());
                        writer.write(cakeSize.getSelectedToggle().selectedProperty().getValue().toString());
                        writer.newLine();
                        writer.write(cakeSelection.getValue());
                        //add cakeSize selection
                        writer.close();
                    } catch (IOException err) {
                        err.printStackTrace();
                    }
                });

        //handles click event on quit button to exit program
        quitBtn.setOnAction(e ->{
            Platform.exit();
        });
        //add an HBox to hold the buttons and arrange them horizontally
        HBox buttonBox = new HBox(10, saveBtn, quitBtn);
        gridPane.setHgap(10);
        gridPane.setVgap(10);
        //node, column, row
        gridPane.setConstraints(orderTitle,2,0);
        gridPane.setConstraints(nameTitle,2,1);
        gridPane.setConstraints(name, 2, 2);
        gridPane.setConstraints(numberTitle,2,3);
        gridPane.setConstraints(number, 2, 4);
        gridPane.setConstraints(cakeSizeTtl, 3, 5);
        gridPane.setConstraints(cakeSm, 3, 6);
        gridPane.setConstraints(cakeMd, 3, 7);
        gridPane.setConstraints(cakeLg, 3, 8);
        gridPane.setConstraints(cakeTypeTtl, 2, 5);
        gridPane.setConstraints(cakeSelection, 2, 6);
        gridPane.setConstraints(buttonBox, 3, 11);
        gridPane.setPadding(new Insets(20));
        //use getChildren and add method to  place the label node in the pane layout
       gridPane.getChildren().addAll(buttonBox, orderTitle, name, number, nameTitle, numberTitle, cakeSm, cakeMd, cakeLg, cakeSizeTtl, cakeSelection, cakeTypeTtl);
        //Use BorderPane to aid in layout
        //controls are typically inserted into a different
        //type  of layout and then added into the BorderPane accordingly
        //like how our buttons and title are in a GridPane right now
        BorderPane mainPain = new BorderPane();
        mainPain.setCenter(gridPane);
        //add the pane to the scene
        Scene scene = new Scene(mainPain, 500, 500);
        stage.setTitle("Assignment2 Order Your Cake!");
        //Placing the Scene in the stage
        stage.setScene(scene);
        //Displays the Stage
        stage.show();
    }

    public static void main(String[] args) {
        //launches the Stage
        launch();
    }

}

【问题讨论】:

  • 请阅读 ToggleButton 的 api doc: selected != text (前者为布尔类型,后者为 String 类型),如果你想要文本,那么 .. 询问文本 :) 另请阅读构造函数的 api doc一个字符串(它是实例化和调用 setText 的捷径 - 因此在该构造函数之后设置另一个文本没有意义)。链式调用中的 getValue (您在描述中显示的第一行代码)是选定的属性总是对或错)
  • 我将再次查看 API 文档,我已经看过但就像我说我是 JavaFX 的新手,所以我正在寻找澄清。我真的不明白为什么这里的人如此野蛮。我什至提到我一直在寻找并试图理解我的问题中的文档。每个人都是从某个地方开始的,我真的在问问题之前尽可能多地进行研究。
  • 我刚去和你所说的一起再次回顾了它,虽然你所说的证实了我认为它在做什么,正如我的问题所述,它没有回答如何让小型或大型保存到选择后的文件。如果您确实尝试回答这个问题,那么请说明我在哪里寻找关于如何做的澄清,但仍然没有得到答复。

标签: javafx radio-button


【解决方案1】:

您可以使用 Toggle使用 Toggle使用 getSelectedToggle()getSelectedToggle()selectedToggleProperty() method,询问 ToggleGroup

然而Toggle 是一个没有定义文本属性的接口。 Toggle 接口有很多实现,RadioButton 就是其中之一。 现在,既然你知道你的ToggleGroup 只包含RadioButton 切换,你可以安全地投射它并询问它的文本。

简要地:

RadioButton selectedToggle = (RadioButton) cakeSize.getSelectedToggle();
if (selectedToggle != null) // it can be null if nothing is selected
   String selectedText = selectedToggle.getText();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-16
    • 1970-01-01
    • 1970-01-01
    • 2020-06-26
    • 1970-01-01
    • 2014-10-15
    • 1970-01-01
    相关资源
    最近更新 更多