用JAVA编写一个GUI记事本程序,实现文本的输入,保存,修改,打开操作

急…... 急… 展开
 我来答
198901245631
推荐于2017-09-21 · TA获得超过3.5万个赞
知道大有可为答主
回答量:9037
采纳率:92%
帮助的人:1665万
展开全部

代码如下:

import java.io.*;
import java.awt.*;
import java.awt.event.*;

public class jtxtfm{
public static void main(String args[]){
jtxtfrm fm=new jtxtfrm();
}
}
class jtxtfrm extends Frame implements ActionListener{
FileDialog op,sv;
Button btn1,btn2,btn3;
TextArea tarea;
jtxtfrm(){
super("读写文件");
setLayout(null);
setBackground(Color.cyan);
setSize(600,300);
setVisible(true);
btn1=new Button("打开");
btn2=new Button("保存");
btn3=new Button("关闭");
tarea=new TextArea("");
add(btn1);add(btn2);add(btn3);add(tarea);
tarea.setBounds(30,50,460,220);
btn1.setBounds(520,60,50,30);
btn2.setBounds(520,120,50,30);
btn3.setBounds(520,180,50,30);
op=new FileDialog(this,"打开",FileDialog.LOAD);
sv=new FileDialog(this,"保存",FileDialog.SAVE);
btn1.addActionListener(this);
btn2.addActionListener(this);
btn3.addActionListener(this);
    addWindowListener(
     new WindowAdapter(){
      public void windowClosing(WindowEvent e){
       setVisible(false);
       System.exit(0);
      }
     }    
    );
}

public void actionPerformed(ActionEvent e){
if(e.getSource()==btn1){
String str;
op.setVisible(true);
try{
File f1=new File(op.getDirectory(),op.getFile());
FileReader fr=new FileReader(f1);
BufferedReader br=new BufferedReader(fr);
tarea.setText("");
while((str=br.readLine())!=null)tarea.append(str+'\n');
fr.close();
}
catch(Exception e1)
{}
}

if(e.getSource()==btn2){
sv.setVisible(true);
try{
File f1=new File(sv.getDirectory(),sv.getFile());
FileWriter fw=new FileWriter(f1);
BufferedWriter bw=new BufferedWriter(fw);
String gt=tarea.getText();
bw.write(gt,0,gt.length());
bw.flush();
fw.close();
}
catch ( Exception e2)
{}
}

if(e.getSource()==btn3){
System.exit(0);
}

}
}

效果图:

kevinwg
推荐于2017-09-21 · TA获得超过207个赞
知道答主
回答量:119
采纳率:100%
帮助的人:65.2万
展开全部
保存时文件名如file.txt 文件格式也要输入
!!!注意该程序只能打开txt文件、java源程序文件、html文件等,
Word的doc文件是特殊格式文件不能用本程序打开。

import java.io.*;
import java.awt.*;
import java.awt.event.*;

public class jtxtfm{
public static void main(String args[]){
jtxtfrm fm=new jtxtfrm();
}
}

class jtxtfrm extends Frame implements ActionListener{
FileDialog op,sv;
Button btn1,btn2,btn3;
TextArea tarea;
jtxtfrm(){
super("读写文件");
setLayout(null);
setBackground(Color.cyan);
setSize(600,300);
setVisible(true);
btn1=new Button("打开");
btn2=new Button("保存");
btn3=new Button("关闭");
tarea=new TextArea("");
add(btn1);add(btn2);add(btn3);add(tarea);
tarea.setBounds(30,50,460,220);
btn1.setBounds(520,60,50,30);
btn2.setBounds(520,120,50,30);
btn3.setBounds(520,180,50,30);
op=new FileDialog(this,"打开",FileDialog.LOAD);
sv=new FileDialog(this,"保存",FileDialog.SAVE);
btn1.addActionListener(this);
btn2.addActionListener(this);
btn3.addActionListener(this);
addWindowListener(
new WindowAdapter(){
public void windowClosing(WindowEvent e){
setVisible(false);
System.exit(0);
}
}
);
}

public void actionPerformed(ActionEvent e){
if(e.getSource()==btn1){
String str;
op.setVisible(true);
try{
File f1=new File(op.getDirectory(),op.getFile());
FileReader fr=new FileReader(f1);
BufferedReader br=new BufferedReader(fr);
tarea.setText("");
while((str=br.readLine())!=null)tarea.append(str+'\n');
fr.close();
}
catch(Exception e1)
{}
}

if(e.getSource()==btn2){
sv.setVisible(true);
try{
File f1=new File(sv.getDirectory(),sv.getFile());
FileWriter fw=new FileWriter(f1);
BufferedWriter bw=new BufferedWriter(fw);
String gt=tarea.getText();
bw.write(gt,0,gt.length());
bw.flush();
fw.close();
}
catch ( Exception e2)
{}
}

if(e.getSource()==btn3){
System.exit(0);
}

}
}
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
497708594
2009-11-20
知道答主
回答量:24
采纳率:0%
帮助的人:2.5万
展开全部
jhgjhghghghghghg
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式