我需要创建一个JavaFX项目,创建一个gui来输入名称和密码。我创建了窗格,并设置了舞台。项目显示正确,但我无法点击按钮获取信息写入文件。它会创建文件但不会存储数据。我需要能够使用多个名称和密码,因此我可以创建一个读者登录程序来检查.txt文件。这是我的代码。 谢谢。
import javax.swing.JOptionPane;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.geometry.HPos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
import java.io.*;
public class AddUser extends Application {
private TextField tfUsername = new TextField();
private TextField tfPassword = new TextField();
private Button btAddUser = new Button("Add User");
private Button btClear = new Button("Clear");
@Override // Override the start method in the Application class
public void start(Stage primaryStage) {
// Create UI
GridPane gridPane = new GridPane();
gridPane.setHgap(5);
gridPane.setVgap(5);
gridPane.add(new Label("Username:"), 0, 0);
gridPane.add(tfUsername, 1, 0);
gridPane.add(new Label("Password:"), 0, 1);
gridPane.add(tfPassword, 1, 1);
gridPane.add(btAddUser, 1, 3);
gridPane.add(btClear, 1, 3);
// Set properties for UI
gridPane.setAlignment(Pos.CENTER);
tfUsername.setAlignment(Pos.BOTTOM_RIGHT);
tfPassword.setAlignment(Pos.BOTTOM_RIGHT);
GridPane.setHalignment(btAddUser, HPos.LEFT);
GridPane.setHalignment(btClear, HPos.RIGHT);
// Process events
btAddUser.setOnAction(e -> writeNewUser());
btClear.setOnAction(e -> {
tfUsername.clear();
tfPassword.clear();
});
// Create a scene and place it in the stage
Scene scene = new Scene(gridPane, 300, 150);
primaryStage.setTitle("Add User"); // Set title
primaryStage.setScene(scene); // Place the scene in the stage
primaryStage.show(); // Display the stage
}
public void writeNewUser() {
PrintWriter fw = null;
try {
fw = new PrintWriter("users.txt");
BufferedWriter bw = new BufferedWriter(fw);
bw.write(tfUsername.getText());
bw.newLine();
bw.write(tfPassword.getText());
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* The main method is only needed for the IDE with limited
* JavaFX support. Not needed for running from the command line.
*/
public static void main(String[] args) {
launch(args);
}
}
答案 0 :(得分:4)
您是“原创的”在fw.close
方法的catch
块的try-catch
块中有writeNewUser
方法......
public void writeNewUser() {
PrintWriter fw = null;
try {
fw = new PrintWriter("users.txt");
BufferedWriter bw = new BufferedWriter(fw);
bw.write(tfUsername.getText());
bw.newLine();
bw.write(tfPassword.getText());
} catch (IOException e) {
e.printStackTrace();
fw.close();
}
}
正如已经指出的那样,只有在出现问题时才会调用此方法。此外,PrintWriter#close
会引发IOException
而fw
可能会null
,从而导致更多问题......
修改后的代码只是稍微好一些......
public void writeNewUser() {
PrintWriter fw = null;
try {
//....
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
将close
电话放在try
部分内意味着只有一切顺利才能调用它。如果由于某种原因发生异常,则永远不会关闭资源
您还应该关闭外部资源,在这种情况下为BufferedWriter
,这会将对每个封闭资源的近距离调用链接起来。
相反,您应该使用try-with-resources
,它会自动关闭资源,例如......
public void writeNewUser() {
try (BufferedWriter bw = new BufferedWriter(new PrintWriter("users.txt"))) {
bw.write(tfUsername.getText());
bw.newLine();
bw.write(tfPassword.getText());
} catch (IOException e) {
e.printStackTrace();
}
}
请查看The try-with-resources Statement了解详情
<强>更新强>
要附加到该文件,您可能需要使用FileWriter
代替,例如......
public void writeNewUser() {
try (BufferedWriter bw = new BufferedWriter(new FileWriter("users.txt", true))) {
bw.write(tfUsername.getText());
bw.newLine();
bw.write(tfPassword.getText());
bw.newLine();
} catch (IOException e) {
e.printStackTrace();
}
}
答案 1 :(得分:0)
问题出在writeNewUser方法中:
public void writeNewUser() {
PrintWriter fw = null;
try {
fw = new PrintWriter("users.txt");
BufferedWriter bw = new BufferedWriter(fw);
bw.write(tfUsername.getText());
bw.newLine();
bw.write(tfPassword.getText());
} catch (IOException e) {
e.printStackTrace();
fw.close();
}
}
fw.close()在catch循环中,但问题是它只在出现错误时关闭(这就是为什么有一个catch块)
解决此问题:
public void writeNewUser() {
PrintWriter fw = null;
try {
fw = new PrintWriter("users.txt");
BufferedWriter bw = new BufferedWriter(fw);
bw.write(tfUsername.getText());
bw.newLine();
bw.write(tfPassword.getText());
fw.close();
} catch (IOException e) {
e.printStackTrace();
fw.close
}
}
^^将fw.close()放在try和catch块中。
至于使其可重复,请尝试在初始化FileWriter之前将密码和用户名(使用scanner类)保存到数组中,然后使用for
循环将数组重写到文件中。
希望这会有所帮助: 经典