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(" "); //设置换行为分隔符 while(scan.hasNext()){ //循环读取 this.area.append(scan.next()); //读取内容到文本区 this.area.append(" "); //设置换行 } 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(); } }