我正在开发一个带有IntelliJ的Scala / JavaFX项目和一个scala插件 Java中访问fxml文件元素的典型方法是为要访问的每个元素设置一个id,然后在controllerclass中声明一个变量,如“@FXML private Label mylabelid;”。 在Scala中,它的工作方式与您在源代码中看到的几乎相同。但是我在第73行得到了一个NullPointerException(标有PROBLEM-comment)。这是唯一的标签,只是设置为null。我尝试了不同的东西。除了missingInputLabel之外,每个元素都按预期设置 LoginView.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import java.net.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<GridPane xmlns:fx="http://javafx.com/fxml/1" fx:controller="logingui.LoginController">
<stylesheets>
<URL value="@/data/gtevStyle.css" />
</stylesheets>
<Label text="SSH-Nutzername:" GridPane.columnIndex="0" GridPane.rowIndex="0" />
<TextField fx:id="sshNameField" promptText="SSH-Nutzername" onKeyReleased="#onKeyReleased_textField" GridPane.columnIndex="1" GridPane.rowIndex="0" />
<Label text="SSH-Passwort:" GridPane.columnIndex="0" GridPane.rowIndex="1" />
<PasswordField fx:id="sshPasswdField" promptText="SSH-Passwort" onKeyReleased="#onKeyReleased_textField" GridPane.columnIndex="1" GridPane.rowIndex="1" />
<Label text="Datenbank-Nutzername:" GridPane.columnIndex="0" GridPane.rowIndex="2" />
<TextField fx:id="databaseNameField" promptText="Datenbank-Nutzername" onKeyReleased="#onKeyReleased_textField" GridPane.columnIndex="1" GridPane.rowIndex="2" />
<Label text="Datenbank-Passwort:" GridPane.columnIndex="0" GridPane.rowIndex="3" />
<PasswordField fx:id="databasePasswdField" promptText="Datenbank-Passwort" onKeyReleased="#onKeyReleased_textField" GridPane.columnIndex="1" GridPane.rowIndex="3" />
<Label text="Name oder Passwort falsch" fx:id="nameOrPasswdWrong" styleClass="warningLabel" GridPane.columnIndex="1" GridPane.rowIndex="4" />
<Label text="Fehlende Angaben" fx:id="missingInputLabel" styleClass="warningLabel" GridPane.columnIndex="1" GridPane.rowIndex="5" />
<Button text="Login" onAction="#login" GridPane.columnIndex="0" GridPane.rowIndex="5" />
</GridPane>
LoginController.scala:
package logingui
import java.net.URL
import java.sql.SQLException
import java.util.ResourceBundle
import java.util.logging.Level
import java.util.logging.Logger
import javafx.fxml.FXML
import javafx.fxml.Initializable
import javafx.scene.control.Label
import javafx.scene.control.PasswordField
import javafx.scene.control.TextField
import javafx.scene.input.KeyEvent
import javafx.scene.input.KeyCode
import connection.GTEVConnection
import javafx.stage.Stage
class LoginController extends Initializable {
@FXML
private var nameOrPasswdWrong: Label = _
@FXML
private var missingInputLabel: Label = _ //TODO missingInputLabel is set to null?!
@FXML
private var sshNameField: TextField = _
@FXML
private var databaseNameField: TextField = _
@FXML
private var sshPasswdField: PasswordField = _
@FXML
private var databasePasswdField: PasswordField = _
//Um einfach auf alle Text-/Passwortfelder referenzieren zu können
private var textFields: Array[TextField] = Array[TextField](sshNameField, databaseNameField, sshPasswdField, databasePasswdField)
var callbackWhenFinished: () => Unit = () => GTEVConnection.close() //Default: Simply close GTEVConnection
def onKeyReleased_textField(ev: KeyEvent) { //TODO declare def as private and mark with @FXML?
if (ev.getCode == KeyCode.ENTER) {
login()
} else {
nameOrPasswdWrong.setVisible(false)
}
}
def login() {
checkInputOfTextFields()
try {
if (!missingInputLabel.isVisible) {
GTEVConnection.createConnection(sshNameField.getText, sshPasswdField.getText, databaseNameField.getText, databasePasswdField.getText)
callbackWhenFinished()
sshNameField.getScene.getWindow.asInstanceOf[Stage].close()
}
} catch {
case ex: SQLException =>
Logger.getLogger("LoginContollerLogger").log(Level.SEVERE, null, ex) //TODO Loggername durch Klassennamen ersetzen
nameOrPasswdWrong.setVisible(true)
}
}
private def checkInputOfTextFields() {
var anyMissingInput: Boolean = false
for (t: TextField <- textFields) {
if (t.getText.isEmpty) {
anyMissingInput = true
t.getStyleClass.add("missingInput")
} else {
t.getStyleClass.remove("missingInput")
}
missingInputLabel.setVisible(anyMissingInput)
}
}
override def initialize(location: URL, resources: ResourceBundle) {
nameOrPasswdWrong.setVisible(false)
missingInputLabel.setVisible(false) //PROBLEM: NullPointerException
}
}
告诉我什么时候我错过了什么或你想知道别的事情,谢谢你的帮助 问候跟踪器
答案 0 :(得分:0)
我找到了解决方法。 当我将missingInputLabel声明为public时,它工作得很好。但实际上我不想因为“信息隐藏”而宣布公开。