我似乎无法在场景中添加新对象。现在,当你点击绿色方块时,它运行方法选择,当你点击一个按钮时,它运行newcity方法,并且应该创建一个蓝色方块。我已经尝试将我的矩形添加到父级,但我总是得到错误。在下面的代码中,City Class中的getChildren()用红色加下划线!
这是我的代码:
主类:
package application;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
public void start(Stage primaryStage) {
try {
Parent root = FXMLLoader.load(getClass().getResource("/fxml/Map1.fxml"));
Scene scene = new Scene(root,576,352);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setResizable(false);
primaryStage.setScene(scene);
primaryStage.setTitle("Map");
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
MainControl类:
package application;
import java.net.URL;
import java.util.ArrayList;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.event.Event;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.shape.Rectangle;
public class MainControl implements Initializable {
boolean selected = false;
Node selectnode;
public void initialize(URL arg0, ResourceBundle arg1) {
};
public void select(Event event) {
selectnode = (Node) event.getSource();
selected = true;
}
public void newcity(ActionEvent event){
City ncity = new City();
ncity.newcity(selectnode.getLayoutX(), selectnode.getLayoutY(),
selectnode, event);
}
}
城市类:
package application;
import javafx.event.ActionEvent;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.shape.Rectangle;
public class City {
public void newcity(double x,double y, Node n,ActionEvent event){
Rectangle cityshape = new Rectangle(n.getLayoutX(), n.getLayoutY(),32, 32);
Parent root = n.getParent();
root.getChildren().add(cityshape); //getChildren() is underlined in red
}
}
Map1 FXML:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.image.*?>
<?import javafx.scene.*?>
<?import javafx.scene.effect.*?>
<?import javafx.scene.shape.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.text.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="352.0" prefWidth="576.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.MainControl">
<children>
<TextField fx:id="textBox" layoutX="2.0" opacity="0.01" prefHeight="352.0" prefWidth="576.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<cursor>
<Cursor fx:constant="DEFAULT" />
</cursor>
<effect>
<ColorAdjust />
</effect>
</TextField>
<Rectangle fx:id="tile1" fill="#26b522" height="32.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile20" fill="#26b522" height="32.0" layoutX="32.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile2" fill="#26b522" height="32.0" layoutX="64.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile3" fill="#26b522" height="32.0" layoutX="96.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile4" fill="#26b522" height="32.0" layoutX="128.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile5" fill="#26b522" height="32.0" layoutX="160.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile6" fill="#26b522" height="32.0" layoutX="192.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile7" fill="#26b522" height="32.0" layoutX="224.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile8" fill="#26b522" height="32.0" layoutX="256.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile9" fill="#26b522" height="32.0" layoutX="288.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile10" fill="#26b522" height="32.0" layoutX="320.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile101" fill="#26b522" height="32.0" layoutX="320.0" layoutY="32.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile91" fill="#26b522" height="32.0" layoutX="288.0" layoutY="32.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile81" fill="#26b522" height="32.0" layoutX="256.0" layoutY="32.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile71" fill="#26b522" height="32.0" layoutX="224.0" layoutY="32.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile61" fill="#26b522" height="32.0" layoutX="192.0" layoutY="32.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile51" fill="#26b522" height="32.0" layoutX="160.0" layoutY="32.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile41" fill="#26b522" height="32.0" layoutX="128.0" layoutY="32.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile31" fill="#26b522" height="32.0" layoutX="96.0" layoutY="32.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile21" fill="#26b522" height="32.0" layoutX="64.0" layoutY="32.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile30" fill="#26b522" height="32.0" layoutX="32.0" layoutY="32.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile11" fill="#26b522" height="32.0" layoutY="32.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile102" fill="#26b522" height="32.0" layoutX="320.0" layoutY="64.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile92" fill="#26b522" height="32.0" layoutX="288.0" layoutY="64.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile82" fill="#26b522" height="32.0" layoutX="256.0" layoutY="64.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile72" fill="#26b522" height="32.0" layoutX="224.0" layoutY="64.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile62" fill="#26b522" height="32.0" layoutX="192.0" layoutY="64.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile52" fill="#26b522" height="32.0" layoutX="160.0" layoutY="64.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile42" fill="#26b522" height="32.0" layoutX="128.0" layoutY="64.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile32" fill="#26b522" height="32.0" layoutX="96.0" layoutY="64.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile22" fill="#26b522" height="32.0" layoutX="64.0" layoutY="64.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile40" fill="#26b522" height="32.0" layoutX="32.0" layoutY="64.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile12" fill="#26b522" height="32.0" layoutY="64.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile103" fill="#26b522" height="32.0" layoutX="320.0" layoutY="96.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile93" fill="#26b522" height="32.0" layoutX="288.0" layoutY="96.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile83" fill="#26b522" height="32.0" layoutX="256.0" layoutY="96.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile73" fill="#26b522" height="32.0" layoutX="224.0" layoutY="96.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile63" fill="#26b522" height="32.0" layoutX="192.0" layoutY="96.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile53" fill="#26b522" height="32.0" layoutX="160.0" layoutY="96.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile43" fill="#26b522" height="32.0" layoutX="128.0" layoutY="96.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile33" fill="#26b522" height="32.0" layoutX="96.0" layoutY="96.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile23" fill="#26b522" height="32.0" layoutX="64.0" layoutY="96.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile50" fill="#26b522" height="32.0" layoutX="32.0" layoutY="96.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile13" fill="#26b522" height="32.0" layoutY="96.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile104" fill="#26b522" height="32.0" layoutX="320.0" layoutY="128.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile94" fill="#26b522" height="32.0" layoutX="288.0" layoutY="128.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile84" fill="#26b522" height="32.0" layoutX="256.0" layoutY="128.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile74" fill="#26b522" height="32.0" layoutX="224.0" layoutY="128.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile64" fill="#26b522" height="32.0" layoutX="192.0" layoutY="128.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile54" fill="#26b522" height="32.0" layoutX="160.0" layoutY="128.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile44" fill="#26b522" height="32.0" layoutX="128.0" layoutY="128.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile34" fill="#26b522" height="32.0" layoutX="96.0" layoutY="128.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile24" fill="#26b522" height="32.0" layoutX="64.0" layoutY="128.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile60" fill="#26b522" height="32.0" layoutX="32.0" layoutY="128.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile14" fill="#26b522" height="32.0" layoutY="128.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile105" fill="#26b522" height="32.0" layoutX="320.0" layoutY="160.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile95" fill="#26b522" height="32.0" layoutX="288.0" layoutY="160.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile85" fill="#26b522" height="32.0" layoutX="256.0" layoutY="160.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile75" fill="#26b522" height="32.0" layoutX="224.0" layoutY="160.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile65" fill="#26b522" height="32.0" layoutX="192.0" layoutY="160.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile55" fill="#26b522" height="32.0" layoutX="160.0" layoutY="160.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile45" fill="#26b522" height="32.0" layoutX="128.0" layoutY="160.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile35" fill="#26b522" height="32.0" layoutX="96.0" layoutY="160.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile25" fill="#26b522" height="32.0" layoutX="64.0" layoutY="160.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile70" fill="#26b522" height="32.0" layoutX="32.0" layoutY="160.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile15" fill="#26b522" height="32.0" layoutY="160.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile106" fill="#26b522" height="32.0" layoutX="320.0" layoutY="192.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile96" fill="#26b522" height="32.0" layoutX="288.0" layoutY="192.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile86" fill="#26b522" height="32.0" layoutX="256.0" layoutY="192.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile76" fill="#26b522" height="32.0" layoutX="224.0" layoutY="192.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile66" fill="#26b522" height="32.0" layoutX="192.0" layoutY="192.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile56" fill="#26b522" height="32.0" layoutX="160.0" layoutY="192.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile46" fill="#26b522" height="32.0" layoutX="128.0" layoutY="192.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile36" fill="#26b522" height="32.0" layoutX="96.0" layoutY="192.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile26" fill="#26b522" height="32.0" layoutX="64.0" layoutY="192.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile80" fill="#26b522" height="32.0" layoutX="32.0" layoutY="192.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile16" fill="#26b522" height="32.0" layoutY="192.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile107" fill="#26b522" height="32.0" layoutX="320.0" layoutY="224.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile97" fill="#26b522" height="32.0" layoutX="288.0" layoutY="224.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile87" fill="#26b522" height="32.0" layoutX="256.0" layoutY="224.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile77" fill="#26b522" height="32.0" layoutX="224.0" layoutY="224.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile67" fill="#26b522" height="32.0" layoutX="192.0" layoutY="224.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile57" fill="#26b522" height="32.0" layoutX="160.0" layoutY="224.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile47" fill="#26b522" height="32.0" layoutX="128.0" layoutY="224.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile37" fill="#26b522" height="32.0" layoutX="96.0" layoutY="224.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile27" fill="#26b522" height="32.0" layoutX="64.0" layoutY="224.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile90" fill="#26b522" height="32.0" layoutX="32.0" layoutY="224.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile17" fill="#26b522" height="32.0" layoutY="224.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile108" fill="#26b522" height="32.0" layoutX="320.0" layoutY="256.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile98" fill="#26b522" height="32.0" layoutX="288.0" layoutY="256.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile88" fill="#26b522" height="32.0" layoutX="256.0" layoutY="256.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile78" fill="#26b522" height="32.0" layoutX="224.0" layoutY="256.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile68" fill="#26b522" height="32.0" layoutX="192.0" layoutY="256.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile58" fill="#26b522" height="32.0" layoutX="160.0" layoutY="256.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile48" fill="#26b522" height="32.0" layoutX="128.0" layoutY="256.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile38" fill="#26b522" height="32.0" layoutX="96.0" layoutY="256.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile28" fill="#26b522" height="32.0" layoutX="64.0" layoutY="256.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile110" fill="#26b522" height="32.0" layoutX="32.0" layoutY="256.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile18" fill="#26b522" height="32.0" layoutY="256.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile109" fill="#26b522" height="32.0" layoutX="320.0" layoutY="288.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile99" fill="#26b522" height="32.0" layoutX="288.0" layoutY="288.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile89" fill="#26b522" height="32.0" layoutX="256.0" layoutY="288.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile79" fill="#26b522" height="32.0" layoutX="224.0" layoutY="288.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile69" fill="#26b522" height="32.0" layoutX="192.0" layoutY="288.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile59" fill="#26b522" height="32.0" layoutX="160.0" layoutY="288.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile49" fill="#26b522" height="32.0" layoutX="128.0" layoutY="288.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile39" fill="#26b522" height="32.0" layoutX="96.0" layoutY="288.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile29" fill="#26b522" height="32.0" layoutX="64.0" layoutY="288.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile120" fill="#26b522" height="32.0" layoutX="32.0" layoutY="288.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile19" fill="#26b522" height="32.0" layoutY="288.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="r" arcHeight="15.0" arcWidth="15.0" fill="#ff1f1f" height="32.0" layoutX="256.0" layoutY="160.0" onMouseClicked="#move" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Label layoutX="495.0" layoutY="6.0" prefHeight="17.0" prefWidth="60.0" text="Moves left:" />
<Label fx:id="movelabel" layoutX="553.0" layoutY="6.0" prefHeight="17.0" prefWidth="21.0" />
<Button mnemonicParsing="false" onAction="#newcity" text="Found City" />
</children>
</AnchorPane>
请帮助
感谢
答案 0 :(得分:3)
我建议创建一个class City
extends Rectangle
import javafx.scene.Node;
import javafx.scene.paint.Paint;
import javafx.scene.shape.Rectangle;
public class City extends Rectangle {
public City(final Node node) {
super(node.getLayoutX(), node.getLayoutY(), 32, 32);
this.setFill(Paint.valueOf("BLUE"));
}
}
如果你想&#34;连接&#34;从 FXML-Controller类到 FXML-File 的方法,您必须在控制器中使用@FXML
对其进行注释。< / p>
这里有一个例子给你:
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
/**
* FXML Controller class
*
* @author Patrick Ott <Patrick.Ott@professional-webworkx.de>
*/
public class MapController implements Initializable {
@FXML
AnchorPane main;
/**
* Initializes the controller class.
*
* @param url
* @param rb
*/
@Override
public void initialize(URL url, ResourceBundle rb) {
// do some initializing work here
}
@FXML
public void movetest(MouseEvent event) {
// your code for evaluating if move is possible..
System.out.println("movetest()");
}
@FXML
public void newcity(ActionEvent event) {
// your code to create a new City
}
@FXML
public void move(MouseEvent event) {
Node node = (Node) event.getSource();
City newCity = new City(node);
main.getChildren().add(newCity);
}
}
因为您的FXML是短的,并且您也没有发布完整信息,我已经使用了您添加到问题中的FXML文件,如下所示。
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.effect.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.shape.*?>
<AnchorPane fx:id="main" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="352.0" prefWidth="576.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="loadobject.MapController">
<children>
<TextField fx:id="textBox" layoutX="2.0" opacity="0.01" prefHeight="352.0" prefWidth="576.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<effect>
<ColorAdjust />
</effect>
</TextField>
<Rectangle fx:id="tile1" fill="#26b522" height="32.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile20" fill="#26b522" height="32.0" layoutX="32.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile2" fill="#26b522" height="32.0" layoutX="64.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile3" fill="#26b522" height="32.0" layoutX="96.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile4" fill="#26b522" height="32.0" layoutX="128.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile5" fill="#26b522" height="32.0" layoutX="160.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile6" fill="#26b522" height="32.0" layoutX="192.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile7" fill="#26b522" height="32.0" layoutX="224.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile8" fill="#26b522" height="32.0" layoutX="256.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile9" fill="#26b522" height="32.0" layoutX="288.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile10" fill="#26b522" height="32.0" layoutX="320.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile101" fill="#26b522" height="32.0" layoutX="320.0" layoutY="32.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile91" fill="#26b522" height="32.0" layoutX="288.0" layoutY="32.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile81" fill="#26b522" height="32.0" layoutX="256.0" layoutY="32.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile71" fill="#26b522" height="32.0" layoutX="224.0" layoutY="32.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile61" fill="#26b522" height="32.0" layoutX="192.0" layoutY="32.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile51" fill="#26b522" height="32.0" layoutX="160.0" layoutY="32.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile41" fill="#26b522" height="32.0" layoutX="128.0" layoutY="32.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile31" fill="#26b522" height="32.0" layoutX="96.0" layoutY="32.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile21" fill="#26b522" height="32.0" layoutX="64.0" layoutY="32.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile30" fill="#26b522" height="32.0" layoutX="32.0" layoutY="32.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile11" fill="#26b522" height="32.0" layoutY="32.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile102" fill="#26b522" height="32.0" layoutX="320.0" layoutY="64.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile92" fill="#26b522" height="32.0" layoutX="288.0" layoutY="64.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile82" fill="#26b522" height="32.0" layoutX="256.0" layoutY="64.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile72" fill="#26b522" height="32.0" layoutX="224.0" layoutY="64.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile62" fill="#26b522" height="32.0" layoutX="192.0" layoutY="64.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile52" fill="#26b522" height="32.0" layoutX="160.0" layoutY="64.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile42" fill="#26b522" height="32.0" layoutX="128.0" layoutY="64.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile32" fill="#26b522" height="32.0" layoutX="96.0" layoutY="64.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile22" fill="#26b522" height="32.0" layoutX="64.0" layoutY="64.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile40" fill="#26b522" height="32.0" layoutX="32.0" layoutY="64.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile12" fill="#26b522" height="32.0" layoutY="64.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile103" fill="#26b522" height="32.0" layoutX="320.0" layoutY="96.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile93" fill="#26b522" height="32.0" layoutX="288.0" layoutY="96.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile83" fill="#26b522" height="32.0" layoutX="256.0" layoutY="96.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile73" fill="#26b522" height="32.0" layoutX="224.0" layoutY="96.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile63" fill="#26b522" height="32.0" layoutX="192.0" layoutY="96.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile53" fill="#26b522" height="32.0" layoutX="160.0" layoutY="96.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile43" fill="#26b522" height="32.0" layoutX="128.0" layoutY="96.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile33" fill="#26b522" height="32.0" layoutX="96.0" layoutY="96.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile23" fill="#26b522" height="32.0" layoutX="64.0" layoutY="96.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile50" fill="#26b522" height="32.0" layoutX="32.0" layoutY="96.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile13" fill="#26b522" height="32.0" layoutY="96.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile104" fill="#26b522" height="32.0" layoutX="320.0" layoutY="128.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile94" fill="#26b522" height="32.0" layoutX="288.0" layoutY="128.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile84" fill="#26b522" height="32.0" layoutX="256.0" layoutY="128.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile74" fill="#26b522" height="32.0" layoutX="224.0" layoutY="128.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile64" fill="#26b522" height="32.0" layoutX="192.0" layoutY="128.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile54" fill="#26b522" height="32.0" layoutX="160.0" layoutY="128.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile44" fill="#26b522" height="32.0" layoutX="128.0" layoutY="128.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile34" fill="#26b522" height="32.0" layoutX="96.0" layoutY="128.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile24" fill="#26b522" height="32.0" layoutX="64.0" layoutY="128.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile60" fill="#26b522" height="32.0" layoutX="32.0" layoutY="128.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile14" fill="#26b522" height="32.0" layoutY="128.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile105" fill="#26b522" height="32.0" layoutX="320.0" layoutY="160.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile95" fill="#26b522" height="32.0" layoutX="288.0" layoutY="160.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile85" fill="#26b522" height="32.0" layoutX="256.0" layoutY="160.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile75" fill="#26b522" height="32.0" layoutX="224.0" layoutY="160.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile65" fill="#26b522" height="32.0" layoutX="192.0" layoutY="160.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile55" fill="#26b522" height="32.0" layoutX="160.0" layoutY="160.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile45" fill="#26b522" height="32.0" layoutX="128.0" layoutY="160.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile35" fill="#26b522" height="32.0" layoutX="96.0" layoutY="160.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile25" fill="#26b522" height="32.0" layoutX="64.0" layoutY="160.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile70" fill="#26b522" height="32.0" layoutX="32.0" layoutY="160.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile15" fill="#26b522" height="32.0" layoutY="160.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile106" fill="#26b522" height="32.0" layoutX="320.0" layoutY="192.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile96" fill="#26b522" height="32.0" layoutX="288.0" layoutY="192.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile86" fill="#26b522" height="32.0" layoutX="256.0" layoutY="192.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile76" fill="#26b522" height="32.0" layoutX="224.0" layoutY="192.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile66" fill="#26b522" height="32.0" layoutX="192.0" layoutY="192.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile56" fill="#26b522" height="32.0" layoutX="160.0" layoutY="192.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile46" fill="#26b522" height="32.0" layoutX="128.0" layoutY="192.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile36" fill="#26b522" height="32.0" layoutX="96.0" layoutY="192.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile26" fill="#26b522" height="32.0" layoutX="64.0" layoutY="192.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile80" fill="#26b522" height="32.0" layoutX="32.0" layoutY="192.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile16" fill="#26b522" height="32.0" layoutY="192.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile107" fill="#26b522" height="32.0" layoutX="320.0" layoutY="224.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile97" fill="#26b522" height="32.0" layoutX="288.0" layoutY="224.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile87" fill="#26b522" height="32.0" layoutX="256.0" layoutY="224.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile77" fill="#26b522" height="32.0" layoutX="224.0" layoutY="224.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile67" fill="#26b522" height="32.0" layoutX="192.0" layoutY="224.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile57" fill="#26b522" height="32.0" layoutX="160.0" layoutY="224.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile47" fill="#26b522" height="32.0" layoutX="128.0" layoutY="224.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile37" fill="#26b522" height="32.0" layoutX="96.0" layoutY="224.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile27" fill="#26b522" height="32.0" layoutX="64.0" layoutY="224.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile90" fill="#26b522" height="32.0" layoutX="32.0" layoutY="224.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile17" fill="#26b522" height="32.0" layoutY="224.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile108" fill="#26b522" height="32.0" layoutX="320.0" layoutY="256.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile98" fill="#26b522" height="32.0" layoutX="288.0" layoutY="256.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile88" fill="#26b522" height="32.0" layoutX="256.0" layoutY="256.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile78" fill="#26b522" height="32.0" layoutX="224.0" layoutY="256.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile68" fill="#26b522" height="32.0" layoutX="192.0" layoutY="256.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile58" fill="#26b522" height="32.0" layoutX="160.0" layoutY="256.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile48" fill="#26b522" height="32.0" layoutX="128.0" layoutY="256.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile38" fill="#26b522" height="32.0" layoutX="96.0" layoutY="256.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile28" fill="#26b522" height="32.0" layoutX="64.0" layoutY="256.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile110" fill="#26b522" height="32.0" layoutX="32.0" layoutY="256.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile18" fill="#26b522" height="32.0" layoutY="256.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile109" fill="#26b522" height="32.0" layoutX="320.0" layoutY="288.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile99" fill="#26b522" height="32.0" layoutX="288.0" layoutY="288.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile89" fill="#26b522" height="32.0" layoutX="256.0" layoutY="288.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile79" fill="#26b522" height="32.0" layoutX="224.0" layoutY="288.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile69" fill="#26b522" height="32.0" layoutX="192.0" layoutY="288.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile59" fill="#26b522" height="32.0" layoutX="160.0" layoutY="288.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile49" fill="#26b522" height="32.0" layoutX="128.0" layoutY="288.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile39" fill="#26b522" height="32.0" layoutX="96.0" layoutY="288.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile29" fill="#26b522" height="32.0" layoutX="64.0" layoutY="288.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile120" fill="#26b522" height="32.0" layoutX="32.0" layoutY="288.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="tile19" fill="#26b522" height="32.0" layoutY="288.0" onMouseClicked="#movetest" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Rectangle fx:id="r" arcHeight="15.0" arcWidth="15.0" fill="#ff1f1f" height="32.0" layoutX="256.0" layoutY="160.0" onMouseClicked="#move" stroke="BLACK" strokeType="INSIDE" width="32.0" />
<Label layoutX="495.0" layoutY="6.0" prefHeight="17.0" prefWidth="60.0" text="Moves left:" />
<Label fx:id="movelabel" layoutX="553.0" layoutY="6.0" prefHeight="17.0" prefWidth="21.0" />
<Button layoutX="4.0" layoutY="6.0" mnemonicParsing="false" onAction="#newcity" text="Found City" />
</children>
</AnchorPane>
我希望它会对你有所帮助。如果事情不明确,请随意评论并询问;)
帕特里克
答案 1 :(得分:0)
JavaFX在幕后摆弄对象。我不知道你的确切错误,但我可以假设它是一个与变量selectnode相关的空指针异常。如果将此变量定义为public static,则可以静态引用它而不是传递它。这也应解决它未定义的问题。