Javafx不显示嵌套组件 [英] Javafx doesn't display a nested component
问题描述
要继续此帖子,我实施了建议的解决方案的一部分.
To continue this post, I implemented part of the solution that was suggested.
我有我的主面板,我想在其上画一个不同类的组件(方向盘).
I have my main panel and I want to drew on it a component which is a different class (steerwheel).
我的主控制器:
public class WindowController {
@FXML SteerWheel steerwheel;
... other componenets..
}
我的新组件:
public SteeringWheel() {
myLabel = new Label();
innerCircle = new Circle();
backgroundCircle = new Circle();
System.out.println("steerwheel created.");
}
.. other methods..
我的mainwindow.fxml文件:
My mainwindow.fxml file :
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.*?>
<?import view.SteerWheel?>
<BorderPane xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/9.0.1" fx:controller="view.WindowController">
<center>
<SteerWheel fx:id="steerwheel" />
</center>
</BorderPane>
和我的steerwheel.fxml文件:
and my steerwheel.fxml file :
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Slider?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.StackPane?>
<?import javafx.scene.shape.Circle?>
<?import javafx.scene.text.Font?>
<AnchorPane xmlns="http://javafx.com/javafx"
xmlns:fx="http://javafx.com/fxml"
fx:controller="view.SteerWheel"
prefHeight="400.0" prefWidth="600.0">
<Label fx:id="mylabel" prefHeight="30.0" prefWidth="102.0" text="mytest" translateY="30" translateX="90">
<StackPane >
<Circle fx:id="innerCircle" fill="darkgray" radius="170" />
<Circle fx:id="backgroundCircle " fill="black" radius="80" />
</StackPane>
</AnchorPane>
我的主要代码加载了fxml文件(与所有提到的文件都位于不同的文件中):
My main code that loads the fxml files (in a different file from all mentioned) :
public class Main extends Application {
public static Stage primaryStage;
@Override
public void start(Stage primary_stage) {
this.primaryStage=primary_stage;
FXMLLoader fxl=new FXMLLoader();
try {
BorderPane root = fxl.load(getClass().getResource("mainwindow.fxml").openStream());
WindowController wc=fxl.getController();
Scene scene = new Scene(root,700,700);
primaryStage.setScene(scene);
primaryStage.show();
} catch (IOException e) {
e.printStackTrace();
}
我看到了构造函数的输出,但是输出到了控制台,但是该组件没有显示在窗口上.
I'm seeing the constructor`s output but to the console but the component isn't displayed on the window.
我尝试在Window.fxml中添加对方向盘的fxml文件的调用:
I tried to add a call for the fxml file of the steerwheel in my Window.fxml :
<BorderPane xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/9.0.1" fx:controller="view.WindowController">
<center>
<VBox maxHeight="-Infinity" prefHeight="450.0" prefWidth="400.0" BorderPane.alignment="TOP_CENTER">
<children>
<fx:include source="steerwheel.fxml" fx:id="steerwheel" />
</children>
</VBox>
</center>
</BorderPane>
现在我遇到以下错误:
原因:java.lang.IllegalArgumentException:无法将view.steerWheel字段view.WindowController.steerWheel设置为javafx.scene.layout.AnchorPane
Caused by: java.lang.IllegalArgumentException: Can not set view.steerWheel field view.WindowController.steerWheel to javafx.scene.layout.AnchorPane
推荐答案
我发现以下帖子描述了我遇到的相同问题: 将数据从一个控制器传递到另一个javafx. java.lang.IllegalArgumentException @fabian还回答了该问题的解决方法.
I found the following post that described the same issue I had : Passing data from one controller to another in javafx. java.lang.IllegalArgumentException @fabian also answered there how to solve the issue.
我实施的解决方案: 我的主要fxml文件:
The solution I implemented : My main fxml file :
<BorderPane xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/9.0.1" fx:controller="view.WindowController">
<center>
<VBox maxHeight="-Infinity" prefHeight="450.0" prefWidth="400.0" BorderPane.alignment="TOP_CENTER">
<children>
<fx:include fx:id="steerwheel" source="steerwheel.fxml" />
</children>
</VBox>
</center>
</BorderPane>
在fxml文件的控件中,我添加了一个以fx:id命名的AnchorPane对象,并将steerWheel obj重命名为steerwheelController:
In the controler of the fxml file I added an AnchorPane object that named after the fx:id and I renamed the steerWheel obj to steerwheelController :
public class WindowController {
@FXML AnchorPane steerwheel;
@FXML steerWheel steerwheelController;
然后,我必须在主目录中使用setLocation方法才能加载内部fxml文件(否则,我会得到一个错误..):
Afterwards, I had to use the setLocation method in my main in order to load the inner fxml file (otherwise I got an error..) :
FXMLLoader fxl=new FXMLLoader();
try {
fxl.setLocation(getClass().getResource("Window.fxml"));
BorderPane root = fxl.load();
WindowController wc=fxl.getController();
希望它对某人有帮助:)
Hoping it will help someone :)
这篇关于Javafx不显示嵌套组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!