控制器中的参考标签

时间:2016-09-06 16:56:36

标签: javafx javafx-2 javafx-8

我正在尝试使用javafx中的fxml创建一个简单的ui。我试图引用我在Controller类的.fxml文件中创建的标签。但由于某种原因,当我查看fx:标签时,我会看到一个错误,其中给出了fx:id。它说Cannot set javafx.scene.control.Label to field 'leaders'。但它适用于ImageView。代码如下:

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>

CustomControl.java

package sample;

import java.awt.*;
import java.io.File;
import java.io.IOException;

import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.ListView;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.VBox;

/**
 * Sample custom control hosting a text field and a button.
 */
public class CustomControl extends VBox {
    @FXML
    private ListView mylist;
    @FXML
    private ImageView icon;
    @FXML
    private ImageView leader;

    @FXML
    private Label overview;
    @FXML
    private Label overtext;
    @FXML
    private Label prediction;
    @FXML
    private Label predictext;
    @FXML
    private Label leaders;
    @FXML
    private Label leaderstext;


    public CustomControl() {
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("custom_control.fxml"));
        fxmlLoader.setRoot(this);
        fxmlLoader.setController(this);

        try {
            fxmlLoader.load();
        } catch (IOException exception) {
            throw new RuntimeException(exception);
        }
    }
    public void setList(){
    mylist.setItems(FXCollections.observableArrayList("Home","BJP","Congress","AAP"));
}

   public void setOverview(String txt) {
        overview.setText(txt);
    }
    public void setOverviewtext(String txt){
        overtext.setText(txt);
    }

public void setLeader(String data){
    File file =new File(data);
    Image image = new Image(file.toURI().toString());
    leader.setImage(image);
}
public void setIcon(String data){
    File file =new File(data);
    Image image = new Image(file.toURI().toString());
    icon.setImage(image);
}

 public void setPrediction(String txt){
      prediction.setText(txt);
  }
  public void setPredictext(String txt){
      predictext.setText(txt);
  }
  public void setLeaders(String txt){
      leaders.setText(txt);
  }
  public void setLeaderstext(String txt){
      leaderstext.setText(txt);
  }

}

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);
    }
}

提前一百万谢谢......

1 个答案:

答案 0 :(得分:2)

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