gpt4 book ai didi

java - 字符串数组类型变量的值不会更新(从另一个类引用时返回空)。 JavaFX

转载 作者:行者123 更新时间:2023-12-02 06:21:49 26 4
gpt4 key购买 nike

我在这里遗漏了一些基本的东西。我有一个在类 (FirstController.java) 中声明的字符串数组 (String[] strArray),然后通过方法进行初始化。我试图从另一个类(SecondController.java)打印此数组,但返回的数组为空。请让我理解为什么会发生这种情况。谢谢你!请参阅以下代码(由 James_D 提供):

FirstController.java(这里声明并初始化了String[] strArray)

public class FirstController {

private Stage Stage2;
public String[] strArray; // this is the string array in question.

@FXML
private Button openStage2;
@FXML
private TextArea textArea1;

@FXML
private void openStage2Action(ActionEvent event) throws IOException {
Stage2 = new Stage();

FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("Second.fxml"));
Object root = fxmlLoader.load();
Scene scene = new Scene((Parent) root);
Stage2.setScene(scene);
SecondController secondController = (SecondController)fxmlLoader.getController();
secondController.textProperty().addListener(new ChangeListener<String>() {
@Override
public void changed(ObservableValue<? extends String> obs, String oldValue, String newValue) {
textArea1.setText(newValue);
strArray = newValue.split("\n"); // here the string array is initialized.
// this string array gets printed by this
for(String s:strArray){
System.out.println(s);
}
}
});
Stage2.show();
}

//public String[] getStrArray(){
// return strArray;
//}

}

SecondController.java(打印时 String[] strArray 结果为空的“另一个类”)

public class SecondController {
private StringProperty text = new SimpleStringProperty(this, "text", "");;
FirstController firstController = new FirstController();

@FXML
private TextArea textArea2 ;
public StringProperty textProperty() {
return text ;
}
public final String getText2() {
return text.get();
}
public final void setText(String text) {
this.text.set(text);
}

// ...

@FXML
private void showTextAction(ActionEvent event) {
text.set(textArea2.getText());
System.out.println(firstController.strArray); // this line prints empty array []

System.out.println(firstController.strArray.toString()); // this will result NullPointerException because the array is empty. But why it is empty?
}
}

First.java(应用程序类):

public class First extends Application {
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("First.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {launch(args);}
}

最佳答案

所以看起来 SecondController 需要访问某种从其他地方填充的字符串数组。只需在 SecondController 中定义一个 ObservableList 并为其提供一个 get(...) 方法即可。

public class SecondController {
private StringProperty text = new SimpleStringProperty(this, "text", "");
// Remove any reference to FirstController
//FirstController firstController = new FirstController();
private ObservableList<String> strArray = FXCollections.observableArrayList();

@FXML
private TextArea textArea2 ;
public StringProperty textProperty() {
return text ;
}
public final String getText2() {
return text.get();
}
public final void setText(String text) {
this.text.set(text);
}
public ObservableList<String> getStrArray() {
return strArray ;
}

// ...

@FXML
private void showTextAction(ActionEvent event) {
text.set(textArea2.getText());
System.out.println(firstController.strArray); // this line prints empty array []

System.out.println(firstController.strArray.toString()); // this will result NullPointerException because the array is empty. But why it is empty?
}
}

现在在 FirstController 中:

@FXML
private void openStage2Action(ActionEvent event) throws IOException {
Stage2 = new Stage();

FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("Second.fxml"));
Object root = fxmlLoader.load();
Scene scene = new Scene((Parent) root);
Stage2.setScene(scene);
SecondController secondController = (SecondController)fxmlLoader.getController();
secondController.textProperty().addListener(new ChangeListener<String>() {
@Override
public void changed(ObservableValue<? extends String> obs, String oldValue, String newValue) {
textArea1.setText(newValue);
strArray = newValue.split("\n"); // here the string array is initialized.
// this string array gets printed by this
for(String s:strArray){
System.out.println(s);
}
}
});
secondController.getStrArray().setAll(...); // or addAll(...) as needed.
Stage2.show();
}

关于java - 字符串数组类型变量的值不会更新(从另一个类引用时返回空)。 JavaFX,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20948173/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com