我有一个带有网格窗格的 FXML 文件,并且想使用 JavaFX 在其中添加另一个项目。
如果我不使用 FXML 文件,我可以在网格窗格中添加项目。
如何在 fxml 网格窗格中添加项目?(想了一些getChildren.add,但找不到)
为此,我将使用一个简单的标签,但在我的程序中,我有 100 多行代码。
主.java:
package fotos;
import java.io.IOException;
import java.io.InputStream;
import javafx.scene.control.Label;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
/**
*
* @author Geert
*/
public class Fotos extends Application
{
private static Stage primaryStage;
@Override
public void start(Stage stage)
{
primaryStage = stage;
openWindow(this.getClass(), "/fotos/SelectPicture.fxml");
}
public static void openWindow(Class<?> c, String fxml)
{
Scene scene = null;
try
{
Parent root = FXMLLoader.load(c.getResource(fxml));
scene = new Scene(root);
}
catch (IOException ex)
{
System.out.println(ex.getMessage());
return;
}
primaryStage.setResizable(false);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args)
{
launch(args);
}
}
选择图片.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.paint.*?>
<?import javafx.scene.text.*?>
<AnchorPane id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2">
<children>
<GridPane fx:id="gridPanePhoto" layoutX="14.0" layoutY="24.0" prefHeight="362.0" prefWidth="572.0">
<children>
<TreeView fx:id="mapTree" prefHeight="372.0" prefWidth="162.0" GridPane.columnIndex="0" GridPane.rowIndex="1" />
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="Maps" GridPane.columnIndex="0" GridPane.rowIndex="0" />
<Text scaleX="1.0" scaleY="0.9999999999607461" strokeType="OUTSIDE" strokeWidth="0.0" text="Picture's" GridPane.columnIndex="1" GridPane.rowIndex="0" />
</children>
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" maxWidth="284.0" minWidth="10.0" prefWidth="188.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="440.0" minWidth="10.0" prefWidth="384.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints maxHeight="186.0" minHeight="10.0" prefHeight="32.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="344.0" minHeight="10.0" prefHeight="340.0" vgrow="SOMETIMES" />
</rowConstraints>
</GridPane>
<MenuBar layoutX="0.0" layoutY="0.0" prefWidth="600.0">
<menus>
<Menu mnemonicParsing="false" text="File">
<items>
<MenuItem mnemonicParsing="false" text="Close" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Edit">
<items>
<MenuItem mnemonicParsing="false" text="Delete" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Help">
<items>
<MenuItem mnemonicParsing="false" text="About" />
</items>
</Menu>
</menus>
</MenuBar>
</children>
</AnchorPane>
选择图片控制器.java:
package fotos;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TreeView;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import javafx.scene.control.Label;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
/**
* FXML Controller class
*
* @author Geert
*/
public class SelectPictureController implements Initializable
{
@FXML
private TreeView<?> mapTree;
@FXML
private ImageView dutchFlag;
@FXML
private ImageView englishFlag;
@FXML
private GridPane gridPanePhoto;
/**
* Initializes the controller class.
*/
@Override
public void initialize(URL url, ResourceBundle rb)
{
Label test = new Label("Test");
gridPanePhoto.add(test,0,1);
}
@FXML
private void dutchClicked(MouseEvent event)
{
//TODO
}
@FXML
private void englishClicked(MouseEvent event)
{
//TODO
}
}