更改Borderplane中的节点

时间:2018-10-31 12:16:52

标签: java javafx fxml

嘿,我正在尝试使用Samplecontroller java类中的addcustview()和displaycustomers()方法来更改javafx边界平面中的Nodes。我走得很远,我得到了java.lang.NullPointerException     在application.SampleController.loadUI(SampleController.java:41)     在application.SampleController.addcustview(SampleController.java:26) 我在哪里弄错了

Main.java

package application;

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

public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {
        try {
            Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
            Scene scene = new Scene(root);
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

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

}

SampleController.java

package application;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;

import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

public class SampleController implements Initializable {
    @FXML
    private BorderPane borderpane;

    @Override
    public void initialize(URL arg0, ResourceBundle arg1) {

    }

    @FXML
    private void addcustview(MouseEvent event) {
        loadUI("Addcust.fxml");
    }

    @FXML
    private void displaycustomers(MouseEvent event) {



    }

    private void loadUI(String ui) {
       Parent root = null;
       try {
        root = FXMLLoader.load(getClass().getResource(ui));
        borderpane.setCenter(root);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

   }


}

Sample.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>

<BorderPane id="borderpane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.SampleController">
    <left>
        <VBox prefHeight="300.0" prefWidth="109.0" BorderPane.alignment="CENTER">
            <children>
                <Button mnemonicParsing="false" onMouseClicked="#addcustview" prefHeight="42.0" prefWidth="181.0" style="-fx-background-color: transparent;" text="Add Customer" />
                <Button mnemonicParsing="false" onMouseClicked="#displaycustomers" prefHeight="44.0" prefWidth="183.0" style="-fx-background-color: transparent;" text="Customers" />
            </children>
        </VBox>
    </left>
    <top>
        <HBox prefHeight="100.0" prefWidth="200.0" BorderPane.alignment="CENTER">
            <children>
                <AnchorPane prefHeight="100.0" prefWidth="171.0">
                    <children>
                        <Text layoutX="6.0" layoutY="50.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Clothes ">
                            <font>
                                <Font size="46.0" />
                            </font>
                        </Text>
                        <Text fill="#207aba" layoutX="67.0" layoutY="85.0" strokeType="OUTSIDE" strokeWidth="0.0" text="M" textAlignment="JUSTIFY">
                            <font>
                                <Font name="Berlin Sans FB Demi Bold" size="25.0" />
                            </font>
                        </Text>
                    </children>
                </AnchorPane>
            </children>
        </HBox>
    </top>
</BorderPane>

0 个答案:

没有答案
相关问题