我试图做一个游戏,我已经在java类和fxml文件中的UI的另一部分中制作了一个单元格网格,但是当我运行它时,ide给出了:
JavaFX应用程序线程” java.lang.RuntimeException:java.lang.reflect.InvocationTargetException并向构造函数plz指出一次包含单元格的数组
这里是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)
im试图做一个游戏,我在java类中创建了一个单元格网格,在fxml文件中创建了UI的另一部分,但是当我运行它时,ide提供了:JavaFX Application Thread“ java.lang.RuntimeException:...
是,James_D明白了。我测试了您的代码,并进行了一些调整。看:
Main.java