我有2个void一个保存带有一些数据的txt和一个加载txt并更新游戏,但是任何时候我保存它,它覆盖txt文件,我想保存不同的txt从输入读取名称txt这是一个按钮调用void save,我在哪里输入?从面板而不是从控制台`
JButton b=new JButton("Salva");
add(b);
b.addActionListener(e -> {
try {
Salva();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
});`
public void Salva () throws IOException {
timer.stop();
final JTextField xField = new JTextField(10); // here am i trying to
xField.getText(); // create a text field and put
//the strin information
xField.add(getName(), null); //to name the file
File f=new File("Namefromtxtfield.txt"); //here the name of
// txtfield
f.createNewFile();
PrintWriter c = new PrintWriter(f);
c.print(crash+" ");
c.print(collectedcoin+" ");
c.print(car.getX()+" ");
c.print(car.getY()+" ");
添加JTextField并获取写在那里的文本,也可以使用JTextField创建全新的JPanel,以便在按钮单击时打开并从那里传递值。