蒋映辉 发表于 2012-6-2 16:34
我觉得是你没有生成对吧。。。。。。
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class NotePad
{
private Frame f;
private MenuBar mb;
private Menu fileMenu;
private MenuItem openMenuItem,saveMenuItem,saveAsMenuItem,quitMenuItem;
private TextArea ta;
private File file;
NotePad()
{
init();
}
private void init()
{
f = new Frame("NotePad v1.0");
mb = new MenuBar();
fileMenu = new Menu("File");
openMenuItem = new MenuItem("Open...");
saveMenuItem = new MenuItem("Save");
saveAsMenuItem = new MenuItem("Save as...");
quitMenuItem = new MenuItem("Quit");
ta = new TextArea("",0,0,TextArea.SCROLLBARS_VERTICAL_ONLY);
f.setBounds(400,300,480,500);
f.setMenuBar(mb);
f.add(ta);
mb.add(fileMenu);
fileMenu.add(openMenuItem);
fileMenu.add(saveMenuItem);
fileMenu.add(saveAsMenuItem);
fileMenu.add(quitMenuItem);
frameEvent();
menuEvent();
f.setVisible(true);
}
private void frameEvent()
{
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});
}
private void menuEvent()
{
saveAsMenuItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
FileDialog saveAsDialog = new FileDialog(f,"Save file as",FileDialog.SAVE);
saveAsDialog.setVisible(true);
file = new File(saveAsDialog.getDirectory()+saveAsDialog.getFile());
saveFile(file);
}
});
saveMenuItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
if (file != null)
{
saveFile(file);
}
else
{
FileDialog saveDialog = new FileDialog(f,"Save file",FileDialog.SAVE);
saveDialog.setVisible(true);
file = new File(saveDialog.getDirectory()+saveDialog.getFile());
saveFile(file);
}
}
});
quitMenuItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
System.exit(0);
}
});
openMenuItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
FileDialog openDialog = new FileDialog(f,"Open file",FileDialog.LOAD);
openDialog.setVisible(true);
try
{
if (openDialog.getDirectory() == null || openDialog.getFile() == null)
{
return;
}
ta.setText("");
file = new File(openDialog.getDirectory(),openDialog.getFile());
BufferedReader br = new BufferedReader(new FileReader(file));
String str = null;
while ((str = br.readLine()) != null)
{
ta.append(str+"\r\n");
}
br.close();
}
catch (IOException oe)
{
throw new RuntimeException("Open file failed.");
}
}
});
}
private void saveFile(File file)
{
try
{
BufferedWriter bw = new BufferedWriter(new FileWriter(file));
bw.write(ta.getText());
bw.close();
}
catch (IOException se)
{
throw new RuntimeException("Save flie failed.");
}
}
public static void main(String[] args)
{
new NotePad();
}
}
源,你用jar生成一下,看能不能用,能用发给我看看.QQ940409666 |