import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.Scanner; import javax.imageio.stream.FileImageInputStream;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea; //=================================================
// File Name : JFileChooser_demo
//------------------------------------------------------------------------------
// Author : Common //类名:myWindowEventHandle
//属性:
//方法:
class Note implements ActionListener{ private JFrame frame = new JFrame("窗体"); //定义窗体
private JTextArea area = new JTextArea("JTextArea"); //定义文本区
private JButton open = new JButton("打开文件"); //打开文件
private JButton save = new JButton("保存文件"); //保存文件
private JLabel label = new JLabel("现在没有打开的文件"); private JPanel butpan = new JPanel(); public Note(){
this.butpan.add(open);
this.butpan.add(save);
//设置窗体中的布局管理器为BorderLayout,所有的组件水平和垂直间距为3
frame.setLayout(new BorderLayout(3,3));
frame.add(this.label,BorderLayout.NORTH);
frame.add(this.butpan,BorderLayout.SOUTH);
frame.add(this.area, BorderLayout.CENTER);
this.frame.setSize(330,180);
this.frame.setVisible(true);
this.frame.addWindowListener(new WindowAdapter(){ //加入事件监听
public void windowClosing(WindowEvent arg0) { //窗口关闭时触发,按下关闭按钮
// TODO 自动生成的方法存根
System.out.println("windowClosing-->窗口关闭");
System.exit(1);
}
});
this.open.addActionListener(this);
this.save.addActionListener(this);
// this.frame.setSize(330,180);
// this.frame.setVisible(true);
} @Override
public void actionPerformed(ActionEvent e) { //按键事件监听
// TODO 自动生成的方法存根
File file = null;
int result = 0;
JFileChooser fileChooser = new JFileChooser();
if(e.getSource() == this.open){
this.area.setText("");
fileChooser.setApproveButtonText("确定"); //定义“确定“按钮”
fileChooser.setDialogTitle("打开文件"); //定义文件选择框标题
result = fileChooser.showOpenDialog(this.frame); //显示打开对话框
if(result == JFileChooser.APPROVE_OPTION){
file = fileChooser.getSelectedFile();
this.label.setText("打开的文件名称为:"+file.getName());
}else if(result == JFileChooser.CANCEL_OPTION){
this.label.setText("没有选择任何文件");
}else{
this.label.setText("操作出现错误");
}
if(file != null){
try{
Scanner scan = new Scanner(new FileInputStream(file));//设置输入流
scan.useDelimiter("\n"); //设置换行为分隔符
while(scan.hasNext()){ //循环读取
this.area.append(scan.next()); //读取内容到文本区
this.area.append("\n"); //设置换行
}
scan.close(); //关闭
}catch(Exception ex){
this.label.setText("文件读取出错");
}
}
} if(e.getSource() == this.save){ result = fileChooser.showSaveDialog(this.frame); //显示保存文件框
if(result == JFileChooser.APPROVE_OPTION){
file = fileChooser.getSelectedFile();
this.label.setText("存储的文件名称为:"+file.getName());
}else if(result == JFileChooser.CANCEL_OPTION){
this.label.setText("没有选择任何文件");
}else{
this.label.setText("操作出现错误");
}
if(file != null){
try{
PrintStream out = new PrintStream(new FileOutputStream(file));
out.print(this.area.getText());
out.close();
}catch(Exception ex){
this.label.setText("文件保存出错");
}
}
} } } //主类
//Function : JFileChooser_demo
public class JFileChooser_demo { public static void main(String[] args) {
// TODO 自动生成的方法存根
new Note(); } }

