我正在使用IntelliJ和SceneBuilder创建预算管理器GUI。我不明白为什么Scene Builder只识别我的一些@FXML标记,而不是全部。即使当我在.fxml文件中手动输入fx:id时,它也将显示在特定元素的“场景构建器”代码部分中,但不会链接到控制器。具体来说,我正在尝试将项目添加到两个选择框中,但是我正在获得NPE。我不确定发生了什么。
//工作控制器
package budget_master.controller;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.stage.Stage;
public class main_scrn_cntrl {
private Stage primaryStage;
@FXML
private Button exit_btn;
//method to handle exit btn function
@FXML
private void handleExitBtn(){
primaryStage = (Stage) exit_btn.getScene().getWindow();
primaryStage.close();
}
}
//正在使用.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.Font?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="600.0" prefWidth="800.0" xmlns="http://javafx.com/javafx/8.0.121" xmlns:fx="http://javafx.com/fxml/1" fx:controller="budget_master.controller.main_scrn_cntrl">
<children>
<BorderPane prefHeight="600.0" prefWidth="800.0">
<top>
<FlowPane alignment="CENTER" prefHeight="40.0" prefWidth="200.0" BorderPane.alignment="CENTER">
<children>
<Label text="Budget Master 2019">
<font>
<Font size="16.0" />
</font>
</Label>
</children></FlowPane>
</top>
<center>
<TabPane fx:id="budgetTabs" prefHeight="200.0" prefWidth="200.0" tabClosingPolicy="UNAVAILABLE" BorderPane.alignment="CENTER">
<tabs>
<Tab text="Ledger">
<content>
<fx:include source="ldgr_tab.fxml" />
</content>
</Tab>
<Tab text="Envelopes" >
<content>
<fx:include source="env_tab.fxml" />
</content>
</Tab>
<Tab text="Reports" />
</tabs>
</TabPane>
</center>
<bottom>
<FlowPane alignment="BOTTOM_RIGHT" prefHeight="40.0" prefWidth="200.0" BorderPane.alignment="CENTER">
<children>
<ButtonBar prefHeight="40.0" prefWidth="200.0">
<buttons>
<Button fx:id="exit_btn" mnemonicParsing="false" onAction="#handleExitBtn" text="Exit" />
</buttons>
<FlowPane.margin>
<Insets right="5.0" />
</FlowPane.margin>
</ButtonBar>
</children>
</FlowPane>
</bottom></BorderPane>
</children>
</AnchorPane>
//控制器不起作用
package budget_master.controller;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ChoiceBox;
import java.net.URL;
import java.util.ArrayList;
import java.util.ResourceBundle;
/**
* Created by Jengel1 on 12/24/2018.
*/
public class env_tab_cntrl implements Initializable {
static ArrayList<String> env_list = new ArrayList();
static ObservableList<String> env_cb_list =
FXCollections.observableArrayList(env_list);
@FXML
private static ChoiceBox env_to_cb;
@FXML
private static ChoiceBox env_from_cb;
@FXML
public static void initialize() {
System.out.println("env init");
load_env_list();
env_to_cb.setValue("Select Envelope");
env_from_cb.setValue("Select Envelope");
env_to_cb.setItems(env_cb_list);
env_from_cb.setItems(env_cb_list);
}
private static void load_env_list(){
env_list.add("Check Balance");
env_list.add("Rent");
env_list.add("Dogs");
}
@FXML
@Override
public void initialize(URL location, ResourceBundle resources) {
load_env_list();
env_to_cb.setValue("Select Envelope");
env_from_cb.setValue("Select Envelope");
env_to_cb.setItems(env_cb_list);
env_from_cb.setItems(env_cb_list);
}
}
//。fxml文件不起作用
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ChoiceBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.FlowPane?>
<?import javafx.scene.text.Font?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="560.0" prefWidth="800.0" xmlns="http://javafx.com/javafx/8.0.121" xmlns:fx="http://javafx.com/fxml/1" fx:controller="budget_master.controller.env_tab_cntrl">
<children>
<BorderPane prefHeight="490.0" prefWidth="800.0">
<top>
<FlowPane alignment="CENTER" prefHeight="30.0" prefWidth="800.0">
<children>
<ChoiceBox fx:id="env_to_cb" prefWidth="150.0">
<FlowPane.margin>
<Insets left="10.0" right="10.0" />
</FlowPane.margin>
</ChoiceBox>
<ChoiceBox fx:id="env_from_cb" prefWidth="150.0">
<FlowPane.margin>
<Insets left="10.0" right="10.0" />
</FlowPane.margin>
</ChoiceBox>
</children></FlowPane>
</top>
</children>
</AnchorPane>