- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在 JavaFx 中开发一个应用程序,其中我在 GridPane 中动态创建 TextFeids 和 CheckBox,如下所示:
我正在添加用户将在 TextField 1 和 TextField 2 中输入的数字,并使用监听器在 TextField 3 中显示,如下所示:
问题:我想要的是,当用户检查复选框时,它应该将 10 添加到 的 TextField 3 中已存在的值(与触发的复选框相同的行) 并更新 TextField 3 的文本,当用户取消选中 CheckBox 时,应从 (与触发的 CheckBox 相同的行) 的 TextField 3 中存在的值减去 10 并更新 TextField 的文本3.我尝试这样做,但它不起作用(不添加和删除):
这就是我创建 GridPane 的方式:
public static GridPane table(int rows){
GridPane table = new GridPane();
for(int i=0; i<rows; i++){
TextField textField = new TextField();
textField.setAlignment(Pos.CENTER);
TextField textField2 = new TextField();
textField2.setAlignment(Pos.CENTER);
CheckBox checkBox = new CheckBox("Check Box");
checkBox.setTextFill(Color.WHITE);
checkBox.setAlignment(Pos.CENTER);
TextField textField3 = new TextField();
textField3.setAlignment(Pos.CENTER);
table.add(textField, 0, i);
table.add(textField2, 1, i);
table.add(checkBox , 2, i);
table.add(textField3,3, i);
GridPane.setMargin(textField, new Insets(5));
GridPane.setMargin(textField2, new Insets(5));
GridPane.setMargin(checkBox, new Insets(5));
GridPane.setMargin(textField3, new Insets(5));
}
table.setAlignment(Pos.CENTER);
return table;
}
从表中特定行和列返回组件的方法
public static Node getComponent (int row, int column, GridPane table) {
for (Node component : table.getChildren()) {
if(GridPane.getRowIndex(component) == row &&
GridPane.getColumnIndex(component) == column) {
return component;
}
}
return null;
}
这就是我添加的方式:
public void add(GridPane table, int numRows){
for(int i=0; i<numRows; i++){
try {
int valueA = Integer.parseInt(((TextField)(getComponent (i, 0, table))).getText());
System.out.println(valueA);
int valueB = Integer.parseInt(((TextField)(getComponent (i, 1, table))).getText());
System.out.println(valueB);
int add = valueA+valueB;
String addToString = Integer.toString(add);
((TextField)(getComponent (i, 3, table))).setText(addToString);
} catch (NullPointerException e) {
System.out.print("Caught the NullPointerException");
}
}
}
问题:使用 CheckBox 进行加法和减法:
public void addPause(GridPane table, int numRows){
for(int i=0; i<numRows; i++){
boolean pause = ((CheckBox)(getComponent (i, 2, table))).isSelected();
int getTextValue = Integer.parseInt(((TextField)(getComponent (i, 3, table))).getText());
int addPause = getTextValue+10;
int removePause = addPause-10;
String addPToString = Integer.toString(addPause);
String removePToString = Integer.toString(removePause);
if (pause) {
((TextField)(getComponent (i, 3, table))).setText(addPToString);
} else {
((TextField)(getComponent (i, 3, table))).setText(removePToString);
}
}
}
这就是我使用监听器触发的方式:
for(Node node : table.getChildren()){
if(node instanceof TextField){
((TextField)node).textProperty().addListener((obs, old, newV)->{
add(table, numRows);
});
}
else if(node instanceof CheckBox){
((CheckBox)node).selectedProperty().addListener((obs, old, newV)->{
addPause(table, numRows);
});
}
}
最佳答案
正如我在您的 previous question 中所建议的那样,只需在创建控件时注册它们的监听器即可。
实际上,您可以仅对两个文本字段和复选框使用单个监听器(每行中),并在其中任何一个发生更改时更新第三个文本框。例如:
public static GridPane table(int rows){
GridPane table = new GridPane();
for(int i=0; i<rows; i++){
TextField textField = new TextField();
textField.setAlignment(Pos.CENTER);
TextField textField2 = new TextField();
textField2.setAlignment(Pos.CENTER);
CheckBox checkBox = new CheckBox("Check Box");
checkBox.setTextFill(Color.WHITE);
checkBox.setAlignment(Pos.CENTER);
TextField textField3 = new TextField();
textField3.setAlignment(Pos.CENTER);
table.add(textField, 0, i);
table.add(textField2, 1, i);
table.add(checkBox , 2, i);
table.add(textField3,3, i);
ChangeListener<Object> listener = (obs, oldValue, newValue) ->
updateTotalField(textField.getText(), textField2.getText(), checkBox.isSelected(), textField3);
textField.textProperty().addListener(listener);
textField2.textProperty().addListener(listener);
checkBox.selectedProperty().addListener(listener);
GridPane.setMargin(textField, new Insets(5));
GridPane.setMargin(textField2, new Insets(5));
GridPane.setMargin(checkBox, new Insets(5));
GridPane.setMargin(textField3, new Insets(5));
}
table.setAlignment(Pos.CENTER);
return table;
}
private static void updateTotalField(String text1, String text2, boolean addPause, TextField output) {
int value1 = parseText(text1);
int value2 = parseText(text2);
int total = value1 + value2 ;
if (addPause) {
total += 10 ;
}
output.setText(Integer.toString(total));
}
private static int parseText(String text) {
// if text is a valid integer:
if (text.matches("\\d+")) {
return Integer.parseInt(text);
} else {
return 0 ;
}
}
现在您可以摆脱 add
和 addPause
方法,以及您在迭代网格 Pane 的子节点并添加监听器时发布的代码块。您也许可以摆脱 getComponent()
方法,除非您在其他地方需要它。
这是 SSCCE 的代码
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class TableOfControlsInGridPane extends Application {
@Override
public void start(Stage primaryStage) {
Scene scene = new Scene(table(10));
primaryStage.setScene(scene);
primaryStage.show();
}
public GridPane table(int rows){
GridPane table = new GridPane();
for(int i=0; i<rows; i++){
TextField textField = new TextField();
textField.setAlignment(Pos.CENTER);
TextField textField2 = new TextField();
textField2.setAlignment(Pos.CENTER);
CheckBox checkBox = new CheckBox("Check Box");
checkBox.setTextFill(Color.WHITE);
checkBox.setAlignment(Pos.CENTER);
TextField textField3 = new TextField();
textField3.setAlignment(Pos.CENTER);
table.add(textField, 0, i);
table.add(textField2, 1, i);
table.add(checkBox , 2, i);
table.add(textField3,3, i);
ChangeListener<Object> listener = (obs, oldValue, newValue) ->
updateTotalField(textField.getText(), textField2.getText(), checkBox.isSelected(), textField3);
textField.textProperty().addListener(listener);
textField2.textProperty().addListener(listener);
checkBox.selectedProperty().addListener(listener);
GridPane.setMargin(textField, new Insets(5));
GridPane.setMargin(textField2, new Insets(5));
GridPane.setMargin(checkBox, new Insets(5));
GridPane.setMargin(textField3, new Insets(5));
}
table.setAlignment(Pos.CENTER);
return table;
}
private void updateTotalField(String text1, String text2, boolean addPause, TextField output) {
int value1 = parseText(text1);
int value2 = parseText(text2);
int total = value1 + value2 ;
if (addPause) {
total += 10 ;
}
output.setText(Integer.toString(total));
}
private int parseText(String text) {
// if text is a valid integer:
if (text.matches("\\d+")) {
return Integer.parseInt(text);
} else {
return 0 ;
}
}
public static void main(String[] args) {
launch(args);
}
}
由于 Java 是一种面向对象的语言,因此首先以面向对象的方式来完成此操作似乎更合适。我不知道您在此 View 中表示什么数据,但您应该定义一个将数据封装在单行中的类:
import javafx.beans.binding.Bindings;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.ReadOnlyIntegerProperty;
import javafx.beans.property.ReadOnlyIntegerWrapper;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleIntegerProperty;
// Obviously use a more sensible name for the class:
public class RowData {
private final IntegerProperty firstValue = new SimpleIntegerProperty();
private final IntegerProperty secondValue = new SimpleIntegerProperty();
private final BooleanProperty includePause = new SimpleBooleanProperty();
private final ReadOnlyIntegerWrapper total = new ReadOnlyIntegerWrapper();
public RowData() {
total.bind(Bindings.createIntegerBinding(() -> {
int total = getFirstValue() + getSecondValue() ;
if (isIncludePause()) {
total += 10 ;
}
return total ;
}, firstValue, secondValue, includePause));
}
public final IntegerProperty firstValueProperty() {
return this.firstValue;
}
public final int getFirstValue() {
return this.firstValueProperty().get();
}
public final void setFirstValue(final int firstValue) {
this.firstValueProperty().set(firstValue);
}
public final IntegerProperty secondValueProperty() {
return this.secondValue;
}
public final int getSecondValue() {
return this.secondValueProperty().get();
}
public final void setSecondValue(final int secondValue) {
this.secondValueProperty().set(secondValue);
}
public final BooleanProperty includePauseProperty() {
return this.includePause;
}
public final boolean isIncludePause() {
return this.includePauseProperty().get();
}
public final void setIncludePause(final boolean includePause) {
this.includePauseProperty().set(includePause);
}
public final ReadOnlyIntegerProperty totalProperty() {
return this.total.getReadOnlyProperty();
}
public final int getTotal() {
return this.totalProperty().get();
}
}
然后是一个显示这些数据的类:
import javafx.beans.binding.Bindings;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.StringProperty;
import javafx.scene.control.CheckBox;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
public class RowDataView {
private final TextField firstValueField ;
private final TextField secondValueField ;
private final CheckBox includePauseBox ;
private final TextField totalField ;
public RowDataView(RowData data) {
firstValueField = new TextField();
bindToStringProperty(data.firstValueProperty(), firstValueField.textProperty());
secondValueField = new TextField();
bindToStringProperty(data.secondValueProperty(), secondValueField.textProperty());
includePauseBox = new CheckBox();
data.includePauseProperty().bind(includePauseBox.selectedProperty());
totalField = new TextField();
totalField.setEditable(false);
totalField.textProperty().bind(data.totalProperty().asString());
}
public void addToGridPane(GridPane pane, int row, int firstValueColumn, int secondValueColumn, int checkboxColumn, int totalColumn) {
pane.add(firstValueField, firstValueColumn, row);
pane.add(secondValueField, secondValueColumn, row);
pane.add(includePauseBox, checkboxColumn, row);
pane.add(totalField, totalColumn, row);
}
public void addToGridPane(GridPane pane, int row, int column) {
addToGridPane(pane, row, column, column+1, column+2, column+3);
}
public void addToGridPane(GridPane pane, int row) {
addToGridPane(pane, row, 0);
}
private void bindToStringProperty(IntegerProperty p, StringProperty s) {
p.bind(Bindings.createIntegerBinding(
() -> {
if (s.get().matches("\\d+")) {
return Integer.parseInt(s.get());
}
return 0 ;
}, s));
}
}
这是一些测试代码:
import java.util.ArrayList;
import java.util.List;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
public class TableOfControlsInGridPane extends Application {
private final List<RowData> data = new ArrayList<>();
@Override
public void start(Stage primaryStage) {
Scene scene = new Scene(table(10), 800, 800);
scene.getStylesheets().add("style.css");
primaryStage.setScene(scene);
primaryStage.show();
}
public GridPane table(int rows){
GridPane table = new GridPane();
table.getStyleClass().add("data-grid");
data.clear();
for(int i=0; i<rows; i++){
RowData rowData = new RowData();
data.add(rowData);
RowDataView rowDataView = new RowDataView(rowData);
rowDataView.addToGridPane(table, i);
}
table.setAlignment(Pos.CENTER);
return table;
}
public static void main(String[] args) {
launch(args);
}
}
我将样式移至样式表 (style.css
):
.data-grid {
-fx-background-color: purple ;
-fx-hgap: 10 ;
-fx-vgap: 10 ;
}
.data-grid .text-field, .data-grid .check-box {
-fx-alignment: center ;
}
.data-grid .check-box {
-fx-text-fill: white ;
}
关于JavaFx:如何更新 GridPane 内动态创建的 TextField 的文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44413649/
我正在阅读一些有关 JavaFX 2.0 的教程,我对 GridPane 布局组件的两种方法有点困惑。 基本上,我会使用 GridPane.add(new Label("My Label"), 0,
我正在使用 JavaFX 8 创建一个应用程序。我使用拖/放动态更改网格 Pane 的内容。我希望每行或每行/列迭代 GridPane 内容。JavaFX 允许通过指定行和列在 GridPane 中添
我想检索网格 Pane 中一个特定单元格的内容。我在单元格中放置了按钮 setConstraints(btt , 0 ,1 ) setConstraints(btt , 0 ,2 ) getChild
基本上,我只想将图像插入网格 Pane 内的单元格中。 GridPane gridpane = new GridPane(); gridpane.add(new Image("File:image/m
我已经使用了GridPane.getChildren().clear(),但是它不起作用。 最佳答案 请注意,gridLinesVisible 属性是一项调试功能,不应依赖它来创建特定的外观。 话虽这
所以我有一个登录页面。如果您点击“还不是用户?立即注册!”它应该在同一窗口中打开一个新页面。现在我正在打开第二页,但它位于单独的窗口中。它还会在打开注册页面时创建错误消息。这是代码: 主要 publi
我正在使用数字键盘制作登录屏幕,但我似乎无法将 Pane 中按钮的网格 Pane 居中对齐。我做错了什么? Main.java import javafx.application.Applicatio
我正在尝试使用 JavaFX 创建一个场景,其中程序的标题位于顶部中心,按钮位于场景左侧的垂直线上。然而,这两个元素都聚集在场景的右上角,而不是我想要的位置。 如何让这些元素显示在我想要的位置? 以下
我对 JavaFX 比较陌生,并且我一直很困惑为什么下面的代码没有产生添加到网格的标签的预期结果。 我想做的是运行一个测试,将 JavaFX 标签添加到我的 FXML GridPane 中,因为我想在
作为一项作业,我正在 JavaFX 中制作一个老虎机程序。 如果我这样做: GridPane grid = new GridPane(); grid.add(new Rectangle(20, 20)
我正在尝试使用 2 列创建一个具有特定布局的窗口,但我无法完全让它按照我想要的方式工作。 首先,一些简化的示例代码: import javafx.application.Application; im
我是 JavaFX 新手,目前我尝试构建一个小型聊天窗口。为此,我为 ListView 设置了以下自定义单元格布局。 Grid 我使用外部 HBox,因此我可以像 Whatsapp... 那样对齐消息
我正在从数据库加载数据并在 GridPane 中动态创建按钮!但数据量很大时,要花很长时间才能看到按钮!我正在使用线程从数据库加载数据,但似乎创建每个按钮并填充 GridPane 花费的时间太长! *
您好,我目前正在使用 JavaFX 中的 GridPane,偶然发现了一个问题...我想创建一个包含三行的布局,其中中间一行会增长并占用所有可用空间,但我无法得到它工作。中间的行变得太大,并将窗口下方
对于 GUI,我必须将多个按钮设置为 GridPane。为此,我想问一下是否有一种方法可以比我更优雅地在网格中添加多个按钮。这是我的代码的摘录。 Button button0 =new Button(
我正在编写一个生成随机颜色网格的 JavaFX 程序。调整窗口大小时,网格应调整为尽可能大,同时仍保持正方形并在底部为文本留出空间。 一切正常,但我遇到的问题是调整大小时 GridPane 中留下的间
我正在使用 JavaFX 8。尝试使用 GridPage 来显示一组属性和值。有些值可能太长,无法放在一行中。我想包装这些值,但似乎无法弄清楚如何使网格 Pane 垂直扩展以容纳包装的文本。我基本上想
我正在尝试使用 GridPane 创建登录 UI。我的窗口以 300 x 300 打开,并且我已将行和列索引设置为某些值,以便我的文本“用户 ID”和“密码”出现在窗口的中心(与所有四个边的距离相等)
我有这个构造函数,我正在尝试用标签填充网格 Pane 。我碰壁了,不知道出了什么问题。我需要在 1 行中创建 13 个标签。 构造函数: public class Labels { @FXML
我已经在我的游戏中实现了拖放功能,但到目前为止我只能“拖放”到硬编码位置。如图所示: 我想要的是: 当飞船被放下时,它的 x,y 值(相对于 GridPane)被保存,或者 保存飞船掉落的单元格。 我
我是一名优秀的程序员,十分优秀!