Java——文件选择框:JFileChooser

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的更多相关文章
- Java Swing提供的文件选择对话框 - JFileChooser
JFileChooser() 构造一个指向用户默认目录的 JFileChooser. JFileChooser(File currentDirectory) 使 ...
- 在Win7下要通过某个 线程 来调用SavaDialog文件选择框的问题
如果 在Win7下要通过某个 线程 来调用SavaDialog文件选择框的代码 选择窗口 有时会出不来 需要设置如下: ThreadthreadOfRec = new Thread(Reciv ...
- IE8 下更改input[file] file文件选择框样式
1/使用绝对定位,将文件选择框固定,并且隐藏该选择框(文件选择框可调整宽高),设置该文件选择框 z-index 调高 比如 999. 2/使用任意标签,调整为与上面选择框相同宽高,目的为使用该标签样式 ...
- layui文件上传中如何先判断后再弹出文件选择框
前言:layui中的上传,如何在点击上传按钮前阻止file的默认打开文件选择框呢?我想点击后先判断下,如果判断结果不符合,就直接弹出提示框,而不是文件选择框,判断符合才进行文件选择.但是在layui的 ...
- Java文件选择对话框(文件选择器JFileChooser)的使用:以一个文件加密器为例
文件加密器,操作过程肯定涉及到文件选择器的使用,所以这里以文件加密器为例.下例为我自己写的一个文件加密器,没什么特别的加密算法,只为演示文件选择器JFileChooser的使用. 加密器界面如图: 项 ...
- js点击某个图标或按钮弹出文件选择框
<HTML> <head> <script type="text/javascript" src="script/jquery-1.6.2. ...
- 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 ...
- MFC打开文件选择框和多选框,保存文件夹的选择,保存文件路径的选择
CString defaultDir = "C:\\"; //默认打开的文件路径 CString fileName = ""; //默认打开的文件名 CStri ...
- ABAP文件选择框函数
因为WS_FILENAME_GET已经被废弃所以使用接口CL_GUI_FRONTEND_SERVICES来实现本地文件的选择. 用接口类CL_GUI_FRONTEND_SERVICES实现的方法 CA ...
随机推荐
- 导入dmp文件时的注意事项
来源于:http://bbs.csdn.net/topics/350167817 --1表空间 CREATE TABLESPACE newjw DATAFILE 'E:\oracle_data\new ...
- 【UOJ #147】【NOIP 2015】斗地主
http://uoj.ac/problem/147 搜索时先枚举三顺子,双顺子和单顺子,然后贪心带牌和成三成双成单出. #include<cstdio> #include<cstri ...
- 评价指标ROC,PR
之前实习的时候一直见公司里面的人说什么AUC, 实际AUC就是ROC曲线的面积 PR是precise和recall曲线,和ROC的区别是,当测试集中的正负样本分布变化的时候,ROC曲线能够保持不变,而 ...
- react 属性与状态 学习笔记
知识点:1.react 属性的调用 this.props.被调用的属性名 设置属性的常用方法:var props = { one: '123', two: 321}调用这个属性:<HelloWo ...
- lightoj 1370 欧拉函数
A - Bi-shoe and Phi-shoe Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & % ...
- dede使用方法----调用导航
在这里,极力推荐学习dede的朋友们观看老李的零基础织梦仿站系列课程的视频,讲的超级棒的~~ 网址链接是:http://www.dede888.com/15daylessons.html. 好了,言归 ...
- oracle 11g 新特性UTL_TCP、UTL_HTTP 和 UTL_SMTP程序包发邮件
首先,创建一个 ACL: begindbms_network_acl_admin.create_acl (acl => 'utlpkg.xml', ---创建的访问控制列 ...
- PHP热身
0.环境说明 win10 xampp phpstorm xampp带了apache和mysql以及phpmyadmin 1.配置mysql:修改默认编码 关mysql xampp/mysql/bin/ ...
- 【poj1655】Balancing Act
http://poj.org/problem?id=1655 (题目链接) 题意 给出一棵树,求树上一点使得使得删除一点后该树的最大子树最小. solution 树的重心裸题. 随意取一点作为根节点, ...
- 帝国cms制作手机网站
1.操作前,我们需要先对网站数据库进行备份. 接下来我们添加手机站的模板组.点击"模板", 选择"模板组管理"中的"导入/导出模板组",然后 ...