【问题标题】:Create new radio button options during runtime在运行时创建新的单选按钮选项
【发布时间】:2017-03-17 08:48:51
【问题描述】:

我正在使用 JavaFX,并且希望在单击按钮时,会创建一个新的单选按钮并在流程窗格中显示。这可能吗?我试图这样做,但似乎什么都没有出现。

编辑: 问题似乎源于 Controller 类中的 insertDemand() 方法。如果我删除该方法的前两行,则会出现单选按钮。我不知道它为什么这样做。但是我需要在单击按钮时出现在屏幕上的一行以及出现的单选按钮,因此删除这两行不是一个可行的解决方案。

这是 fxml:

<GridPane alignment="center" hgap="10" prefHeight="800.0" prefWidth="1200.0" stylesheets="/sample/sample.css" vgap="10" xmlns="http://javafx.com/javafx/8.0.76-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
    <children>
        <!--Insert-->
        <ScrollPane GridPane.columnIndex="0" GridPane.rowIndex="1" GridPane.rowSpan="4">
            <content>
                <FlowPane>
                    <Button fx:id="demandB" mnemonicParsing="false" onAction="#insertDemand"  text="Demand" />
                </FlowPane>
            </content>
        </ScrollPane>
        <StackPane fx:id="workspaceSP" GridPane.columnIndex="1" GridPane.rowIndex="0" GridPane.rowSpan="2">
        </StackPane>
        <ScrollPane GridPane.columnIndex="1" GridPane.rowIndex="3">
            <content>
                <FlowPane fx:id="lineButtonsFP" prefHeight="100.0">
                    <Label text="Select a Curve:" />

                </FlowPane>
            </content>
        </ScrollPane>
    </children>
    <columnConstraints>
        <ColumnConstraints maxWidth="600.0" minWidth="392.0" prefWidth="400.0" />
    </columnConstraints>
    <rowConstraints>
        <RowConstraints maxHeight="200.0" minHeight="22.0" prefHeight="39.0" />
        <RowConstraints />
    </rowConstraints>
</GridPane>

这是控制器类:

public class Controller {

    static Parent graphMaker;
    @FXML
    StackPane workspaceSP;
    @FXML
    FlowPane lineButtonsFP;
    static LinkedList<Demand> curvesLL;
    ToggleGroup group = new ToggleGroup();
    public void insertDemand() {
        Demand demand = new Demand(workspaceSP,200,200,400,400);
        curvesLL.add(demand);
        RadioButton radioButton = new RadioButton("Demand");
        radioButton.setToggleGroup(group);
        radioButton.setSelected(true);
        lineButtonsFP.getChildren().add(radioButton);
    }
}

需求是我创建的一个类。创建后,显示一行:

public class Demand {

        public Demand(Pane pane, double startX, double startY, double endX, double endY){
            pane.getChildren().add(makeLine(startX, startY, endX, endY));
        }

        private Line makeLine(double startX, double startY, double endX, double endY) {
            Line line = new Line(startX, startY, endX, endY);
            return line;
        }
    }

}

感谢您的帮助:)

编辑 2: 我找到了如何让它工作。不知道为什么这可以解决问题,但是如果您将 insertDemand() 方法的前两行移到底部,以便首先制作单选按钮,然后创建 Deman 对象,那么它就可以工作了。

【问题讨论】:

  • 我不是这里的专家,但我猜您需要使用 Java API 在运行时添加新的单选按钮。
  • 为我工作。请发帖minimal reproducible example
  • 一个完整的 fxml+controller 类(+ 可能用于加载 fxml 的代码?)会告诉我们这个问题......我为完成这项工作所做的唯一步骤是包装 ButtonFlowPaneVBox 中,并将您的代码 sn-p 添加到控制器。顺便说一句:您的窗口/FlowPane 的父级不会太小而不能偶然显示FlowPane 的其他子级?
  • 感谢 Fabian 向我展示了这一点。我已经更新了这个问题。父级的大小似乎足够好。

标签: java javafx radio-button


【解决方案1】:

我做了一个简单的例子。我没有打扰 FXML,但我不明白为什么会有任何不同。如果您仍然遇到问题,您需要提供一个不起作用的示例。

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.RadioButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;


public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {
        try {
            BorderPane root = new BorderPane();
            Scene scene = new Scene(root,400,400);
            FlowPane lineButtonsFP = new FlowPane();
            root.setCenter(lineButtonsFP);
            ToggleGroup group = new ToggleGroup();

            Button add = new Button("add");
            root.setTop(add);
            add.setOnAction((action) ->
            {
                RadioButton radioButton = new RadioButton("Demand");
                radioButton.setToggleGroup(group);
                radioButton.setSelected(true);
                lineButtonsFP.getChildren().add(radioButton);                
            });
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        launch(args);
    }
}

【讨论】:

  • 感谢大卫花时间写出来。我已经更新了帖子,以尝试进一步找出不起作用的地方。欣赏一切!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-02-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-25
  • 1970-01-01
相关资源
最近更新 更多