Java——文件选择框:JFileChooser的更多相关文章

  1. Java Swing提供的文件选择对话框 - JFileChooser

    JFileChooser()           构造一个指向用户默认目录的 JFileChooser. JFileChooser(File currentDirectory)           使 ...

  2. 在Win7下要通过某个 线程 来调用SavaDialog文件选择框的问题

    如果 在Win7下要通过某个 线程 来调用SavaDialog文件选择框的代码  选择窗口 有时会出不来 需要设置如下:    ThreadthreadOfRec = new Thread(Reciv ...

  3. IE8 下更改input[file] file文件选择框样式

    1/使用绝对定位,将文件选择框固定,并且隐藏该选择框(文件选择框可调整宽高),设置该文件选择框 z-index 调高 比如 999. 2/使用任意标签,调整为与上面选择框相同宽高,目的为使用该标签样式 ...

  4. layui文件上传中如何先判断后再弹出文件选择框

    前言:layui中的上传,如何在点击上传按钮前阻止file的默认打开文件选择框呢?我想点击后先判断下,如果判断结果不符合,就直接弹出提示框,而不是文件选择框,判断符合才进行文件选择.但是在layui的 ...

  5. Java文件选择对话框(文件选择器JFileChooser)的使用:以一个文件加密器为例

    文件加密器,操作过程肯定涉及到文件选择器的使用,所以这里以文件加密器为例.下例为我自己写的一个文件加密器,没什么特别的加密算法,只为演示文件选择器JFileChooser的使用. 加密器界面如图: 项 ...

  6. js点击某个图标或按钮弹出文件选择框

    <HTML> <head> <script type="text/javascript" src="script/jquery-1.6.2. ...

  7. VBS 文件选择框,选择Excel文件

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 on error resume Next Set objDialog=CreateObject("UserAcc ...

  8. MFC打开文件选择框和多选框,保存文件夹的选择,保存文件路径的选择

    CString defaultDir = "C:\\"; //默认打开的文件路径 CString fileName = ""; //默认打开的文件名 CStri ...

  9. ABAP文件选择框函数

    因为WS_FILENAME_GET已经被废弃所以使用接口CL_GUI_FRONTEND_SERVICES来实现本地文件的选择. 用接口类CL_GUI_FRONTEND_SERVICES实现的方法 CA ...

随机推荐

  1. Java Native Method

    一.什么是java native method? "A native method is a Java method whose implementation is provided by ...

  2. svn做目录访问控制(AuthzSVNAccessFile)

    这个是Apache的配置文件 加载模块和svn的相关设置

  3. 【转】css布局居中和CSS内容居中区别和对应DIV CSS代码

    原文地址:http://www.divcss5.com/jiqiao/j771.shtml css布局居中和CSS内容居中区别和对应DIV CSS代码教程与图文代码案例篇 对于新手来说DIV CSS布 ...

  4. The resource identified by this request is only capable of generating responses with characteristics

    [转]今天在调试springMVC的时候,在将一个对象返回为json串的时候,浏览器中出现异常: The resource identified by this request is only cap ...

  5. ElasticSearch快照备份及恢复

    工作步骤: 1:建立备份快照数据挂载点,即共享文件目录(Shared Filesystem): 2:建立快照仓储repository: 3:建立snapshot快照备份: 4:恢复snapshot快照 ...

  6. 离线安装redis集群

    Step0:redis集群组件需求 Step1:离线安装ruby Step2:离线安装rubygems Step3:安装rubygems的 redis api Step4:离线安装tcl 8.6 St ...

  7. java.net.URL请求远程文件下载

    1:浏览器请求下载 public void listStockcodeUplaod(HttpServletRequest req, HttpServletResponse res) throws Ex ...

  8. js json 对象相互转换

    字符串转对象(strJSON代表json字符串)   var obj = eval(strJSON);   var obj = strJSON.parseJSON();   var obj = JSO ...

  9. MySQ中Lmax_connections的合理设置

    max_connections 是指整个mysql服务器的最大连接数max_used_connections 是指每个数据库用户的最大连接数 MySQL服务器的连接数并不是要达到最大的100%为好,还 ...

  10. WPF弹出取消确定框

    MessageBoxResult dr = MessageBox.Show("是否在"+ConfigHelper.GetAppSetting("SourceDBName& ...