我试图做一个游戏,我在java类中创建了一个单元格网格,并在fxml文件中创建了UI的另一部分,但是当我运行它时,ide提供了:
JavaFX应用程序线程” java.lang.RuntimeException:java.lang.reflect.invocationTargetException 并向构造函数plz help指出包含单元格的数组一次
这是usergrid类,包含单元格类鼠标手势和实际网格 请注意,由于我正在尝试解决此问题,因此鼠标手势类尚未完成
package sample;
import javafx.beans.value.changelistener;
import javafx.beans.value.ObservableValue;
import javafx.fxml.FXML;
import javafx.scene.Node;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import java.lang.reflect.invocationTargetException;
public class userGrid extends Pane {
static int row;
static int column;
static int height;
static int width;
static boolean star;
static boolean wall;
@FXML
StackPane gridRoot;
gridCell [][] boardCells;
userGrid actualGrid = new userGrid();
public userGrid() {
try {
actualGrid.row = DataModel.row;
actualGrid.column = DataModel.column;
actualGrid.star = false;
actualGrid.wall = false;
actualGrid.width = DataModel.gridWidth;
actualGrid.height = DataModel.gridHeight;
actualGrid.boardCells = new gridCell[row][column];
MouseGesture M = new MouseGesture();
for (int y = 0; y < actualGrid.row; y++) {
for (int x = 0; x < actualGrid.column; x++) {
gridCell cell = new gridCell(y,x,false,false);
M.paint(cell);
actualGrid.add(cell,row,column);
}
}
gridRoot.getchildren().add(actualGrid);
}catch (Exception e){
e.getcause();
System.out.println("check");
e.printStackTrace();
}
}
public void add(gridCell cell,int row,int column){
int cellWidth = userGrid.width/userGrid.column;
int cellHeight = userGrid.height/userGrid.row;
int x = cellWidth*column;
int y= cellHeight*row;
boardCells[x][y] = cell;
cell.setPrefSize(cellWidth,cellHeight);
cell.setTranslateX(x);
cell.setTranslateY(y);
getchildren().add(cell);
}
public gridCell getcell(int x,int y){ return boardCells[x][y]; }
public class gridCell extends StackPane{
int row;
int column;
boolean star;
boolean wall;
public gridCell(int row,int column,boolean star,boolean wall){
this.star=star;
this.wall=wall;
this.row=row;
this.column=column;
getStyleclass().add("cell");
}
public void makeStar(){
getStyleclass().remove("cell-highlight");
getStyleclass().add("cell-star-highlight");
}
public void makeSmileyFace(int n){
if(n==1){
getStyleclass().remove("cell-highlight");
getStyleclass().add("cell-smiley1-highlight");
}else if(n==2){
getStyleclass().remove("cell-smile2-highlight");
getStyleclass().add("cell-smiley2-highlight");
}
}
public void makeWall(){
getStyleclass().remove("cell-wall-highlight");
getStyleclass().add("cell-wall-highlight");
}
public void cellHover(){
getStyleclass().remove("cell-hover-highlight");
getStyleclass().add("cell-hover-highlight");
}
public void RemoveStar(){
getStyleclass().remove("cell-highlight");
getStyleclass().add("cell-star-highlight");
}
public void RemoveSmileyFace(int n){
if(n==1){
getStyleclass().remove("cell-highlight");
}else if(n==2){
getStyleclass().remove("cell-smile2-highlight");
}
}
public void RemoveWall(){
getStyleclass().remove("cell-wall-highlight");
}
public void UnhighlightHover(){
getStyleclass().remove("cell-hover-highlight");
}
public String toString() {
return this.column + "C||R" + this.row;
}
}
public class MouseGesture{
public void paint(Node node){
if(true){
node.hoverProperty().addListener(new changelistener<Boolean>(){
@Override
public void changed(ObservableValue<? extends Boolean> observable,Boolean oldValue,Boolean newValue){
System.out.println( observable + ": " + newValue);
if( newValue) {
((gridCell) node).cellHover();
} else {
((gridCell) node).UnhighlightHover();
}
for( String s: node.getStyleclass())
System.out.println( node + ": " + s);
}
});
// node.setOnmousepressed( onmousepressedEventHandler);
// node.setOnDragDetected( onDragDetectedEventHandler);
// node.setOnmouseDragEntered(onmouseDragEnteredEventHandler);
}
}
}
}
这是fxml文件 我做了一些按钮,然后尝试使用fx:grid root的id将实际的网格添加到堆栈窗格中
<?xml version="1.0" encoding="UTF-8"?>
<!--<?import sample.userGrid?>-->
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.StackPane?>
<GridPane prefHeight="400.0" prefWidth="600.0" stylesheets="@userGrid.css"
xmlns="http://javafx.com/javafx/8.0.172-ea" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="sample.userGrid">
<VBox>
<HBox alignment="TOP_LEFT">
<Button fx:id="starButton" id="starButton-highlight" text="Star" ></Button>
<Button fx:id="wallButton" id="wallButton-highlight" text="Wall" ></Button>
<Button fx:id="smiley1Button" id="smiley1Button-highlight" text="smiley1" ></Button>
<Button fx:id="smiley2Button" id="smiley2Button-highlight" text="smiley2" ></Button>
</HBox>
<StackPane fx:id="gridRoot">
<children>
</children>
</StackPane>
</VBox>
</GridPane>
这是我尝试调用网格便笺的方式,在尝试确定问题原因时会遇到一些尝试问题
package sample;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
import java.io.IOException;
public class initializePageController {
@FXML
TextField GridWidth;
@FXML
TextField GridHeight;
@FXML
TextField row;
@FXML
TextField column;
@FXML
TextField name1;
@FXML
TextField name2;
Parent root;
Stage stage;
public void pageDimensions() throws IOException {
int gHeight =Integer.parseInt(GridHeight.getText());
int gWidth = Integer.parseInt(GridHeight.getText());
int rrow = Integer.parseInt(row.getText());
int ccolumn = Integer.parseInt(column.getText());
DataModel.gridHeight=gHeight;
DataModel.gridWidth=gWidth;
DataModel.row=rrow;
DataModel.column=ccolumn;
try {
System.out.println("Dimension Happened");
//System.out.println( gHeight);
System.out.println("AIMING FOR GRID");
try {
root = FXMLLoader.load(getclass().getResource("/sample/GridInitializer.fxml"));
stage = (Stage) row.getScene().getWindow();
Scene scene = new Scene(root,DataModel.gridWidth,DataModel.gridHeight);
stage.setScene(scene);
stage.setTitle("please god ");
stage.show();
} catch (Exception e) {
System.out.println("grid initiakizer went wring /init.page controller");
e.printStackTrace();
}
}catch (Exception ex){
System.out.println("101");
ex.getcause();
System.out.println("101");
}
}
}
这是用于包含数据的数据模型类
package sample;
public class DataModel {
public static int gridHeight;
public static int gridWidth;
public static String name1;
public static String name2;
public static int row;
public static int column;
}
和用于设计代码的css文件
.cell{
-fx-background-color: rgb(38,38,38);
-fx-border-color: red;
-fx-border-width: 1px;
}
.cell-highlight {
-fx-background-color:derive(blue,0.9);
}
.cell-hover-highlight {
-fx-background-color:derive(green,0.9);
}
.cell-star-highlight{
-fx-background-color: derive(yellow,1%);
-fx-shape: "M 100 0 L175 200 L0 75 L200 75 L25 200 Z";
}
.cell-smily1-highlight{
-fx-shape: "M2 1 h1 v1 h1 v1 h-1 v1 h-1 v-1 h-1 v-1 h1 z";
-fx-background-color: derive(red,10%);
}
.cell-smily2-highlight{
-fx-background-color: derive(blue,10%);
-fx-shape: "M2 1 h1 v1 h1 v1 h-1 v1 h-1 v-1 h-1 v-1 h1 z";
}
.cell-wall-highlight{
}
这是完整的堆栈跟踪信息,前三行只是一个检查`
action happend
Dimension Happened
AIMING FOR GRID
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.invocationTargetException
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$ClickGenerator.postProcess(Scene.java:3470)
at javafx.scene.Scene$ClickGenerator.access$8100(Scene.java:3398)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3766)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:394)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
at java.security.accessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:432)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:410)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:431)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$4(WinApplication.java:186)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.reflect.invocationTargetException
at sun.reflect.NativeMethodaccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodaccessorImpl.invoke(NativeMethodaccessorImpl.java:62)
at sun.reflect.DelegatingMethodaccessorImpl.invoke(DelegatingMethodaccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
at sun.reflect.GeneratedMethodaccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodaccessorImpl.invoke(DelegatingMethodaccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1771)
... 31 more
Caused by: java.lang.StackOverflowError
at javafx.scene.Node.getScene(Node.java:932)
at javafx.scene.Node.updateCanReceiveFocus(Node.java:8099)
at javafx.scene.Node.setTreeVisible(Node.java:8007)
at javafx.scene.Node.updateTreeVisible(Node.java:7998)
at javafx.scene.Node.<init>(Node.java:2349)
at javafx.scene.Parent.<init>(Parent.java:1295)
at javafx.scene.layout.Region.<init>(Region.java:457)
at javafx.scene.layout.Pane.<init>(Pane.java:124)
at sample.userGrid.<init>(userGrid.java:26)
at sample.userGrid.<init>(userGrid.java:25)
at sample.userGrid.<init>(userGrid.java:25)
at sample.userGrid.<init>(userGrid.java:25)