【发布时间】: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 的代码?)会告诉我们这个问题......我为完成这项工作所做的唯一步骤是包装
Button和FlowPane在VBox中,并将您的代码 sn-p 添加到控制器。顺便说一句:您的窗口/FlowPane的父级不会太小而不能偶然显示FlowPane的其他子级? -
感谢 Fabian 向我展示了这一点。我已经更新了这个问题。父级的大小似乎足够好。
标签: java javafx radio-button