【发布时间】:2021-10-21 13:55:10
【问题描述】:
我需要制作一个 JavaFX 程序,其中用户在使用 FXML 构建组件时使用文本字段输入名称并使用切换按钮输入速度。我已经为此工作了几天,但没有任何运气。
<TableView fx:id="table" layoutX="200.0" prefHeight="420.0" prefWidth="461.0">
<columns>
<TableColumn fx:id="firstn" prefWidth="75.0" text="First Name" />
<TableColumn fx:id="lastn" prefWidth="75.0" text="Last Name" />
<TableColumn fx:id="addresss" prefWidth="75.0" text="Address" />
<TableColumn fx:id="speeed" prefWidth="84.0" text="Speed(mb/s)" />
<TableColumn fx:id="bwidth" prefWidth="75.0" text="Bandwidth" />
<TableColumn fx:id="durationn" prefWidth="75.0" text="Duration" />
</columns>
</TableView>
<TextField fx:id="fname" layoutX="14.0" layoutY="46.0" promptText="First Name" />
<TextField fx:id="lname" layoutX="14.0" layoutY="71.0" promptText="Last Name" />
<TextField fx:id="address" layoutX="14.0" layoutY="96.0" promptText="Address" />
<Button layoutX="31.0" layoutY="339.0" mnemonicParsing="false" onAction="#save1" text="Save Package" />
<ToggleButton fx:id="sp1" layoutX="11.0" layoutY="169.0" mnemonicParsing="false" text="2">
<toggleGroup>
<ToggleGroup fx:id="speed" />
</toggleGroup>
</ToggleButton>
<ToggleButton fx:id="sp2" layoutX="34.0" layoutY="169.0" mnemonicParsing="false" text="5" toggleGroup="$speed" />
<ToggleButton fx:id="sp3" layoutX="57.0" layoutY="169.0" mnemonicParsing="false" text="10" toggleGroup="$speed" />
<ToggleButton fx:id="sp4" layoutX="85.0" layoutY="169.0" mnemonicParsing="false" text="20" toggleGroup="$speed" />
<ToggleButton fx:id="sp5" layoutX="114.0" layoutY="169.0" mnemonicParsing="false" text="50" toggleGroup="$speed" />
<ToggleButton fx:id="sp6" layoutX="143.0" layoutY="169.0" mnemonicParsing="false" text="100" toggleGroup="$speed" />
<ToggleButton fx:id="ba1" layoutX="13.0" layoutY="237.0" mnemonicParsing="false" text="1">
<toggleGroup>
<ToggleGroup fx:id="bandw" />
</toggleGroup>
</ToggleButton>
<ToggleButton fx:id="ba2" layoutX="37.0" layoutY="237.0" mnemonicParsing="false" text="5" toggleGroup="$bandw" />
<ToggleButton fx:id="ba3" layoutX="61.0" layoutY="237.0" mnemonicParsing="false" text="10" toggleGroup="$bandw" />
<ToggleButton fx:id="ba4" layoutX="89.0" layoutY="237.0" mnemonicParsing="false" text="100" toggleGroup="$bandw" />
<ToggleButton fx:id="ba5" layoutX="125.0" layoutY="237.0" mnemonicParsing="false" text="Flat" toggleGroup="$bandw" />
<ToggleButton fx:id="du1" layoutX="13.0" layoutY="295.0" mnemonicParsing="false" text="1 year">
<toggleGroup>
<ToggleGroup fx:id="duration" />
</toggleGroup>
</ToggleButton>
<ToggleButton fx:id="du2" layoutX="70.0" layoutY="295.0" mnemonicParsing="false" text="2 years" toggleGroup="$duration" />
@FXML
private TextField fname;
@FXML
private TextField lname;
@FXML
private TextField address;
@FXML
private ToggleGroup speed;
@FXML
private ToggleGroup bandw;
@FXML
private ToggleGroup duration;
@FXML
private TableView<Person> table = new TableView<Person>();
public void save1(ActionEvent e){
ObservableList<Person> data = table.getItems();
data.add(new Person(fname.getText(),lname.getText(),address.getText(),
speed.getSelectedToggle(),bandw.getSelectedToggle(),duration.getSelectedToggle()));
table.setItems(data);
}
基本上,我的程序应该是这样的,但是我无法将保存按钮添加到 tableview。
【问题讨论】:
-
从不将通过
@FXML初始化的变量设置为new值。 -
你能把它扩展成minimal reproducible example吗?
-
要添加到
TableView,只需将一个新对象添加到您在其中显示的List<Person>。您无需多次致电table.setItems()。一个简单的table.getItems().add(new Person())就足够了。话虽如此,没有minimal reproducible example,我们无法确定您的问题是什么。 -
无关:最好学习 Java 命名约定并坚持下去。这将帮助您避免使用诸如“速度”、“持续时间”和“地址”等令人困惑的
fx:id值。