Java 中带有 GUI 的输入和输出流

input and output streams with a GUI in Java

我不明白为什么我总是收到这个错误。我需要设计一个程序,该程序使用带有按钮的 GUI 将数据写入文件,并使用按钮读取数据并将其显示在文本框中。我遇到错误,告诉我在 readData 和 writeData 方法的标记后需要一个注释名称。到目前为止,这是我的代码,请放轻松我是这个网站的新手。

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Date;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import java.io.*;

public class Exercise_17_5 extends Application{
    @Override 
    public void start(Stage primaryStage) throws FileNotFoundException, IOException{
        BorderPane pane = new BorderPane();

        HBox hbox = new HBox(10);
        hbox.setAlignment(Pos.CENTER);
        Button btWrite = new Button("Write");
        Button btRead = new Button("Read");
        hbox.getChildren().addAll(btWrite, btRead);
        hbox.setPadding(new Insets(5,5,5,5));
        pane.setBottom(hbox);

        TextArea taDisplay = new TextArea();
        taDisplay.setWrapText(true);
        pane.setCenter(taDisplay);


        primaryStage.setTitle("Exercise 17_05");
        Scene scene = new Scene(pane, 350,250);
        primaryStage.setScene(scene);
        primaryStage.show();

        btWrite.setOnAction(e -> writeData());
        btRead.setOnAction(e -> readData());

        private int[] array = {1,2,3,4,5};
        private double[] arrayDouble = {5.5};
        private Date[] dateArray = {new Date()};

        public void writeData() throws IOException{
            try(ObjectOutputStream output = new ObjectOutputStream(new FileOutputStream("Exercise15_05.dat"));
                    ){
                output.writeObject(array);
                output.writeObject(arrayDouble);
                output.writeObject(dateArray);
            }
        }

        public void readData() throws IOException{
            try(ObjectInputStream input = new ObjectInputStream(new FileInputStream("Exercise15_05.dat"));
                    ){
                int[] newInt = (int[])(input.readObject());
                double[] newDouble = (double[])(input.readObject());
                Date[] newDate = (Date[])(input.readObject());

                String existingText = taDisplay.getText();

                for(int i =0; i < newInt.length; i ++){
                    taDisplay.setText = (existingText +  " " + newInt[i]);
                }
                taDisplay.setText(existingText + "\n" + newDouble[0] + "\n" + newDate[0]);

            }
        }
    }

    public static void main(String[] args){
        Application.launch(args);
    }
}

您似乎已将 readDatawriteData 方法放入 start 方法中,以便访问 taDisplay 变量。您不想这样做 - 相反,修改您的代码,以便将您需要的所有变量作为参数传递给您的函数。

我尝试了其他方法。我认为你对我试图以错误的方式访问 TextArea 的看法是正确的。这是对我有用的代码

public class Exercise_17_5 extends Application{
    @Override 
    public void start(Stage primaryStage){
        BorderPane pane = new BorderPane();

        HBox hbox = new HBox(10);
        hbox.setAlignment(Pos.CENTER);
        Button btWrite = new Button("Write");
        Button btRead = new Button("Read");
        hbox.getChildren().addAll(btWrite, btRead);
        hbox.setPadding(new Insets(5,5,5,5));
        pane.setBottom(hbox);

        TextArea taDisplay = new TextArea();
        taDisplay.setWrapText(true);
        pane.setCenter(taDisplay);


        primaryStage.setTitle("Exercise 17_05");
        Scene scene = new Scene(pane, 350,250);
        primaryStage.setScene(scene);
        primaryStage.show();

        btWrite.setOnAction(e -> {
            try {
                writeData();
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        });

        btRead.setOnAction(e -> {
            try {
                taDisplay.setText(readData());
            } catch (Exception e1) {
                taDisplay.setText("don't be stupid. You have to write data first");
            }
        });

    }

    int[] intArray = {1,2,3,4,5};
    double[] doubleArray = {5.5};
    Date[] dateArray = {new Date()};

    private String readData() throws Exception{
        ObjectInputStream input = new ObjectInputStream(new FileInputStream("Exercise17_05.dat"));

        int[] newIntArray = (int[])(input.readObject());
        double[] newDoubleArray = (double[])(input.readObject());
        Date[] newDateArray = (Date[])(input.readObject());

        String txt = "";

        for(int i = 0; i < newIntArray.length; i++){
            txt = txt + " " + newIntArray[i];
        }

        txt = txt + "\n" + newDoubleArray[0] + "\n" + newDateArray[0];
        input.close();
        return txt;
    }

    private void writeData() throws Exception {

        ObjectOutputStream output = new ObjectOutputStream(new FileOutputStream("Exercise17_05.dat"));

        output.writeObject(intArray);
        output.writeObject(doubleArray);
        output.writeObject(dateArray);

        output.close();
    }



    public static void main(String[] args){
        Application.launch(args);
    }
}