《Java编程思想》读书笔记(10)

package com.vitamin.UI;
import javax.swing.
*;
import javax.swing.filechooser.FileFilter;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.
event.*;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import com.vitamin.Console.
*;

public class FileTest extends JFrame
{
    
private JButton btnOpen = new JButton("Open");
    
private JButton btnSave = new JButton("Save");
    
private JPanel p = null;
    
private JTextField tfDir = new JTextField();
    
private JTextField tfFileName = new JTextField();
    
private JScrollPane sp = new JScrollPane();
    
private JEditorPane ep = new JEditorPane();
    
private File file = null;
    
    
public FileTest() 
    
{
        super();
        
// TODO Auto-generated constructor stub
        this.initForm();
    }

    
    
public void initForm()
    
{
        Container con 
= this.getContentPane();
        con.setLayout(
new BorderLayout());
        
this.tfDir.setEditable(false);
        
this.tfFileName.setEditable(false);
        
this.btnSave.setEnabled(false);
        
        
this.btnOpen.addActionListener(new ActionListener(){
             
public void actionPerformed(ActionEvent e)
             
{
                 JFileChooser fc 
= new JFileChooser();
                 fc.setFileFilter(
new TextFileFilter() );
                
int returnVal =  fc.showOpenDialog(FileTest.this);
                
if(returnVal == JFileChooser.APPROVE_OPTION)
                
{
                    file 
= fc.getSelectedFile();
                    
try
                    
{
                        ep.setPage(file.toURL());
                        tfFileName.setText(file.getAbsolutePath());
                        btnSave.setEnabled(
true);
                    }

                    
catch(IOException ex)
                    
{
                         
throw new RuntimeException(ex);
                    }

                    
                }

                
else if(returnVal == JFileChooser.CANCEL_OPTION)
                
{
                    tfFileName.setText(
"你已经取消了文件的选择");
                }

             }
        
        }
);
        
        
this.btnSave.addActionListener(new ActionListener(){
               
public void actionPerformed(ActionEvent e)
               
{
                     
try {
                          FileWriter fw 
= new FileWriter(file);
                          ep.write(fw);
                          JOptionPane.showMessageDialog(FileTest.
this,"保存成功");
                        }
 catch (IOException ioe) {
                          
throw new RuntimeException(ioe);
                        }

               }

        }
);
        sp.getViewport().add(ep);
        
        p 
= new JPanel();
        p.add(
this.btnOpen);
        p.add(
this.btnSave);
        con.add(p,BorderLayout.SOUTH);        
        con.add(sp,BorderLayout.CENTER);
        p 
= new JPanel();
        p.setLayout(
new GridLayout(2,1));
        p.add(
this.tfDir);
        p.add(
this.tfFileName);
        con.add(p,BorderLayout.NORTH);
        console.run(
this,400,400);    
    }

      
public class TextFileFilter extends
        javax.swing.filechooser.FileFilter 
{
        
public boolean accept(File f) {
          
return f.getName().endsWith(".txt")
            
|| f.isDirectory();
        }

        
public String getDescription() {
          
return "Text Files (*.txt)";
        }

      }

    
/**
     * @param args
     
*/

    
public static void main(String[] args) 
    
{
        FileTest ft 
= new FileTest();

    }


}


做了个读写文件的小东西,功能很烂,有时间做个完整的记事本吧。

posted on   Phinecos(洞庭散人)  阅读(337)  评论(0编辑  收藏  举报

编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述

导航

统计

点击右上角即可分享
微信分享提示