- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
对于一个项目,我得到了代码并且必须将其重构为 FXML,我正在尝试使用我得到的一段代码,它是 AnchorPane 的扩展,但是当我在 FXML 中将其从 AnchorPane 切换到 World 时,我在场景生成器和运行它时出现错误。我已经尝试了很多东西,但有点困惑,现在不知道该尝试什么。我注释掉了 World 的原始构造函数,但如果可能相关,可以将其发布
世界:
package game;
import javafx.animation.AnimationTimer;
import javafx.scene.Node;
import javafx.scene.layout.AnchorPane;
import java.util.ArrayList;
import java.util.List;
public abstract class World extends AnchorPane {
private AnimationTimer timer;
public void createTimer() {
timer = new AnimationTimer() {
@Override
public void handle(long now) {
act(now);
List<Actor> actors = getObjects(Actor.class);
for (Actor anActor: actors) {
anActor.act(now);
}
}
};
}
public void start() {
createTimer();
timer.start();
}
public void stop() {
timer.stop();
}
public void add(Actor actor) {
getChildren().add(actor);
}
public void remove(Actor actor) {
getChildren().remove(actor);
}
public <A extends Actor> List<A> getObjects(Class<A> cls) {
ArrayList<A> someArray = new ArrayList<A>();
for (Node n: getChildren()) {
if (cls.isInstance(n)) {
someArray.add((A)n);
}
}
return someArray;
}
public abstract void act(long now);
}
FXML
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.image.*?>
<?import javafx.scene.layout.*?>
<?import game.End?>
<?import game.Log?>
<?import game.WetTurtle?>
<?import game.Turtle?>
<?import game.Obstacle?>
<?import game.World?>
<World maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity"
prefHeight="800.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/10.0.2-internal"
xmlns:fx="http://javafx.com/fxml/1" fx:controller="game.Level1Controller">
<ImageView>
<image>
<Image url="@../backgrounds/frogbg.png" />
</image>
</ImageView>
<End layoutX="13.0" layoutY="138.8">
<image>
<Image url="@../end/End.png" />
</image></End>
<End layoutX="141.0" layoutY="138.0">
<image>
<Image url="@../end/End.png" />
</image>
</End>
<End layoutX="269.0" layoutY="138.0">
<image>
<Image url="@../end/End.png" />
</image>
</End>
<End layoutX="398.0" layoutY="138.0">
<image>
<Image url="@../end/End.png" />
</image>
</End>
<End layoutX="528.0" layoutY="138.0">
<image>
<Image url="@../end/End.png" />
</image>
</End>
<Log fitHeight="30.0" layoutY="209.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../log/log3.png" />
</image>
</Log>
<Log fitHeight="30.0" layoutX="220.0" layoutY="209.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../log/log3.png" />
</image>
</Log>
<Log fitHeight="30.0" layoutX="440.0" layoutY="209.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../log/log3.png" />
</image>
</Log>
<WetTurtle fitHeight="45.0" layoutX="600.0" layoutY="252.0" preserveRatio="true">
<image>
<Image url="@../turtle/TurtleAnimation1.png" />
</image>
</WetTurtle>
<WetTurtle fitHeight="45.0" layoutX="400.0" layoutY="252.0" preserveRatio="true">
<image>
<Image url="@../turtle/TurtleAnimation1.png" />
</image>
</WetTurtle>
<WetTurtle fitHeight="45.0" layoutX="200.0" layoutY="252.0" preserveRatio="true">
<image>
<Image url="@../turtle/TurtleAnimation1.png" />
</image>
</WetTurtle>
<Log fitHeight="35.0" layoutY="308.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../log/logs.png" />
</image>
</Log>
<Log fitHeight="35.0" layoutX="440.0" layoutY="308.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../log/logs.png" />
</image>
</Log>
<Log fitHeight="35.0" layoutX="50.0" layoutY="358.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../log/log3.png" />
</image>
</Log>
<Log fitHeight="35.0" layoutX="270.0" layoutY="358.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../log/log3.png" />
</image>
</Log>
<Log fitHeight="35.0" layoutX="490.0" layoutY="358.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../log/log3.png" />
</image>
</Log>
<Turtle fitHeight="45.0" layoutX="500.0" layoutY="402.0" preserveRatio="true">
<image>
<Image url="@../turtle/TurtleAnimation1.png" />
</image></Turtle>
<Turtle fitHeight="45.0" layoutX="300.0" layoutY="402.0" preserveRatio="true">
<image>
<Image url="@../turtle/TurtleAnimation1.png" />
</image>
</Turtle>
<WetTurtle fitHeight="45.0" layoutX="700.0" layoutY="402.0" preserveRatio="true">
<image>
<Image url="@../turtle/TurtleAnimation1.png" />
</image>
</WetTurtle>
<Obstacle fitHeight="28.0" layoutX="500.0" layoutY="511.0" preserveRatio="true">
<image>
<Image url="@../car/car1Left.png" />
</image>
</Obstacle>
<Obstacle fitHeight="35.0" layoutX="500.0" layoutY="557.0" preserveRatio="true">
<image>
<Image url="@../truck/truck2Right.png" />
</image>
</Obstacle>
<Obstacle fitHeight="35.0" layoutY="557.0" preserveRatio="true">
<image>
<Image url="@../truck/truck2Right.png" />
</image>
</Obstacle>
<Obstacle fitHeight="28.0" layoutX="550.0" layoutY="611.0" preserveRatio="true">
<image>
<Image url="@../car/car1Left.png" />
</image>
</Obstacle>
<Obstacle fitHeight="28.0" layoutX="400.0" layoutY="611.0" preserveRatio="true">
<image>
<Image url="@../car/car1Left.png" />
</image>
</Obstacle>
<Obstacle fitHeight="28.0" layoutX="250.0" layoutY="611.0" preserveRatio="true">
<image>
<Image url="@../car/car1Left.png" />
</image>
</Obstacle>
<Obstacle fitHeight="28.0" layoutX="100.0" layoutY="611.0" preserveRatio="true">
<image>
<Image url="@../car/car1Left.png" />
</image>
</Obstacle>
<Obstacle fitHeight="35.0" layoutX="600.0" layoutY="657.0" preserveRatio="true">
<image>
<Image url="@../truck/truck1Right.png" />
</image>
</Obstacle>
<Obstacle fitHeight="35.0" layoutX="300.0" layoutY="657.0" preserveRatio="true">
<image>
<Image url="@../truck/truck1Right.png" />
</image>
</Obstacle>
<Obstacle fitHeight="35.0" layoutY="657.0" preserveRatio="true">
<image>
<Image url="@../truck/truck1Right.png" />
</image>
</Obstacle>
</World>
错误
Exception in Application start method
java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:473)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:372)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:941)
Caused by: java.lang.RuntimeException: Exception in Application start method
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:973)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:198)
at java.base/java.lang.Thread.run(Thread.java:844)
Caused by: javafx.fxml.LoadException:
/C:/Users/harry/IdeaProjects/Frogger%20Game/out/production/Frogger%20Game/game/fxml/level1.fxml:16
at javafx.fxml/javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2625)
at javafx.fxml/javafx.fxml.FXMLLoader.access$700(FXMLLoader.java:105)
at javafx.fxml/javafx.fxml.FXMLLoader$InstanceDeclarationElement.constructValue(FXMLLoader.java:1021)
at javafx.fxml/javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:754)
at javafx.fxml/javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2722)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2552)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2466)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3253)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3210)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3179)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3152)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3129)
at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:3122)
at Frogger.Game/game.Main.start(Main.java:25)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:919)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$11(PlatformImpl.java:449)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$9(PlatformImpl.java:418)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:417)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:175)
... 1 more
Caused by: java.lang.InstantiationException
at java.base/jdk.internal.reflect.InstantiationExceptionConstructorAccessorImpl.newInstance(InstantiationExceptionConstructorAccessorImpl.java:48)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:488)
at java.base/java.lang.Class.newInstance(Class.java:560)
at javafx.fxml/javafx.fxml.FXMLLoader$InstanceDeclarationElement.constructValue(FXMLLoader.java:1019)
... 20 more
Exception running application game.Main
Process finished with exit code 1
最佳答案
World
是抽象的,您无法构造抽象类的实例。
当你声明一个元素时:
<World ...>
在 FXML 中,当您加载 FXML 文档时,FXMLLoader 将尝试创建 World
类的实例。当它这样做时,它会失败,因为 World
是抽象的并且无法实例化。
您的代码或设计中可能还存在其他问题,但至少存在此处指出的问题。
if i want to make an instance of it but it extend AnchorPane how do i go about that?
你不能,你的类层次结构已经由你的 extends 子句定义了,除非你改变层次结构设计(例如重新编码 World 所以它不是抽象的),你必须实例化 World 的具体子类。
I have another class that extends world that isn't abstract so i should use that instead i guess?
是的。
关于java - 尝试使用 AnchorPane 的扩展版本时出现实例化和加载异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59402011/
我正在编写一个 JavaFX 应用程序,顶部有一个菜单栏,下面有一个 AnchorPane,我可以在其中使用其他 FXML 文件来显示内容。问题是当代码运行时,即使我将 Vgrow 设置为“始终”,其
正如标题所示,我创建了一个根 AnchorPane,其中放置了两个 AnchorPane,上部 AnchorPane 以某种方式变得圆滑,但在下部 AnchorPane 的情况下,上侧是圆角的,而下侧
所以我正在致力于构建一个类似于 IDE 的应用程序,例如 Eclipse 或 Netbeans。我想要有不同的区域,用户可以选择一些内容,例如将显示在其中一个区域中的 View 。我的第一次尝试是将控
我想创建一个基于GridPane的应用程序。为了实现自适应性,我使用 AnchorPane。布局在宽度上延伸得很好,但在高度上却很糟糕。如何修复?
我有一个 TabPane,其中包含两个 Tab,并在 FXML 中设置。初始化时,我动态地将 AnchorPane 添加到 Tab 之一。然后,我将 ListView 设置为 AnchorPane 的
所以我对这个有点卡住了。我有一个非常基本的游戏,你可以用箭头键在网格中移动一艘船。 我添加了另一个线程,其中包含一些应该自动在网格中漫游的怪物。我可以从打印语句中看到线程正在运行并且怪物正在移动,但是
我初始化了一些不是我的锚定 Pane 的元素: AnchorPane root = new AnchorPane(listLoader.load(), textareaLoader.load(), f
对于一个项目,我得到了代码并且必须将其重构为 FXML,我正在尝试使用我得到的一段代码,它是 AnchorPane 的扩展,但是当我在 FXML 中将其从 AnchorPane 切换到 World 时
我正在尝试使鼠标事件在我的 javaFx 中工作。我希望能够向我的 anchor 面板添加鼠标事件,但我似乎无法使其工作。 room = new AnchorPane(); r
您好,我有以下代码,我在 anchor Pane 内添加一个圆圈,并在圆圈上添加文本。 Circle是一个Circle,ArrayList,有多个圆圈。 pane.getChildren().
我有一列有 3 行。每行包含一个图像。此列是 GridPane 的子项。如您在以下 FXML 中所见:
我在 AnchorPane 内有一个 JavaFX Label,它包含在 VBox 中。这个位于 ScrollPane 中。我希望文本自动换行并自动适应窗口的宽度。 这是我的代码的简化示例: publ
这似乎是一个简单的问题,但我还没有找到相关的答案。 我正在创建一个简单的 Javafx 应用程序,并在单击时创建可以拖动的节点。 我想确保特定类型的节点(圆形)始终绘制在 AnchorPane 顶部。
我正在做一个项目,我需要你的帮助。 我想知道是否可以将 percentage 中的锚定 Pane 约束设置为类似 AnchorPane.setLeftAnchor(content1, 35%); 最佳
我正在使用 SceneBuilder 8.0.0 和 JavaFX 8。 我有一个 Button btn 和一个 Label lbl 附加到 AnchorPane ap。 当应用程序启动时,btn 和
我有 AnchorPane根 根目录下的HBox HBox中有两个VBox,每个都没有名字,new AnchorPane和带有文本的每个 AnchorPane 中的无名标签 有表格,只是模板不适合我。
我正在使用 sceneBuilder。 How to clear an AnchorPane ? Is there a way to delete all the nodes contained in
我希望我的 Javafx 组件(如标签、TextFields 分别包装在 VBox 中)保持在窗口的中央,即使它已调整大小。见附件图片。目前这些组件包含在 anchorPane 中。FXML 文件代码
我正在 javafx 中创建一个小应用程序。我想设置背景颜色,所以以下是我的 FXML 代码..我需要如何设置背景颜色
我会将我的锚定 Pane 打印为 png:我在我的 Controller 中加载了一个图像并写了一些文本,而不是我想保存整个面板,但我不知道我必须使用哪些工具。 URL fxmlpath = this
我是一名优秀的程序员,十分优秀!