-1
我想使我的未修飾場景可拖動。我的根佈局包含帶有控件和內容的工具欄。我給了工具欄的id併爲.fxml佈局設置了控制器。 然後我做了所有步驟,如Dragging an undecorated Stage in JavaFX 但我有空指針,當試圖調用EffectUtilities.makeDraggable(stage,tool_bar); 下面是代碼:JavaFX中的NPE在調用控制器中的字段時
主要
public class UpdaterMain extends Application{
private Stage primaryStage;
private BorderPane rootLayout;
@Override
public void start(Stage primaryStage) throws Exception {
this.primaryStage = primaryStage;
this.primaryStage.initStyle(StageStyle.UNDECORATED);
this.primaryStage.initStyle(StageStyle.TRANSPARENT);
initRootLayout();
showContentOverview();
}
/**
* Initializes the root layout.
*/
public void initRootLayout() {
try {
// Load root layout from fxml file.
FXMLLoader loader = new FXMLLoader();
loader.setLocation(UpdaterMain.class.getResource("RootLayout.fxml"));
rootLayout = (BorderPane) loader.load();
// Show the scene containing the root layout.
Scene scene = new Scene(rootLayout);
primaryStage.setScene(scene);
UpdaterMainController controller =
loader.getController();
controller.registerStage(primaryStage); <<--Here is NPE
primaryStage.show();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Shows main content
*/
public void showContentOverview() {
try {
// Load person overview.
FXMLLoader loader = new FXMLLoader();
loader.setLocation(UpdaterMain.class.getResource("updater-layout.fxml"));
SplitPane contentOverview = loader.load();
// Set person overview into the center of root layout.
rootLayout.setCenter(contentOverview);
} catch (IOException e) {
e.printStackTrace();
}
}
public Stage getPrimaryStage() {
return primaryStage;
}
public static void main(String[] args) {
launch(args);
}
}
控制器
import com.iminegination.utils.EffectUtilities;
import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.ToolBar;
import javafx.stage.Stage;
public class UpdaterMainController {
@FXML
private ToolBar tool_bar;
public void registerStage(Stage stage) {
EffectUtilities.makeDraggable(stage, tool_bar); <<--Here is NPE (tool_bar is null, but why?)
}
public UpdaterMainController() {
}
}
layout.fxml
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="690.0" prefWidth="1024.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.iminegination.updater.view.UpdaterMainController">
<top>
<ToolBar id="tool_bar" nodeOrientation="RIGHT_TO_LEFT" prefHeight="40.0" prefWidth="200.0">
<items>
<Button id="exitButton" mnemonicParsing="false" text="X" onAction="#click"/>
<Button mnemonicParsing="false" text="Settings" />
<Button mnemonicParsing="false" text="_" />
</items>
</ToolBar>
</top>
</BorderPane>