我无法弄清楚为什么我一直收到这个错误。我需要设计一个程序,使用带有按钮的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);
}
}
答案 0 :(得分:0)
您似乎已将readData
和writeData
方法放在start
方法中,以便能够访问您的taDisplay
变量。您不想这样做 - 而是修改您的代码,以便将您需要的所有变量作为参数传递给您的函数。
答案 1 :(得分:0)
我尝试了别的东西。我认为你试图以错误的方式访问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);
}
}