我需要建议。我尝试从GridPane获取一个对象(按行和列索引)。我的方法返回节点,我不知道如何将它转换为另一个对象(例如,标签)或另一个方法,它返回其他东西。我使用了SceneBuilder。我知道我没有收到一个我必须得到的物品。
代码:
import java.net.URL;
import java.util.ResourceBundle;
import javafx.application.Platform;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.geometry.Insets;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.text.Text;
class XY{
public Node getNodeFromGridPane(GridPane gridPane, int col, int row) {
for (Node node : gridPane.getChildren()) {
if (GridPane.getColumnIndex(node) == col && GridPane.getRowIndex(node) == row) {
return node;
}
}
return null;
}
}
public class FXMLDocumentController implements Initializable {
@FXML
private GridPane grid;
@FXML
private Button button1;
@FXML
private Button button2;
@FXML
private TextField row;
@FXML
private TextField column;
@FXML
private Button send;
@FXML
private void handleButtonAction(ActionEvent event) {
Button button = (Button) event.getSource();
Text temp = new Text();
Platform.runLater(new Runnable(){
@Override
public void run(){
XY a = new XY();
int x=Integer.parseInt(column.getText());
int y=Integer.parseInt(row.getText());
Label label = (Label) a.getNodeFromGridPane(grid,x,y);
if (label!=null)
grid.getChildren().remove(label);
}
});
String name = button.getText();
temp.setText(name);
grid.add(temp,Integer.parseInt(column.getText()),Integer.parseInt(row.getText()));
grid.setMargin(temp,new Insets(10,10,10,10));
}
@Override
public void initialize(URL url, ResourceBundle rb) {
row.setText("0");
column.setText("0");
for (int i=0;i<9;i++)
for (int j=0;j<9;j++){
grid.add(new Label(),i,j);
}
}
}
FXML:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Text?>
<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="javafxapplication6.FXMLDocumentController">
<children>
<GridPane layoutX="10.0" layoutY="24.0" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="498.0" prefWidth="691.0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" maxWidth="482.0" minWidth="10.0" prefWidth="446.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="295.0" minWidth="10.0" prefWidth="154.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<GridPane fx:id="grid" gridLinesVisible="true" prefHeight="275.0" prefWidth="275.0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<opaqueInsets>
<Insets bottom="5.0" left="10.0" right="5.0" top="5.0" />
</opaqueInsets>
</GridPane>
<VBox prefHeight="200.0" prefWidth="100.0" GridPane.columnIndex="1">
<children>
<GridPane>
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<TextField fx:id="column" GridPane.rowIndex="1" />
<TextField fx:id="row" GridPane.columnIndex="1" GridPane.rowIndex="1" />
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="kolumna" wrappingWidth="100.92578125" />
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="wiersz" GridPane.columnIndex="1" />
<Button fx:id="button1" mnemonicParsing="false" onAction="#handleButtonAction" text="1" GridPane.rowIndex="2" />
<Button fx:id="button2" mnemonicParsing="false" onAction="#handleButtonAction" text="2" GridPane.columnIndex="1" GridPane.rowIndex="2" />
</children>
</GridPane>
</children>
</VBox>
</children>
</GridPane>
问题是:
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
at javafxapplication6.XY.getNodeFromGridPane(FXMLDocumentController.java:25)
at javafxapplication6.FXMLDocumentController$1.run(FXMLDocumentController.java:58)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
at java.lang.Thread.run(Thread.java:745)