文件未写入,已关闭文件
File not being written to, already closed the file
我正在编写一个 GUI 程序,通过使用在单独的序列 class 中定义的迭代或递归方法来计算数字序列中的第 n 个元素。我想要它,以便当用户关闭 window 时,序列的前十个元素被写入文本文件,以及这两种方法的效率,每一行都用逗号分隔。
出于某种原因,当我关闭 window 时,文件没有被写入。我已经确保文件已关闭,所以我不确定为什么没有任何内容写入文件
图形界面
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.*;
import java.util.*;
public class recursiveGUI extends JPanel
{
int counterEfficiency;
private JFrame frame;//The frame
private JPanel panel;//The panel
private JRadioButton iterative;
private JRadioButton recursive;
private JLabel enter;
private JTextField enter2;
private JButton compute;
private JLabel result;
private JTextField result2;
private JLabel efficiency;
private JTextField efficiency2;
private ButtonGroup radioButtons;
public recursiveGUI()
{
frame=new JFrame("Project 3");
panel=new JPanel();
iterative=new JRadioButton("Iterative");
recursive=new JRadioButton("Recursive");
enter=new JLabel("Enter n");
enter2=new JTextField("");
compute=new JButton("Compute");
result=new JLabel("Results");
result2=new JTextField("");
efficiency=new JLabel("Efficiency");
efficiency2=new JTextField("");
radioButtons=new ButtonGroup();
frame.addWindowListener(new WindowAdapter(){
public void windowClosed(WindowEvent e){
try
{
PrintWriter outFile= new PrintWriter("efResults.txt");
for(int n=0;n<=10;n++)
{
String str=n+",";
Sequence.computeIterative(n);
str+=Sequence.getEfficiency();
Sequence.computeIterative(n);
str+=","+Sequence.getEfficiency();
outFile.println(str);
}
outFile.close();
}
catch (FileNotFoundException e1)
{
e1.printStackTrace();
}
}
});
compute.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
int n;
if(iterative.isSelected())
{
String input=enter2.getText();
n=Integer.parseInt(input);
result2.setText(Integer.toString(Sequence.computeIterative(n)));
efficiency2.setText(Integer.toString(Sequence.getEfficiency()));
}
else if(recursive.isSelected())
{
String input=enter2.getText();
n=Integer.parseInt(input);
result2.setText(Integer.toString(Sequence.computeRecursive(n)));
efficiency2.setText(Integer.toString(Sequence.getEfficiency()));
}
}
});
//Adding the parts together
panel.setLayout(new GridLayout(6,2));
radioButtons.add(iterative);
radioButtons.add(recursive);
panel.add(new JLabel());panel.add(iterative);
panel.add(new JLabel());panel.add(recursive);
panel.add(enter);panel.add(enter2);
panel.add(new JLabel());panel.add(compute);
panel.add(result);panel.add(result2);
panel.add(efficiency);panel.add(efficiency2);
frame.add(panel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setSize(600,300);
frame.setBackground(Color.red);
frame.setVisible(true);
}
//Main method
public static void main(String[] args)
{
recursiveGUI myGUI=new recursiveGUI();
}
}
顺序class
public class Sequence
{
static int efficiency;
public static int computeIterative(int n)
{
int result = 0;
if(n==0)
{
result=0;
}
else if(n==1)
{
result=1;
}
else
{
int first=1;
int second=0;
for(int i=2;i<=n;i++)
{
efficiency++;
result=2*second+first;
second=first;
first=result;
}
}
return result;
}
public static int computeRecursive(int n)
{
int result=0;
efficiency++;
if(n==0)
{
result=0;
}
else if(n==1)
{
result=1;
}
else
{
result=2*computeRecursive(n-1)+computeRecursive(n-2);
}
return result;
}
public static int getEfficiency()
{
int result=efficiency;
efficiency=0;
return result;
}
public static void main(String[] args)
{
computeIterative(5);
}
}
添加 frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
在收听 window 关闭事件 之前,代码变为:
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);//////add
frame.addWindowListener(new WindowAdapter(){
public void windowClosed(WindowEvent e){
...
}
因为 setDefaultCloseOperation 的默认值是 HIDE_ON_CLOSE
。有了这个,window 并没有关闭,它只是被隐藏了。
我正在编写一个 GUI 程序,通过使用在单独的序列 class 中定义的迭代或递归方法来计算数字序列中的第 n 个元素。我想要它,以便当用户关闭 window 时,序列的前十个元素被写入文本文件,以及这两种方法的效率,每一行都用逗号分隔。
出于某种原因,当我关闭 window 时,文件没有被写入。我已经确保文件已关闭,所以我不确定为什么没有任何内容写入文件
图形界面
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.*;
import java.util.*;
public class recursiveGUI extends JPanel
{
int counterEfficiency;
private JFrame frame;//The frame
private JPanel panel;//The panel
private JRadioButton iterative;
private JRadioButton recursive;
private JLabel enter;
private JTextField enter2;
private JButton compute;
private JLabel result;
private JTextField result2;
private JLabel efficiency;
private JTextField efficiency2;
private ButtonGroup radioButtons;
public recursiveGUI()
{
frame=new JFrame("Project 3");
panel=new JPanel();
iterative=new JRadioButton("Iterative");
recursive=new JRadioButton("Recursive");
enter=new JLabel("Enter n");
enter2=new JTextField("");
compute=new JButton("Compute");
result=new JLabel("Results");
result2=new JTextField("");
efficiency=new JLabel("Efficiency");
efficiency2=new JTextField("");
radioButtons=new ButtonGroup();
frame.addWindowListener(new WindowAdapter(){
public void windowClosed(WindowEvent e){
try
{
PrintWriter outFile= new PrintWriter("efResults.txt");
for(int n=0;n<=10;n++)
{
String str=n+",";
Sequence.computeIterative(n);
str+=Sequence.getEfficiency();
Sequence.computeIterative(n);
str+=","+Sequence.getEfficiency();
outFile.println(str);
}
outFile.close();
}
catch (FileNotFoundException e1)
{
e1.printStackTrace();
}
}
});
compute.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
int n;
if(iterative.isSelected())
{
String input=enter2.getText();
n=Integer.parseInt(input);
result2.setText(Integer.toString(Sequence.computeIterative(n)));
efficiency2.setText(Integer.toString(Sequence.getEfficiency()));
}
else if(recursive.isSelected())
{
String input=enter2.getText();
n=Integer.parseInt(input);
result2.setText(Integer.toString(Sequence.computeRecursive(n)));
efficiency2.setText(Integer.toString(Sequence.getEfficiency()));
}
}
});
//Adding the parts together
panel.setLayout(new GridLayout(6,2));
radioButtons.add(iterative);
radioButtons.add(recursive);
panel.add(new JLabel());panel.add(iterative);
panel.add(new JLabel());panel.add(recursive);
panel.add(enter);panel.add(enter2);
panel.add(new JLabel());panel.add(compute);
panel.add(result);panel.add(result2);
panel.add(efficiency);panel.add(efficiency2);
frame.add(panel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setSize(600,300);
frame.setBackground(Color.red);
frame.setVisible(true);
}
//Main method
public static void main(String[] args)
{
recursiveGUI myGUI=new recursiveGUI();
}
}
顺序class
public class Sequence
{
static int efficiency;
public static int computeIterative(int n)
{
int result = 0;
if(n==0)
{
result=0;
}
else if(n==1)
{
result=1;
}
else
{
int first=1;
int second=0;
for(int i=2;i<=n;i++)
{
efficiency++;
result=2*second+first;
second=first;
first=result;
}
}
return result;
}
public static int computeRecursive(int n)
{
int result=0;
efficiency++;
if(n==0)
{
result=0;
}
else if(n==1)
{
result=1;
}
else
{
result=2*computeRecursive(n-1)+computeRecursive(n-2);
}
return result;
}
public static int getEfficiency()
{
int result=efficiency;
efficiency=0;
return result;
}
public static void main(String[] args)
{
computeIterative(5);
}
}
添加 frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
在收听 window 关闭事件 之前,代码变为:
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);//////add
frame.addWindowListener(new WindowAdapter(){
public void windowClosed(WindowEvent e){
...
}
因为 setDefaultCloseOperation 的默认值是 HIDE_ON_CLOSE
。有了这个,window 并没有关闭,它只是被隐藏了。