尝试调整边框窗格时JavaFX:java.lang.NullPointerException

时间:2015-02-06 18:05:48

标签: javafx resize borderpane

晚上好人,

我还处于Java编程的最初阶段。我目前的目标是在启动初始化期间调整BorderPane的顶部大小。当然,拥有一个包含静态常量的单独类(比如大小的值)会非常有用。

不幸的是,我总是得到一个NullPointerException。我猜这是我看不到的非常简单的东西。我希望你能帮助我。

提前多多感谢!

package main;

import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.Region;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;


public class Main extends Application {

@FXML
Pane main_top;

@Override
public void start(Stage primaryStage) {
    try {
        BorderPane root = (BorderPane)FXMLLoader.load(getClass().getResource("MainGui.fxml"));
        Scene scene = new Scene(root,400,400);
        scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
        primaryStage.setScene(scene);
        primaryStage.show();
        primaryStage.setMaximized(true);
        BorderPane.layoutInArea(main_top, 0, 0, 200, GlobalProperties.MainGuiTopPaneHeight, 0, null, false, false, null, null, false);
    } catch(Exception e) {
        e.printStackTrace();
    }

}

编辑:

java.lang.NullPointerException
at javafx.scene.layout.Region.layoutInArea(Unknown Source)
at main.Main.start(Main.java:27)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$153(Unknown Source)
at com.sun.javafx.application.LauncherImpl$$Lambda$51/2117598489.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$166(Unknown Source)
at com.sun.javafx.application.PlatformImpl$$Lambda$44/1051754451.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null$164(Unknown Source)
at com.sun.javafx.application.PlatformImpl$$Lambda$47/1401292544.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$165(Unknown Source)
at com.sun.javafx.application.PlatformImpl$$Lambda$46/1775282465.run(Unknown Source)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$141(Unknown Source)
at com.sun.glass.ui.win.WinApplication$$Lambda$37/1109371569.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

EDIT2(MainGui.fxml):

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

<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.BorderPane?>

<BorderPane xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="main.MainGuiController">
   <top>
      <Pane fx:id="main_top" prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: #525260;" BorderPane.alignment="CENTER" />
   </top>
   <left>
      <Pane fx:id="main_left" prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: #525252;" BorderPane.alignment="CENTER" />
   </left>
   <center>
      <Pane fx:id="main_center" prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER" />
   </center>
   <right>
      <Pane fx:id="main_right" prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: #525252;" BorderPane.alignment="CENTER" />
   </right>
   <bottom>
      <Pane fx:id="main_bottom" prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: #525260;" BorderPane.alignment="CENTER" />
   </bottom>
</BorderPane>

1 个答案:

答案 0 :(得分:0)

经过几天的反复试验,我找到了解决方案。现在,它解决后看起来很容易; - )

我所要做的就是在控制器中添加控制器初始化界面。

package main;

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

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.layout.Pane;


public class MainGuiController implements Initializable{

//***** @author n3wton

//**** FXML declarations for MainGui.fxml
@FXML
private Pane main_top;
@FXML
private Button button1;

//***** Beginning of auto-initialized method 
@Override
public void initialize(URL arg0, ResourceBundle arg1) {

    button1.setPrefSize(1, 1);
    main_top.setPrefSize(50, 10);

    }
}