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();
}
}
做了个读写文件的小东西,功能很烂,有时间做个完整的记事本吧。
本文转自Phinecos(洞庭散人)博客园博客,原文链接:http://www.cnblogs.com/phinecos/archive/2006/06/08/420130.html,如需转载请自行联系原作者
微信关注我们
原文链接:https://yq.aliyun.com/articles/343694
转载内容版权归作者及来源网站所有!
低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
相关文章
发表评论
资源下载
更多资源优质分享App
近一个月的开发和优化,本站点的第一个app全新上线。该app采用极致压缩,本体才4.36MB。系统里面做了大量数据访问、缓存优化。方便用户在手机上查看文章。后续会推出HarmonyOS的适配版本。
Apache Tomcat
Tomcat是Apache 软件基金会(Apache Software Foundation)的Jakarta 项目中的一个核心项目,由Apache、Sun 和其他一些公司及个人共同开发而成。因为Tomcat 技术先进、性能稳定,而且免费,因而深受Java 爱好者的喜爱并得到了部分软件开发商的认可,成为目前比较流行的Web 应用服务器。
Eclipse
Eclipse 是一个开放源代码的、基于Java的可扩展开发平台。就其本身而言,它只是一个框架和一组服务,用于通过插件组件构建开发环境。幸运的是,Eclipse 附带了一个标准的插件集,包括Java开发工具(Java Development Kit,JDK)。
JDK
JDK是 Java 语言的软件开发工具包,主要用于移动设备、嵌入式设备上的java应用程序。JDK是整个java开发的核心,它包含了JAVA的运行环境(JVM+Java系统类库)和JAVA工具。