使用Java GUI的输入和输出流



我不明白为什么会一直出现这个错误。我需要设计一个使用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);
    }
}

相关内容

  • 没有找到相关文章

最新更新