클래스와 중첩 된 JavaFX Controller
이 포함 된 아래 코드가 있습니다. JavaFX 컨트롤러 클래스를 초기화하고 볼 수있는 것처럼 인스턴스를 가져옵니다.공용 클래스 내의 중첩 된 JavaFX 컨트롤러
SpeechWindow 클래스 : SpeechWindow
의 인스턴스를 생성하는 기본 자바 FX 애플리케이션 클래스에서 호출하는
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.layout.BorderPane?>
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.60" xmlns:fx="http://javafx.com/fxml/1">
<center>
<TextArea fx:id="textArea" editable="false" prefHeight="200.0" prefWidth="200.0" promptText="...no response" wrapText="true" BorderPane.alignment="CENTER" />
</center>
</BorderPane>
시도 : 여기
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import application.Main;
import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Scene;
import javafx.scene.control.TextArea;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
/**
* SpeechWindow
*
*/
public class SpeechWindow extends Stage {
private Container container = new Container();
/**
* Constructor
*/
public SpeechWindow() {
setTitle("SpeechResults");
setScene(new Scene(container));
}
/**
* @param text
*/
public void appendText(String text) {
container.appendText(text);
}
/**
*
*/
public void clear() {
container.clear();
}
public class Container extends BorderPane implements Initializable {
@FXML
private TextArea textArea;
public Container() {
// FXMLLOADER
FXMLLoader loader = new FXMLLoader(getClass().getResource("SpeechWindow.fxml"));
try {
loader.load();
} catch (IOException ex) {
ex.printStackTrace();
}
}
@Override
public void initialize(URL location , ResourceBundle resources) {
System.out.println("Container has been initialized..");
}
public void appendText(String text) {
Platform.runLater(() -> textArea.appendText(text + "\n"));
}
public void clear() {
Platform.runLater(textArea::clear);
}
}
}
IT는 FXML 코드입니다 그리고 appendText();
메쏘드를 호출하십시오. 널 포인터가 나오는 이유는 무엇입니까?
목표는 다음과 같습니다
그런 다음 내가 외부 클래스 인스턴스를 생성하고 그것을 사용하려고합니다.
간다 무엇이 잘못 : 내가 방법 appendText();
을 SpeechWindow
클래스의 인스턴스를 생성하고 호출하는 경우
내가 그런 일이 왜, 내가 모든 것을 해 괜찮아가 완료보고 ... 널 포인터 예외가 내 실수?
Oh daaaamn 내가 Fabian에게 감사 드려요! 나는 그것이 작동하는지 확인한 다음 대답으로 표시 할 것입니다 ... – GOXR3PLUS