import java.awt.*;
import java.awt.event.*;
import java.io.*;
class MenuDemo
{
private Frame f;
private MenuBar mb;
private Menu m;
private MenuItem dm,gm,bm;
private FileDialog open,save;
private TextArea ta;
private File file;
MenuDemo()
{
init();
}
public void init()
{
f=new Frame("魔兽世界");
mb=new MenuBar();
m=new Menu("文件");
dm=new MenuItem("打开");
gm=new MenuItem("退出");
bm=new MenuItem("保存");
open=new FileDialog(f,"打开WoW",FileDialog.LOAD);
save=new FileDialog(f,"关闭WoW",FileDialog.SAVE);
ta=new TextArea();
f.setBounds(300,100,600,600);
//f.setLayout(new FlowLayout());使用默认布局,没设置东南西北,就填充了
f.setMenuBar(mb);
f.add(ta);
mb.add(m);
m.add(dm);
m.add(bm);
m.add(gm);
myEvent();
f.setVisible(true);
} private void myEvent()
{
bm.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(file==null)
{
save.setVisible(true);
String s1=save.getFile();
String s2=save.getDirectory();
if (s1==null||s2==null)
{
return;
}
file=new File(s2,s1);
}
else
{
BufferedWriter bw=null;
try
{
bw=new BufferedWriter(new FileWriter(file));
String s=ta.getText();
bw.write(s);
bw.flush();
bw.close();
}
catch (IOException e1)
{
throw new RuntimeException("写入失败");
} }
}
});
dm.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
open.setVisible(true);
String filePath=open.getDirectory();
String name=open.getFile();
if (filePath==null||name==null)
{
return;
}
ta.setText("");
file=new File(filePath,name);
BufferedReader br=null;
try
{
br=new BufferedReader(new FileReader(file));
String line=null;
while ((line=br.readLine())!=null)
{
ta.append(line+"\r\n");
}
}
catch (IOException e1)
{
throw new RuntimeException("读取失败");
}
finally
{
try
{
if (br!=null)
{
br.close();
}
}
catch (IOException e1)
{
System.out.println("读取流关闭失败");
}
}
}
});
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
gm.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
}
public static void main(String[] args)
{
new MenuDemo();
}
}这个程序实现了非常简易的记事本功能,只有保存,读取,和退出功能,但是如果不打开文件,而是直接在这个程序里的文本区域里写一些东西按保存后,这个文件是没有立刻出现的,而需要再一次按保存才出现,小弟实在不知道为什么。求大神解惑。(也就是说当我直接在文本区域里写了一些数据后,需要按2次保存才能看到这个文件的出现。)
解决方案 »
- 怎么把一个单链表倒序输出
- 如何用java做一个类似于telnet的软件
- 复杂的el ${bActionForm.container.account} 表示什么含义
- 关于多文本的问题
- 有关成员变量的问题
- 请问那里可以下载到JAVA写的程序和有没有用JAVA设计的典型网站
- 程序员的悲伤-->> 谁能救救我姥姥....???
- 我的JB编译用j2sdk1.4时为什么出错?
- HELP!!!!!!
- android编程遇到的一个代码,请问double后面的...是什么意思?
- 怎么让自己的程序开机自启动
- Java中截屏 存储到BufferedImage中 除了createScreenCapture之外还有什么其他的函数么?
bm.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(file==null)
{
save.setVisible(true);
String s1=save.getFile();
String s2=save.getDirectory();
if (s1==null||s2==null)
{
return;
}
file=new File(s2,s1);
}
else
{
BufferedWriter bw=null;
try
{
bw=new BufferedWriter(new FileWriter(file));
String s=ta.getText();
bw.write(s);
bw.flush();
bw.close();
}
catch (IOException e1)
{
throw new RuntimeException("写入失败");
} }
}
});==>bm.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (file == null) {
save.setVisible(true);
String s1 = save.getFile();
String s2 = save.getDirectory();
if (s1 == null || s2 == null) {
return;
}
file = new File(s2, s1);
}
BufferedWriter bw = null;
try {
bw = new BufferedWriter(new FileWriter(file));
String s = ta.getText();
bw.write(s);
bw.flush();
bw.close();
} catch (IOException e1) {
throw new RuntimeException("写入失败");
}
}
});
if(file==null)
{
save.setVisible(true);
String s1=save.getFile();
String s2=save.getDirectory();
if (s1==null||s2==null)
{
return;
}
file=new File(s2,s1);
if(!file.exists()){
try {
file.createNewFile();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
// else
//{
BufferedWriter bw=null;
try
{
bw=new BufferedWriter(new FileWriter(file));
String s=ta.getText();
bw.write(s);
bw.flush();
bw.close();
}
catch (IOException e1)
{
throw new RuntimeException("写入失败");
} //}
bm.addActionListener 中else 关键字 去掉(包括"{}").里面的内容保留
bm.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (file == null) {
save.setVisible(true);
String s1 = save.getFile();
String s2 = save.getDirectory();
if (s1 == null || s2 == null) {
return;
}
file = new File(s2, s1);
}
BufferedWriter bw = null;
try {
bw = new BufferedWriter(new FileWriter(file));
String s = ta.getText();
bw.write(s);
bw.flush();
bw.close();
} catch (IOException e1) {
throw new RuntimeException("写入失败");
}
}
});
package menupack;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class MenuDemo
{
private Frame f;
private MenuBar mb;
private Menu m;
private MenuItem dm,gm,bm;
private FileDialog open,save;
private TextArea ta;
private File file;
MenuDemo()
{
init();
}
public void init()
{
f=new Frame("魔兽世界");
mb=new MenuBar();
m=new Menu("文件");
dm=new MenuItem("打开");
gm=new MenuItem("退出");
bm=new MenuItem("保存");
open=new FileDialog(f,"打开WoW",FileDialog.LOAD);
save=new FileDialog(f,"关闭WoW",FileDialog.SAVE);
ta=new TextArea();
f.setBounds(300,100,600,600);
//f.setLayout(new FlowLayout());使用默认布局,没设置东南西北,就填充了
f.setMenuBar(mb);
f.add(ta);
mb.add(m);
m.add(dm);
m.add(bm);
m.add(gm);
myEvent();
f.setVisible(true);
} private void myEvent()
{
/*bm.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(file==null)
{
save.setVisible(true);
String s1=save.getFile();
String s2=save.getDirectory();
if (s1==null||s2==null)
{
return;
}
file=new File(s2,s1);
}
try
{
BufferedWriter bw=new BufferedWriter(new FileWriter(file));
String s=ta.getText();
bw.write(s);
bw.flush();
bw.close();
}
catch (IOException e1)
{
throw new RuntimeException("写入失败");
}
}
});*/
bm.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (file == null) {
save.setVisible(true);
String s1 = save.getFile();
String s2 = save.getDirectory();
if (s1 == null || s2 == null) {
return;
}
file = new File(s2, s1);
}
BufferedWriter bw = null;
try {
bw = new BufferedWriter(new FileWriter(file));
String s = ta.getText();
bw.write(s);
bw.flush();
bw.close();
} catch (IOException e1) {
throw new RuntimeException("写入失败");
}
}
});
dm.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
open.setVisible(true);
String filePath=open.getDirectory();
String name=open.getFile();
if (filePath==null||name==null)
{
return;
}
ta.setText("");
file=new File(filePath,name);
BufferedReader br=null;
try
{
br=new BufferedReader(new FileReader(file));
String line=null;
while ((line=br.readLine())!=null)
{
ta.append(line+"\r\n");
}
}
catch (IOException e1)
{
throw new RuntimeException("读取失败");
}
finally
{
try
{
if (br!=null)
{
br.close();
}
}
catch (IOException e1)
{
System.out.println("读取流关闭失败");
}
}
}
});
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
gm.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
}
public static void main(String[] args)
{
new MenuDemo();
}
}
你瞧~
1) 运行
2)输入文本:abc
3)点击保存,弹出对话框,输入test.txt,点击保存按钮
4)查看test.txt文件,有内容。 在JDK_HOME\lib 下
save=new FileDialog(f,"关闭WoW",FileDialog.SAVE);
if(file==null) {
弹出文件选择对话框;
打开用户选择的文件--> file
} else {
将内容写入file中。
}
明显这个else就是错误滴啊。