java可视化打开文件
来源:互联网 发布:从零开始学淘宝美工 编辑:程序博客网 时间:2024/06/03 23:05
import java.awt.*;import java.awt.event.*;import java.io.*;public class Data extends WindowAdapter implements ActionListener,TextListener{ Frame f; TextArea ta1; //多行文本区 Panel p1; TextField tf1; //文本框 Button b1,b2,b3; FileDialog fd; //文件对话框 File file1 = null; //文件对象初始化为空值 public static void main(String args[]){ //主方法调用自定义的display()方法 (new Data()).display(); } public void display(){ //display()方法 //设置界面与监听 f = new Frame("EditFile"); f.setSize(680,400); f.setLocation(200,140); f.setBackground(Color.lightGray); f.addWindowListener(this); tf1 = new TextField(); //文本框 tf1.setEnabled(false); //设置文本行的初始字体 tf1.setFont(new Font("Dialog",0,20)); f.add(tf1,"North"); ta1 = new TextArea(); //多行文本区 //设置文本区的初始字体 ta1.setFont(new Font("Dialog",0,20)); f.add(ta1); //注册文本区的事件监听程序 ta1.addTextListener(this); p1 = new Panel(); //面板与布局 p1.setLayout(new FlowLayout(FlowLayout.LEFT)); b1 = new Button("打开"); b2 = new Button("保存"); b3 = new Button("另存为"); p1.add(b1); //三个按钮挂上面板 p1.add(b2); p1.add(b3); b2.setEnabled(false); b3.setEnabled(false); //注册按钮的事件监听程序 b1.addActionListener(this); b2.addActionListener(this); b3.addActionListener(this); f.add(p1,"South");//面板贴上窗体 f.setVisible(true); }//display()方法结束 //实现TextListener接口中的方法, //当对多行对文本区的内容编辑时触发 public void textValueChanged(TextEvent e){ // "保存"和"另存为"两个按钮点亮 b2.setEnabled(true); b3.setEnabled(true); } //响应单击按钮的高级事件 public void actionPerformed(ActionEvent e){ long start = System.currentTimeMillis();//开始时间 if (e.getSource()==b1){ //单击[打开]按钮时 fd = new FileDialog(f,"Open",FileDialog.LOAD); fd.setVisible(true); //创建并显示打开文件对话框 if ((fd.getDirectory()!=null) && (fd.getFile()!=null)){ //单行文本框显示文件路径名 tf1.setText(fd.getDirectory()+fd.getFile()); try { //以缓冲区方式读取文件内容 file1 = new File(fd.getDirectory(),fd.getFile());//文件对象赋值 //文件读入通道连向文件对象 BufferedInputStream fr = new BufferedInputStream( new FileInputStream(file1));; BufferedReader br = new BufferedReader(new InputStreamReader( fr), 10 * 1024 * 1024);//定义文件缓冲区 String aline; //按行读取文本,每行附加在多行文本区之后 while ((aline=br.readLine()) != null){ ta1.append(aline+"\r\n"); } fr.close(); br.close(); //关闭文件缓冲区 } catch (IOException ioe){ System.out.println(ioe); } } //结束if文件不为空 } //结束if单击[打开]按钮 long end = System.currentTimeMillis();//结束时间 ta1.append("总共耗时:"+(end - start)+"ms"); // 输入输出异常捕获 //单击[Save]或[SaveAs]按钮时 if ((e.getSource()==b2) || (e.getSource()==b3)){ //单击[SaveAs]按钮时, //或单击[Save]按钮且文件对象为空时 if ((e.getSource()==b3) || (e.getSource()==b2)&&(file1==null)){ //文件对话框对象,保存方式 fd = new FileDialog(f,"Save",FileDialog.SAVE); if (file1==null) fd.setFile("Edit1.txt"); //缺省文件名 else fd.setFile(file1.getName()); fd.setVisible(true); //创建并显示保存文件对话框 if ((fd.getDirectory()!=null) && (fd.getFile()!=null)){ //单行文本框中显示文件路径和名称 tf1.setText(fd.getDirectory()+fd.getFile()); //文件对象的赋值 file1 = new File(fd.getDirectory(),fd.getFile()); save(file1); //调用自定义的save方法 } } else{ //文件对象不为空时 save(file1); } } }//响应单击按钮的高级事件结束 //自定义的save方法,参数为文件对象 public void save(File file1){ try { //将文本区内容写入字符输出流 //文件写入通道连向文件对象 FileWriter fw = new FileWriter(file1); fw.write(ta1.getText());//写入多行文本框的内容 fw.close(); //关闭通道 b2.setEnabled(false); //点灭保存按钮 b3.setEnabled(false); } catch (IOException ioe){ //异常处理 System.out.println(ioe); } } //响应关闭窗口事件 public void windowClosing(WindowEvent e){ System.exit(0); }}
0 0
- java可视化打开文件
- android java 打开可视化组件编辑器
- java打开PPT文件
- Java 打开pdf文件
- JAVA打开网址-文件
- java十六进制打开文件
- java 打开文件
- java 打开文件
- Eclipse打开java文件
- Java打开exe文件
- JAVA打开PDF文件
- java打开文件对话框
- java打开文件
- JAVA 可视化
- java文件打开保存代码
- java 直接打开pdf文件
- java 直接打开pdf文件
- java 直接打开pdf文件
- Leetcode 54 spiral matrix II (螺旋矩阵)
- Android 之在Activity中fragment相互通信
- 为什么DCDC的效率一般情况下要比LDO高
- WPF教程(二十四)Canvas
- Google原生下拉刷新组件SwipeRefreshLayout
- java可视化打开文件
- 反射调用
- linux内核源码总览之四---------kernel目录
- 关于nginx+tomcat搭建反向代理时路径和端口问题
- js-随机产生一个由10个元素组成的数组
- MFC点击对话框的X按钮使其最小化
- 总结:项管成本管理计算公式
- 自定义菜单(IMOOC高级篇)
- UCOSII移植笔记(UCOSII+STM32F103+LED)