使用FXML控制器

时间:2018-11-03 20:03:49

标签: controller fxml

尝试学习FXML。我有一个使用硬编码UI运行的程序,正在尝试将其移至FXML。 我认为这是我有一个问题理解的控制器。我需要程序读取txt文件,就像现在创建ArrayList一样,并且不确定如何使用控制器达到这一点,因此我可以为xml中所需的按钮编写eventHandlers。 (这些按钮正在替换ListView)。 (下面的代码删除了所有硬编码的UI元素。) 而且不知道如何处理scanner.close()下方的内容,因为它与ListView绑定在一起。

感谢您的帮助。

public class Building extends Application {

    BuildingInfo bInfo = new BuildingInfo();

    //Declare an ImageView array for the building images
    private ImageView[] BuildingImages = {
            new ImageView("file:resources/images/dh_ext_side.png"),
            new ImageView("file:resources/images/at_ext_back.png"),
            new ImageView("file:resources/images/jwh_ext_court_door.png"),
            new ImageView("file:resources/images/etrc1_ext.png"),
            new ImageView("file:resources/images/ps_ext_lot.png"),
            new ImageView("file:resources/images/co_ext_front.png"),
            new ImageView("file:resources/images/autobody_ext_bays.png"),
            new ImageView("file:resources/images/sh_ext_front.png")
    };

    //create an ObservableArrayList
    private ObservableList<Building> list = FXCollections.observableArrayList();


    //Override the start method in the Application Class
    @Override
    public void start(Stage primaryStage) throws IOException, ClassNotFoundException {

        Scanner scanner = new Scanner(new File("resources/Buildings.txt"));


        //Read the data file
        while (scanner.hasNext()) {
            String campus = scanner.next();
            String name = scanner.next();
            double latitude = scanner.nextDouble();
            double longitude = scanner.nextDouble();
            String imageName = scanner.next();
            String buildingCode = scanner.next();

            //Instantiate the constructors and catch exceptions
            try {
                Coordinates coordinates = new Coordinates(latitude, longitude);
                Building building = new Building(campus, name, coordinates, imageName, buildingCode);
                list.add(building);
            } catch (InvalidLatitudeException e) {
                System.out.println("\nInvalid latitude coordinate");
            } catch (InvaildLongitudeException e) {
                System.out.println("\nInvalid longitude coordinate");
            }
        }

        //Close the file
        scanner.close();

            buildingName.setText(bInfo.getList(listView.getSelectionModel().getSelectedIndex()).getCampus() +
                    " - " + bInfo.getList(listView.getSelectionModel().getSelectedIndex()).getName());
            latitude.setText("Your building Coordinates: " + String.valueOf(bInfo.getList(listView.getSelectionModel().getSelectedIndex()).getCoordinates().getLatitude()));
            longitude.setText(", " + String.valueOf(bInfo.getList(listView.getSelectionModel().getSelectedIndex()).getCoordinates().getLongitude()));
        });

    }
}    

XML:

<VBox xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="gtcc_buildings.Controller">
    <children>
        <HBox alignment="CENTER" prefHeight="73.0" style="-fx-background-color: #1d572e;">
            <children>
                <Label fx:id="buildingNames" alignment="CENTER" text="Welcome to GTCC" textFill="#f2b653">
                    <font>
                        <Font name="Calibri" size="36.0" />
                    </font>
                    <padding>
                        <Insets bottom="10.0" top="10.0" />
                    </padding>
                </Label>
            </children>
        </HBox>
        <ImageView fx:id="buildingImage" fitHeight="342.0" fitWidth="512.0" pickOnBounds="true">
            <image>
                <Image url="@../../Titans-Logo_0_0.png" />
            </image></ImageView>
        <HBox alignment="CENTER" spacing="35.0" style="-fx-background-color: #1d572e;">
            <children>
                <Button fx:id="backButton" mnemonicParsing="false" onAction="#backButtonClick" prefWidth="73.0" text="BACK">
                    <textFill>
                        <LinearGradient endX="1.0" endY="1.0">
                            <stops>
                                <Stop color="BLACK" />
                                <Stop color="BLACK" offset="0.007662835249042145" />
                                <Stop color="#f2b653" offset="0.6743295019157087" />
                                <Stop color="#f2b653" offset="1.0" />
                            </stops>
                        </LinearGradient>
                    </textFill></Button>
                <Label fx:id="coordinateLocation" alignment="CENTER" prefHeight="47.0" prefWidth="240.0" text="Coordinate Location" textFill="#f2b653">
                    <font>
                        <Font name="Calibri" size="18.0" />
                    </font>
                    <padding>
                        <Insets bottom="10.0" top="10.0" />
                    </padding>
                </Label>
                <Button fx:id="forwardButton" mnemonicParsing="false" onAction="#forwardButtonClick" text="FORWARD">
                    <textFill>
                        <LinearGradient endX="1.0" endY="1.0">
                            <stops>
                                <Stop color="#f2b653" />
                                <Stop color="#f2b653" offset="0.007662835249042145" />
                                <Stop color="BLACK" offset="0.6743295019157084" />
                                <Stop color="BLACK" offset="1.0" />
                            </stops>
                        </LinearGradient>
                    </textFill></Button>
            </children>
        </HBox>
    </children>
</VBox>

控制器:

public class Controller implements Initializable {
    ArrayList<Building> buildings = new ArrayList<>();

    //Create FXML links for controllers
    @FXML
    Label buildingNames;
    @FXML
    Button backButton;
    @FXML
    Label coordinateLocation;
    @FXML
    Button forwardButton;
    @FXML
    ImageView buildingImage;

    @Override
    public void initialize(URL location, ResourceBundle resources) {

    }

0 个答案:

没有答案