主题:[讨论]请教关于"记事本"的"保存"问题!!
为什么我下面程序中的"保存"功能不用实现,请指出问题所在!!
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
class lianxi extends JFrame
{
lianxi(String Title)
{
super(Title);
LianJie();
WenBen();
}
JMenuBar jmenubar=new JMenuBar();
JMenu[] jmenu=new JMenu[]{new JMenu("文件"),new JMenu("编辑")};
JMenuItem[][] jmenuitem=new JMenuItem[][]{{new JMenuItem("新建"),new JMenuItem("打开"),new JMenuItem("保存"),new JMenuItem("退出")},
{new JMenuItem("剪切"),new JMenuItem("复制"),new JMenuItem("粘贴"),new JMenuItem("删除")}};
final JTextArea word=new JTextArea();
ActionListener action=new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String MuLu=null;
JMenuItem AnNiu=(JMenuItem)e.getSource();
String str=AnNiu.getText();
if(str.equals("新建"))
{
word.setText(" ");
}
else if(str.equals("打开"))
{
FileDialog DuiHuaKuang=new FileDialog(lianxi.this,"打开文件",FileDialog.LOAD);
DuiHuaKuang.setVisible(true);
MuLu=DuiHuaKuang.getDirectory()+DuiHuaKuang.getFile();
if(MuLu!=null)
{
try
{
FileInputStream FileInput=new FileInputStream(MuLu);
byte[] a=new byte[1000];
int len=FileInput.read(a);
word.append(new String(a,0,len));
FileInput.close();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
/*else
{
}*/
}
else if(str.equals("保存"))
{
if(MuLu!=null)
{
try
{
FileOutputStream FileOutput=new FileOutputStream(MuLu);
FileOutput.write(word.getText().getBytes());
FileOutput.close();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
}
}
};
public void LianJie()
{
int i,j;
for(i=0;i<jmenu.length;i++)
{
jmenubar.add(jmenu[i]);
for(j=0;j<jmenuitem[i].length;j++)
{
jmenu[i].add(jmenuitem[i][j]);
jmenuitem[i][j].addActionListener(action);
}
}
this.setJMenuBar(jmenubar);
}
public void WenBen()
{
JScrollPane jsp = new JScrollPane(word);
this.getContentPane().add(jsp);
}
public static void main(String[] args)
{
lianxi L=new lianxi("菜单窗口");
L.setSize(600,400);
L.setLocation(180,180);
L.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
L.setVisible(true);
}
}