关于记事本程序的重复保存问题
Q:运行后,打开一个文档,再修改,按新建时,不会提示保存。
因为全局变量num在打开时已置1,求教怎么修改,是不是有一个函数能将jta中的内容与原文件内容比较?
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import javax.swing.*;
public class J2009 extends JFrame {
JFileChooser fc=new JFileChooser();
File file=null;
JTextArea jta;
Color color = Color.lightGray;
String s="2009- -Java程序设计综合实验";
int num=0;
public static void centerWindow(Window f){
//获得桌面大小
Toolkit tk=f.getToolkit();
Dimension dm=tk.getScreenSize();
f.setLocation((int)(dm.getWidth()-f.getWidth())/2,(int)(dm.getHeight()-f.getHeight())/2);
}
void saving(){
try{
FileWriter Writef=new FileWriter(file);
Writef.write(jta.getText());
Writef.close();
num=1;
}
catch(Exception e){e.printStackTrace();}
}
void judging(){
if(num==0){
if(!(jta.getText().equals(""))){
int option= JOptionPane.showConfirmDialog(
J2009.this, "是否要保存 ", "2009- -Java程序设计综合实验 ",JOptionPane.YES_NO_CANCEL_OPTION);
if(option==JOptionPane.YES_OPTION)
{
if(file!=null) fc.setSelectedFile(file);
int returnVal=fc.showSaveDialog(J2009.this);
if(returnVal==JFileChooser.APPROVE_OPTION){
file=fc.getSelectedFile();
saving();}
}
else if(option==JOptionPane.NO_OPTION)
{jta.setText("");}
}
}
else jta.setText("");
}
public J2009(){
this.setTitle(s);//标题未设定
jta=new JTextArea();
this.add(jta);
JMenuBar mb=new JMenuBar();
JMenu fileMenu=new JMenu("文件(F)");
fileMenu.setMnemonic(KeyEvent.VK_F);
JMenuItem newMenuItem=new JMenuItem("新建(N)");
newMenuItem.setMnemonic(KeyEvent.VK_N);
newMenuItem.addActionListener(new NewActionListener());
fileMenu.add(newMenuItem);
JMenuItem openMenuItem=new JMenuItem("打开(O)");
openMenuItem.setMnemonic(KeyEvent.VK_O);
openMenuItem.addActionListener(new OpenActionListener());
fileMenu.add(openMenuItem);
JMenuItem saveMenuItem=new JMenuItem("保存(S)");
saveMenuItem.setMnemonic(KeyEvent.VK_S);
saveMenuItem.addActionListener(new SaveActionListener());
fileMenu.add(saveMenuItem);
JMenuItem fontMenuItem=new JMenuItem("字体");
fileMenu.add(fontMenuItem);//字体未设定
JMenuItem colorMenuItem=new JMenuItem("背景颜色(C)");
colorMenuItem.setMnemonic(KeyEvent.VK_C);
colorMenuItem.addActionListener(new colorActionListener());
fileMenu.add(colorMenuItem);
JMenuItem exitMenuItem=new JMenuItem("退出(E)");
exitMenuItem.setMnemonic(KeyEvent.VK_E);
exitMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E ,ActionEvent.CTRL_MASK));
exitMenuItem.addActionListener(new ExitActionListener());
fileMenu.add(exitMenuItem);
JMenu menu2=new JMenu("Java上机题目");
JMenuItem item1=new JMenuItem("回文数");