我在使用label.setText(String)
时遇到问题。我已为我尝试使用fx:id
的标签设置了setText()
,并将我的Controller与我的.fxml文件相关联。
我最初有这个问题尝试在加载场景时用我的SQLite数据库中的值填充标签,并决定通过Button(ActionEvent)
加载场景时它是否有效,因为我不确定我的{{1}工作正常。
我可以将变量的值打印到控制台,但不是EventListener
。我查看了其他已提交的问题,例如this one和this one also,但没有进一步转发。
感谢任何帮助!
Options.java
label.setText()
OptionsController.java(我删除了很多此问题不需要的代码)
package PengVapour;
import Controllers.OptionsController;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
public class Options extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage optionsStage) {
try {
FXMLLoader loader = new FXMLLoader(getClass().getResource("../FXML/Options.fxml"));
Parent root = (Parent)loader.load();
OptionsController opCont = (OptionsController)loader.getController();
optionsStage.addEventHandler(WindowEvent.WINDOW_SHOWN, event -> opCont.handleSceneShownEvent());
Scene scene = new Scene(root);
scene.getStylesheets().add(getClass().getResource("../CSS/PengVapour.css").toExternalForm());
optionsStage.setTitle("Peng Vapour - Application Options");
optionsStage.setScene(scene);
optionsStage.show();
optionsStage.setResizable(false);
} catch (Exception e) {
e.printStackTrace();
}
}
}
OptionsDB.java(我删除了很多此问题不需要的代码)
package Controllers;
import Database.OptionsDB;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.control.*;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.net.URL;
import java.sql.SQLException;
import java.util.ResourceBundle;
public class OptionsController implements Initializable {
@FXML MenuItem fileExit;
@FXML Button mainMenu;
@FXML TextField concentrateThreshold;
@FXML TextField vgThreshold;
@FXML TextField pgThreshold;
@FXML TextField nicotineThreshold;
@FXML TextField cleanerThreshold;
@FXML public static Label currentConcentrateThresholdLabel;
@FXML public static Label currentVGThresholdLabel;
@FXML public static Label currentPGThresholdLabel;
@FXML public static Label currentNicotineThresholdLabel;
@FXML public static Label currentCleanerThresholdLabel;
@FXML CheckBox confirm;
@FXML Button submit;
@FXML Button testB;
public static double concentrateUpdateValue;
public static double vgUpdateValue;
public static double pgUpdateValue;
public static double nicotineUpdateValue;
public static double cleanerUpdateValue;
public boolean confirmed = false;
@Override
public void initialize(URL location, ResourceBundle resources) {}
@FXML
public void handleSceneShownEventTest(ActionEvent e) throws InvocationTargetException {
OptionsDB.showCurrentThresholds();
}
Options.fxml
package Database;
import Controllers.OptionsController;
import java.lang.reflect.InvocationTargetException;
import java.sql.*;
public class OptionsDB {
public static String concentrateCurrentThreshold;
public static String vgCurrentThreshold;
public static String pgCurrentThreshold;
public static String nicotineCurrentThreshold;
public static String cleanerCurrentThreshold;
public static void main() {
connect();
}
public static Connection connect() {
String url = "jdbc:sqlite:C:\\Users\\PengStation420\\IdeaProjects\\PengVapour\\PVIM.sqlite";
Connection conn = null;
try {
conn = DriverManager.getConnection(url);
} catch (SQLException e) {
System.out.println(e.getMessage());
}
return conn;
}
public static void showCurrentThresholds() throws InvocationTargetException {
String sqlShowCurrentThresholds = "SELECT * FROM Options";
try (Connection conn = connect();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sqlShowCurrentThresholds)){
while (rs.next()) {
concentrateCurrentThreshold = String.valueOf(rs.getDouble("ConcentrateThreshold"));
vgCurrentThreshold = String.valueOf(rs.getDouble("VGThreshold"));
pgCurrentThreshold = String.valueOf(rs.getDouble("PGThreshold"));
nicotineCurrentThreshold = String.valueOf(rs.getDouble("NicotineThreshold"));
cleanerCurrentThreshold = String.valueOf(rs.getDouble("CleanerThreshold"));
System.out.println(concentrateCurrentThreshold);
System.out.println(vgCurrentThreshold);
System.out.println(pgCurrentThreshold);
System.out.println(nicotineCurrentThreshold);
System.out.println(cleanerCurrentThreshold);
OptionsController.currentConcentrateThresholdLabel.setText(concentrateCurrentThreshold);
OptionsController.currentVGThresholdLabel.setText(vgCurrentThreshold);
OptionsController.currentPGThresholdLabel.setText(pgCurrentThreshold);
OptionsController.currentNicotineThresholdLabel.setText(nicotineCurrentThreshold);
OptionsController.currentCleanerThresholdLabel.setText(cleanerCurrentThreshold);
}
System.out.println();
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
}
NullPointerException在OptionsDB.java中的<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.CheckBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.VBox?>
<VBox fx:id="vBox" alignment="CENTER" minHeight="450" minWidth="600" prefHeight="450.0" prefWidth="600.0" stylesheets="@../CSS/PengVapour.css" xmlns="http://javafx.com/javafx/8.0.121" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Controllers.OptionsController">
<children>
<MenuBar>
<menus>
<Menu mnemonicParsing="false" text="File">
<items>
<MenuItem fx:id="fileExit" mnemonicParsing="false" onAction="#menuBarFileExit" text="Exit" />
</items>
</Menu>
</menus>
</MenuBar>
<Label fx:id="head" alignment="CENTER" prefHeight="58.0" prefWidth="600.0" stylesheets="@../CSS/PengVapour.css" text="Options" textAlignment="CENTER" />
<AnchorPane prefHeight="368.0" prefWidth="407.0">
<children>
<Button fx:id="testB" layoutX="250.0" layoutY="320.0" mnemonicParsing="false" onAction="#handleSceneShownEventTest" text="Test" />
<Label layoutX="99.0" layoutY="29.0" text="Set Running Low Thresholds:" />
<Label layoutX="78.0" layoutY="75.0" text="Concentrates:" />
<TextField fx:id="concentrateThreshold" layoutX="173.0" layoutY="71.0" prefHeight="29.0" prefWidth="100.0" />
<Label layoutX="146.0" layoutY="107.0" text="VG:" />
<TextField fx:id="vgThreshold" layoutX="173.0" layoutY="103.0" prefHeight="29.0" prefWidth="100.0" />
<Label layoutX="146.0" layoutY="139.0" text="PG:" />
<TextField fx:id="pgThreshold" layoutX="173.0" layoutY="135.0" prefHeight="29.0" prefWidth="100.0" />
<Label layoutX="110.0" layoutY="172.0" text="Nicotine:" />
<TextField fx:id="nicotineThreshold" layoutX="173.0" layoutY="168.0" prefHeight="29.0" prefWidth="100.0" />
<Label layoutX="51.0" layoutY="206.0" text="Isopropyl Alcohol:" />
<TextField fx:id="cleanerThreshold" layoutX="173.0" layoutY="202.0" prefHeight="29.0" prefWidth="100.0" />
<Button fx:id="mainMenu" layoutX="490.0" layoutY="319.0" mnemonicParsing="false" onAction="#mainMenuButton" text="Main Menu" />
<Button fx:id="submit" layoutX="344.0" layoutY="266.0" mnemonicParsing="false" onAction="#submitButton" text="Submit" visible="false" />
<CheckBox fx:id="confirm" layoutX="174.0" layoutY="271.0" mnemonicParsing="false" onAction="#confirmChanges" text="Confirm Change(s)" />
<Label layoutX="281.0" layoutY="75.0" text="ml" />
<Label layoutX="281.0" layoutY="107.0" text="ml" />
<Label layoutX="281.0" layoutY="139.0" text="ml" />
<Label layoutX="281.0" layoutY="172.0" text="ml" />
<Label layoutX="281.0" layoutY="206.0" text="ml" />
<Label layoutX="388.0" layoutY="29.0" text="Current Thresholds:" />
<Label layoutX="517.0" layoutY="74.0" text="ml" />
<Label layoutX="517.0" layoutY="106.0" text="ml" />
<Label layoutX="517.0" layoutY="138.0" text="ml" />
<Label layoutX="517.0" layoutY="171.0" text="ml" />
<Label layoutX="517.0" layoutY="205.0" text="ml" />
<Label fx:id="currentConcentrateThresholdLabel" layoutX="444.0" layoutY="74.0" text="??" />
<Label fx:id="currentVGThresholdLabel" layoutX="444.0" layoutY="106.0" text="??" />
<Label fx:id="currentPGThresholdLabel" layoutX="444.0" layoutY="138.0" text="??" />
<Label fx:id="currentNicotineThresholdLabel" layoutX="444.0" layoutY="171.0" text="??" />
<Label fx:id="currentCleanerThresholdLabel" layoutX="444.0" layoutY="205.0" text="??" />
</children>
</AnchorPane>
</children>
</VBox>
方法的OptionsController.currentConcentrateThresholdLabel.setText(concentrateCurrentThreshold);
处抛出。
我认为我错过了一些愚蠢的东西。