各位堆垛机 friend 大家好!我在为我的游戏创建正确的保存功能时遇到问题。我遇到的主要问题是捕获名为 display 的 J 文本区域内的所有数据。显示屏反射(reflect)了游戏中迄今为止已采取的 Action ,显示屏顶部写着“欢迎来到 Togiz Kumalak”。
我的问题是,当我保存游戏时,它只将第一行文本记录到文件中,而不是所有行。
一个例子是:
Welcome to Togiz Kumalak
Player 1 Cup 1
Player 2 Cup 3
等等。当我保存文件时,只会显示顶行。
这是我当前的保存监听器代码。
class SaveListener implements ActionListener
{
String a = display.getText();
JFileChooser myChooser= new JFileChooser();
public void actionPerformed(ActionEvent event)
{
String fileName;
myChooser.setCurrentDirectory(new File("."));
myChooser.showSaveDialog(null);
fileName = myChooser.getSelectedFile().getPath();
try
{
OutputStream file = new FileOutputStream(fileName);
PrintStream printStream = new PrintStream(file);
printStream.print(a);
printStream.close();
/* Version 11, 23.1. Scroll pane and text area support when saving. */
}
catch(IOException e)
{
System.out.println("Problem making or writing to an output stream.");
System.out.println(e);
}
}
}
最佳答案
使用 JTextArea API 的 write(...)
方法,而不是尝试创建您自己的方法。
关于java - 如何将 JtextArea 中的所有文本输出到保存文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23379940/