我尝试编写的代码有一个 jTextField 供用户输入字符串。然后,用户将单击 jButton 以创建文件。对于文件的创建,我想到的是:获取用户输入的字符串,并将其附加到文件创建方法中的 .txt 中。下面的代码显示了这次不成功的尝试:
String input;
Scanner scan = new Scanner(System.in);
input = scan; //This is totally wrong, but I'm not sure how to convert the scanner
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
final Formatter x;
try{
x = new Formatter(input + ".txt");
} catch(Exception e) {
System.out.println("Your code is bad. You should feel bad.");
}
}
private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {
input = jTextField1.getText();
}
最佳答案
没关系,我找到了适合我的情况的最佳解决方案。不过谢谢!
final Formatter x;
try{
x = new Formatter(jTextField1.getText() + ".txt");
} catch(Exception e) {
System.out.println("Something went horribly wrong..");
}
}
关于java - 如何允许用户在 Java 中创建他们选择的新 .txt 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21671003/