的数据。这里有一个新的项目与相关的代码。正如您在textfield中看到的那样,即使initData应该初始化testVar变量,它也不会发生,并且在initialize()变量中为null。我需要在那里获取变量,所以我可以从网络连接中将数据从表中取出。下面的代码:ObjectOutputStream在PlayerList.java中不可用 - 无法再获取表
FXMLController.java:
package de.freakyonline.testproject;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
public class FXMLController implements Initializable {
String testVar;
@FXML
private ResourceBundle resources;
@FXML
private URL location;
@FXML
private TextArea testTextArea;
@Override
public void initialize(URL url, ResourceBundle rb) {
// Here it's null
testTextArea.appendText("TestVar: " + testVar);
}
void initData(Remote remoteObj) {
this.testVar = remoteObj.getVar();
}
}
MainApp.java:
package de.freakyonline.testproject;
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class MainApp extends Application {
Thread remote;
Remote remoteObj;
String testVar = new String("success");
@Override
public void start(Stage stage) throws Exception {
FXMLLoader root = new FXMLLoader(
getClass().getResource("/fxml/Scene.fxml")
);
try {
remoteObj = new Remote(testVar);
remote = new Thread(remoteObj);
} catch (Exception ex) { ex.printStackTrace(); }
remote.start();
Scene scene = new Scene((Parent) root.load());
FXMLController controller = root.<FXMLController>getController();
controller.initData(remoteObj);
scene.getStylesheets().add("/styles/Styles.css");
stage.setTitle("TestProjects ...");
stage.setScene(scene);
stage.show();
}
/**
* The main() method is ignored in correctly deployed JavaFX application.
* main() serves only as fallback in case the application can not be
* launched through deployment artifacts, e.g., in IDEs with limited FX
* support. NetBeans ignores main().
*
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
Scene.fxml:
<?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.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="de.freakyonline.testproject.FXMLController">
<center>
<TextArea fx:id="testTextArea" prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER" />
</center>
</BorderPane>
Remote.java:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package de.freakyonline.testproject;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
/**
*
* @author uwe
*/
public class Remote implements Runnable {
private String testVar;
public Remote (String testVar) {
this.testVar = testVar;
}
public String getVar() {
return testVar;
}
public void run() {
}
}
请将代码减少到相关部分,因为没有人愿意阅读代码 –
的页面完成:)谢谢。 –
尝试向后追踪从它的空点 –