我尝试创建自己的自定义节点,但我以某种方式失败了。 当我尝试运行控制器/场景时,它总是抛出一个错误。 这是我的代表自定义节点的类
package com.lollookup.scene.customcontrol;
import com.lollookup.scene.data.ChampionInfoData;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Pane;
import javax.xml.soap.Text;
import java.io.IOException;
/**
* @author Yasin
*/
public class ChampionInfo extends Pane {
@FXML
private ImageView championImage;
@FXML
private Text KDA;
@FXML
private Text winRate;
@FXML
private Text masteryScore;
@FXML
private Text masteryLevel;
public ChampionInfo() {
try {
Parent root = FXMLLoader.load(getClass().getResource("championinfo.fxml"));
} catch (IOException e) {
e.printStackTrace();
}
}
public void setData(ChampionInfoData championInfoData) {
this.championImage.setImage(new Image(championInfoData.getUrl()));
this.KDA.setTextContent(championInfoData.getKDA());
this.winRate.setTextContent(championInfoData.getWinRate());
this.masteryScore.setTextContent(championInfoData.getMasteryScore());
this.masteryLevel.setTextContent(championInfoData.getMasteryLevel());
}
}
这是我的fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Separator?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.Text?>
<fx:root maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="84.0" prefWidth="238.0" type="Pane" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.lollookup.scene.customcontrol.ChampionInfo">
<children>
<Separator layoutY="3.0" prefHeight="0.0" prefWidth="238.0" />
<HBox prefHeight="84.0" prefWidth="200.0">
<children>
<ImageView fx:id="championImage" fitHeight="72.0" fitWidth="66.0" pickOnBounds="true" preserveRatio="true">
<HBox.margin>
<Insets left="10.0" top="10.0" />
</HBox.margin>
</ImageView>
<VBox prefHeight="200.0" prefWidth="100.0">
<children>
<Text fx:id="KDA" strokeType="OUTSIDE" strokeWidth="0.0" text="kda">
<VBox.margin>
<Insets top="10.0" />
</VBox.margin>
</Text>
<Text fx:id="winRate" strokeType="OUTSIDE" strokeWidth="0.0" text="winRate" />
<Text fx:id="masteryLevel" strokeType="OUTSIDE" strokeWidth="0.0" text="champLevel" />
<Text fx:id="masteryScore" strokeType="OUTSIDE" strokeWidth="0.0" text="champScore" />
</children>
<HBox.margin>
<Insets left="10.0" />
</HBox.margin>
</VBox>
</children>
</HBox>
<Separator layoutY="79.0" prefHeight="0.0" prefWidth="238.0" />
</children>
</fx:root>
我现在的问题是,我无法真正启动/实现自定义节点。 所以不管我是否创建了这样的场景:
package com.lollookup.scene.customcontrol;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
/**
* @author Yasin
*/
public class ChampionInfoExample extends Application {
@Override
public void start(Stage stage) throws Exception {
//ChampionInfo championInfo = new ChampionInfo(new ChampionInfoData("https://placeholdit.imgix.net/~text?txtsize=33&txt=350%C3%97150&w=350&h=150", "1:1", "50%", "0", "1"));
ChampionInfo championInfo = new ChampionInfo();
//championInfo.setData(new ChampionInfoData("https://placeholdit.imgix.net/~text?txtsize=33&txt=350%C3%97150&w=350&h=150", "1:1", "50%", "0", "1"));
stage.setScene(new Scene(championInfo));
stage.setTitle("Custom Control");
stage.setWidth(300);
stage.setHeight(200);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
或像这样实例化: Stream.of(championData).forEach(p - &gt; championDataContainer.getChildren()。add(new ChampionInfo()));
这就是抛出的东西:
Exception in Application start method
Exception in thread "main" java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.StackOverflowError
at java.net.URLStreamHandler.setURL(URLStreamHandler.java:537)
at java.net.URLStreamHandler.parseURL(URLStreamHandler.java:304)
at sun.net.www.protocol.file.Handler.parseURL(Handler.java:67)
at java.net.URL.<init>(URL.java:615)
at java.net.URL.<init>(URL.java:483)
at sun.misc.URLClassPath$FileLoader.getResource(URLClassPath.java:1222)
at sun.misc.URLClassPath$FileLoader.findResource(URLClassPath.java:1212)
at sun.misc.URLClassPath$1.next(URLClassPath.java:240)
at sun.misc.URLClassPath$1.hasMoreElements(URLClassPath.java:250)
at java.net.URLClassLoader$3$1.run(URLClassLoader.java:601)
at java.net.URLClassLoader$3$1.run(URLClassLoader.java:599)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader$3.next(URLClassLoader.java:598)
at java.net.URLClassLoader$3.hasMoreElements(URLClassLoader.java:623)
at sun.misc.CompoundEnumeration.next(CompoundEnumeration.java:45)
at sun.misc.CompoundEnumeration.hasMoreElements(CompoundEnumeration.java:54)
at java.util.ServiceLoader$LazyIterator.hasNextService(ServiceLoader.java:354)
at java.util.ServiceLoader$LazyIterator.hasNext(ServiceLoader.java:393)
at java.util.ServiceLoader$1.hasNext(ServiceLoader.java:474)
at javax.xml.stream.FactoryFinder$1.run(FactoryFinder.java:352)
at java.security.AccessController.doPrivileged(Native Method)
at javax.xml.stream.FactoryFinder.findServiceProvider(FactoryFinder.java:341)
at javax.xml.stream.FactoryFinder.find(FactoryFinder.java:313)
at javax.xml.stream.FactoryFinder.find(FactoryFinder.java:227)
at javax.xml.stream.XMLInputFactory.newInstance(XMLInputFactory.java:154)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2472)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
at com.lollookup.scene.customcontrol.ChampionInfo.<init>(ChampionInfo.java:38)
at sun.reflect.GeneratedConstructorAccessor2.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
我该怎么办? 我很感激任何帮助 感谢
编辑: 调试后,我发现了为什么它是null。与给出的代码没有任何关系。谢谢StackOverflow!