Javafx 控制器中的参考标签

Javafx 控制器中的参考标签,javafx,javafx-2,javafx-8,Javafx,Javafx 2,Javafx 8,我试图在javafx中使用fxml创建一个简单的ui。我正在尝试引用我在控制器类的.fxml文件中创建的标签。但是由于某种原因,当我查看fxml标记时,我会看到一个错误,其中fx:id是给定的。它说,无法将javafx.scene.control.Label设置为字段“leaders”。但是它对图像视图很有效。代码如下: customcontrol.fxml <?xml version="1.0" encoding="UTF-8"?> <?import javafx.scene

我试图在javafx中使用fxml创建一个简单的ui。我正在尝试引用我在控制器类的
.fxml
文件中创建的标签。但是由于某种原因,当我查看fxml标记时,我会看到一个错误,其中fx:id是给定的。它说,
无法将javafx.scene.control.Label设置为字段“leaders”
。但是它对
图像视图
很有效。代码如下:

customcontrol.fxml

<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.canvas.*?>
<?import javafx.scene.text.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.image.*?>
<?import java.lang.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.geometry.Insets?>

<fx:root prefHeight="800.0" prefWidth="1005.0" type="javafx.scene.layout.VBox" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
    <children>

        <BorderPane prefHeight="800.0" prefWidth="1005.0">
            <top>
                <MenuBar BorderPane.alignment="CENTER">
                    <menus>
                        <Menu mnemonicParsing="false" text="About">
                            <items>
                                <MenuItem mnemonicParsing="false" text="Close" />
                            </items>
                        </Menu>
                    </menus>
                </MenuBar>
            </top>
            <left>
                <ListView fx:id="mylist" minHeight="765.0" minWidth="250.0" prefHeight="771.0" prefWidth="250.0" BorderPane.alignment="CENTER_LEFT" />
            </left>
            <center>
                <ScrollPane hbarPolicy="NEVER" prefViewportHeight="771.0" prefViewportWidth="750.0" vbarPolicy="ALWAYS">
                    <content>
                        <AnchorPane prefHeight="850.0" prefWidth="750.0" BorderPane.alignment="CENTER">
                            <children>
                                <ImageView fx:id="icon" fitHeight="200.0" fitWidth="150.0" layoutX="14.0" layoutY="14.0" pickOnBounds="true" preserveRatio="true" />
                                <ImageView fx:id="leader" fitHeight="200.0" fitWidth="150.0" layoutX="586.0" layoutY="14.0" pickOnBounds="true" preserveRatio="true" />
                                <Label fx:id="title" layoutX="231.0" layoutY="88.0" text="Punjab -2017" textAlignment="CENTER">
                                    <font>
                                        <Font name="American Typewriter Bold" size="43.0" />
                                    </font>
                                </Label>
                                <Label fx:id="overview" layoutX="15.0" layoutY="283.0" text="Party Overview">
                                    <font>
                                        <Font name="AppleGothic Regular" size="23.0" />
                                    </font>
                                </Label>

                                <Separator layoutX="15.0" layoutY="316.0" prefHeight="3.0" prefWidth="689.0" AnchorPane.bottomAnchor="1181.0" AnchorPane.leftAnchor="15.0" AnchorPane.rightAnchor="46.0" AnchorPane.topAnchor="316.0">
                                    <opaqueInsets>
                                        <Insets />
                                    </opaqueInsets>
                                </Separator>
                                <Label fx:id="overtext" layoutX="17.0" layoutY="336.0" prefHeight="172.0" prefWidth="686.0" text="Label">
                                    <font>
                                        <Font name="Calibri Light" size="15.0" />
                                    </font>
                                </Label>
                                <Label fx:id="prediction" layoutX="19.0" layoutY="497.0" text="Election Prediction">
                                    <font>
                                        <Font name="AppleGothic Regular" size="23.0" />
                                    </font>
                                </Label>
                                <Label fx:id="predictext" layoutX="21.0" layoutY="547.0" prefHeight="172.0" prefWidth="686.0" text="Label">
                                    <font>
                                        <Font name="Calibri Light" size="15.0" />
                                    </font>
                                </Label>
                                <Separator layoutX="19.0" layoutY="530.0" prefHeight="3.0" prefWidth="689.0">
                                    <opaqueInsets>
                                        <Insets />
                                    </opaqueInsets>
                                </Separator>
                                <Label fx:id="leaders" layoutX="19.0" layoutY="719.0" text="Major Leaders">
                                    <font>
                                        <Font name="AppleGothic Regular" size="23.0" />
                                    </font>
                                </Label>
                                <Separator layoutX="19.0" layoutY="752.0" prefHeight="3.0" prefWidth="689.0">
                                    <opaqueInsets>
                                        <Insets />
                                    </opaqueInsets>
                                </Separator>
                                <Label  layoutX="21.0" layoutY="769.0" prefHeight="172.0" prefWidth="686.0" text="Label" fx:id="leaderstext">
                                    <font>
                                        <Font name="Calibri Light" size="15.0" />
                                    </font>
                                </Label>
                            </children>
                        </AnchorPane>
                    </content>
                </ScrollPane>
            </center>
        </BorderPane>
    </children>
</fx:root>
Main.java

    package sample;

import javafx.application.Application;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;


public class Main extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        CustomControl customControl = new CustomControl();
      //  customControl.setText("Hello!");
        customControl.setList();
        customControl.setIcon("/Users/Arun/Dropbox/Camera Saves/camera uploads/2015-04-02 16.23.11.jpg");
        customControl.setLeader("/Users/Arun/Dropbox/Camera Saves/camera uploads/2015-04-02 16.23.11.jpg");
        stage.setScene(new Scene(customControl));
        stage.setTitle("Custom Control");
        stage.setWidth(1017);
        stage.setHeight(800);
        stage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

万分之一的提前感谢….

您的导入错误。删除
import java.awt.*
并为JavaFX控件添加正确的导入(
导入JavaFX.scene.control.Label;
等)

您的导入错误。删除
import java.awt.*并为JavaFX控件添加正确的导入(
导入JavaFX.scene.control.Label;
,等等)

感谢加载,无法想象如此愚蠢的错误:)已被此问题困扰了3天。感谢加载,无法想象如此愚蠢的错误:)已被此问题困扰了3天。
    package sample;

import javafx.application.Application;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;


public class Main extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        CustomControl customControl = new CustomControl();
      //  customControl.setText("Hello!");
        customControl.setList();
        customControl.setIcon("/Users/Arun/Dropbox/Camera Saves/camera uploads/2015-04-02 16.23.11.jpg");
        customControl.setLeader("/Users/Arun/Dropbox/Camera Saves/camera uploads/2015-04-02 16.23.11.jpg");
        stage.setScene(new Scene(customControl));
        stage.setTitle("Custom Control");
        stage.setWidth(1017);
        stage.setHeight(800);
        stage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}