在运行时创建新的单选按钮选项

时间:2016-11-03 16:49:47

标签: java javafx radio-button

我正在使用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>

这是Controller类:

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对象,那么它就能正常工作。

1 个答案:

答案 0 :(得分:0)

我做了一个有效的简单例子。我没有为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);
    }
}