嗨,我是FXML的新手,也是JavaFX的新手。
正如标题中所说的,我想创建一个带有GridPane单元格的64+。我的问题是:
我是否必须为这些64+单元编写所有代码,或者有一种方法(如for循环)来生成它们?
这是我的密码。我的计划是有一个由8x8 (甚至更多)的草细胞组成的网格。
草类
package auto;
import javafx.scene.paint.Color;
import javafx.scene.shape.*;
public class Grass extends Ground{
public Grass(){
ground= new Rectangle(50,50,Color.GREEN);
}
}地面级
package auto;
import javafx.scene.shape.Rectangle;
public abstract class Ground {
protected Rectangle ground;
}FXML文档
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.shape.*?>
<?import javafx.geometry.*?>
<StackPane alignment="CENTER"
xmlns:fx="http://javafx.com/fxml"
fx:controller="auto.FXMLDocumentController"
>
<Rectangle fx:id="Back" id="Back" height="700" width="500" />
<VBox id="APP" prefWidth="450" maxWidth="450" minWidth="450" >
<StackPane style="-fx-border-color: green; -fx-border-width: 2px;">
<Rectangle id="menu-style" height="50" width="450"/>
<HBox id="Menu">
<Button text="Start"/>
<Label> Current Cars</Label>
<Button text="Add Car"/>
</HBox>
</StackPane>
<GridPane id="Game" fx:id="Game">
//8x8 grid of Grass
</GridPane>
</VBox>
</StackPane>主要Java代码
package auto;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
public class RaceGame extends Application {
@Override
public void start( Stage stage ) throws Exception {
stage.setTitle("Race Game");
Parent root =FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
String css = this.getClass().getResource("RaceGame.css").toExternalForm();
Scene scene = new Scene(root);
scene.getStylesheets().add(css);
stage.setScene(scene);
stage.show();
}
/**
* @param args the command line arguments
*/
public static void main( String[] args ) {
launch(args);
}
}EDIT#1
我试过用控制器。问题是格拉斯不是节点。
package auto;
import javafx.fxml.FXML;
import javafx.scene.layout.GridPane;
public class FXMLDocumentController {
@FXML
private GridPane Game;
public FXMLDocumentController() {
for(int i=0;i<8;i++){
for(int j=0;j<8;j++){
Game.add(new Grass(), i, j);
}
}
}
}发布于 2018-01-03 14:29:07
回答
我用这样的方式解决了这个问题:
initialize()方法。与FXML控制器类的构造函数不同,初始化方法可以使用所有@FXML引用,因为它们是在构造函数之后和初始化方法之前加载的。Ground extends Rectangle,这样网格就可以接受地物。现在地面是一个节点,可以添加到网格中。地面级
package auto;
import javafx.scene.shape.Rectangle;
public abstract class Ground extends Rectangle{
}草类
package auto;
import javafx.scene.paint.Color;
public class Grass extends Ground{
Grass(){
this.setWidth(50);
this.setHeight(50);
this.setFill(Color.GREEN);
}
}FXML文件
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.shape.*?>
<?import javafx.geometry.*?>
<StackPane alignment="CENTER"
xmlns:fx="http://javafx.com/fxml"
fx:controller="auto.FXMLDocumentController"
>
<Rectangle fx:id="Back" id="Back" height="700" width="500" />
<VBox id="APP" prefWidth="450" maxWidth="450" minWidth="450" >
<StackPane style="-fx-border-color: green; -fx-border-width: 2px;">
<Rectangle id="menu-style" height="50" width="450"/>
<HBox id="Menu">
<Button text="Start"/>
<Label> Current Cars</Label>
<Button text="Add Car"/>
</HBox>
</StackPane>
<GridPane id="Game" fx:id="Game">
</GridPane>
</VBox>
</StackPane>FXML控制器
package auto;
import javafx.fxml.FXML;
import javafx.scene.layout.GridPane;
public class FXMLDocumentController {
@FXML
private GridPane Game;
public void initialize() {
for(int i=0;i<8;i++){
for(int j=0;j<8;j++){
Game.add(new Grass(), i, j);
}
}
}
}发布于 2018-01-03 21:19:47
我知道你似乎自己回答了这个问题,但无论如何,我还是会把这个留在这里的,看来你把这件事复杂化了。
import javafx.fxml.Initializable;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import java.net.URL;
import java.util.ResourceBundle;
public class FXMLDocumentController implements Initializable {
public Rectangle Back;
public GridPane Game;
private void createGrass(){
for(int i=0;i<8;i++){
for(int j=0;j<8;j++){
Game.add(new Rectangle(50,50, Color.GREEN), i, j);
}
}
}
@Override
public void initialize(URL location, ResourceBundle resources) {
createGrass();
}
}https://stackoverflow.com/questions/48076233
复制相似问题