我目前正在进行一项任务,我需要创建一个具有两个按钮的应用程序:读/写,以及带有灰色背景和蓝色文本的textArea,它将显示写入的文件的读取内容按下按钮。我必须保存一个包含5个数字的数组,日期和一个双精度数(2.5)。 除了将文本附加到textArea之外,我已经到了让一切工作的地步......无论我在哪里传递值,它都会给我一个错误或者告诉我它不存在一个变量!
我的问题是:如何获取我从文件中读取的值以更新textArea中的文本而不仅仅是控制台?
这是我的第一个文件,它将创建应用程序并填充场景。按钮和文本区域都是正确的颜色。
package chapter17;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.text.Font;
import javafx.stage.Stage;
import java.io.IOException;
import javafx.scene.layout.Region;
import javafx.scene.control.TextArea;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
public class Exercise17_5 extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
HBox hBox = new HBox();
hBox.setSpacing(10);
hBox.setAlignment(Pos.CENTER);
Button write = new Button("Write");
Button read = new Button("Read");
TextArea readText = new TextArea("This is text testing...");
readText.setPrefColumnCount(15);
readText.setPrefRowCount(5);
readText.setWrapText(true);
readText.setStyle("-fx-text-fill: blue");
//readText.setStyle("-fx-background-color: grey");
readText.setFont(Font.font("Times", 20));
hBox.getChildren().addAll(write, read, readText);
ReadWrite readWriting = new ReadWrite();
write.setOnAction(e -> {
try {
readWriting.write();
}
catch (IOException excepiton) {
excepiton.printStackTrace();
}});
read.setOnAction(e -> {
try {
readWriting.read();
}
catch (IOException exception){
exception.printStackTrace();
}
});
Scene scene = new Scene(hBox,550,550);
primaryStage.setScene(scene);
primaryStage.setTitle("Exercise 17.5");
primaryStage.show();
Region region = (Region) readText.lookup(".content");
region.setStyle("-fx-background-color: gray");
}
}
这是第二个包含read和write方法的文件,以及存储读取内容的变量。我可以将它们打印到控制台,如下所示。它们需要附加到我现有的textArea readText。
package chapter17;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Date;
public class ReadWrite extends Exercise17_5 {
int[] numbers = {1, 2, 3, 4, 5};
public ReadWrite(){
};
public void write() throws IOException {
try (ObjectOutputStream output = new ObjectOutputStream(new FileOutputStream("Exercise17_5.dat"))){
output.writeObject(numbers);
output.writeObject(new Date());
output.writeDouble(2.5);
}
catch (IOException exception) {
exception.printStackTrace();
}
}
public void read() throws IOException {
try{
FileInputStream inputFile = new FileInputStream("Exercise17_5.dat");
ObjectInputStream input = new ObjectInputStream(inputFile);
int[] numbers = (int[])(input.readObject());
java.util.Date date = (java.util.Date)(input.readObject());
double decimal = (double)(input.readDouble());
for (int i = 0; i < numbers.length; i++){
System.out.print(numbers[i] + " ");
}
System.out.println();
System.out.println(decimal);
System.out.println(date);
input.close();
}
catch (IOException | ClassNotFoundException exception2){
exception2.printStackTrace();
}
}
}