java 文件选择器

MyFrame

package 文件选择器;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.WindowConstants;

public class MyFrame extends JFrame {
    public MyFrame(){
        super();
    }
    
    public MyFrame (String title){
        super (title);
        this.addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent e) {
                System.exit(1);
            }
        });
    }
    
    /**重写setSize方法*/
    public void setSize(int width,int height){
        super.setSize(width,height);
//      super.setResizable(false);
        
        int screenW = (int)this.getToolkit().getScreenSize().getWidth(); 
        int screenH = (int)this.getToolkit().getScreenSize().getHeight();
        this.setLocation((screenW-width)/2,(screenH-height)/2);
    }
}

Note

package 文件选择器;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.Scanner;

public class Note implements ActionListener{
    private MyFrame _myFrame;
    private JLabel _label;
    private JButton _openBtn;
    private JButton _saveBtn;
    private JTextArea _textArea;
    
    public Note(){
        _myFrame = new MyFrame("文件选择框");
        _myFrame.setSize(200,200);
        _myFrame.setLayout(new BorderLayout());
        //初始化组件
        this.initComponents();
        _myFrame.setVisible(true);
    }
    
    private void initComponents(){
        JPanel labelP = new JPanel();
        _label = new JLabel ("当前没有打开任何文件");
        labelP.add(_label);
        _myFrame.add(labelP,BorderLayout.NORTH);
        
        JPanel contentP = new JPanel ();
        _textArea = new JTextArea();
        _textArea.setLineWrap(true);
        _myFrame.add(new JScrollPane(_textArea),BorderLayout.CENTER);
        
        JPanel btnP = new JPanel(new GridLayout(1,2));
        _openBtn = new JButton("打开");
        _saveBtn = new JButton("保存");
        _openBtn.addActionListener(this);
        _saveBtn.addActionListener(this);
        btnP.add(_openBtn);
        btnP.add(_saveBtn);
        _myFrame.add(btnP,BorderLayout.SOUTH);
        
    }
    

    /**ActionListener接口方法*/
    public void actionPerformed(ActionEvent e) {
        File file = null;
        JFileChooser fileChooser = new JFileChooser();//打开根目录(默认是用户)
        if (e.getSource()==_openBtn){ //点击了打开按钮
            fileChooser.setDialogTitle("打开文件");
            fileChooser.setApproveButtonText("确定");
            int result = fileChooser.showOpenDialog(_myFrame);
            if (result == JFileChooser.APPROVE_OPTION){ //点击了确定按钮
                file = fileChooser.getSelectedFile();
                _label.setText("打开的文件为:"+file.getName());
            }else if (result == JFileChooser.CANCEL_OPTION){ //点击了取消按钮
                _label.setText("没有选择任何文件");
            }else{
                _label.setText("操作出现错误");
            }
            
            if (file!=null){
                try {
                    Scanner scan = new Scanner(new FileInputStream(file));
                    scan.useDelimiter("\n");    //设置换行分隔符
                    while(scan.hasNext()){
                        _textArea.append(scan.next());
                        _textArea.append("\n");
                    }
                    scan.close();
                }catch (Exception ex){
                    _label.setText("文件读取出错");
                }
            }
        }else{  //点击了保存按钮
            fileChooser.setDialogTitle("保存文件");
            fileChooser.setApproveButtonText("确定");
            int result = fileChooser.showSaveDialog(_myFrame);
            if (result == JFileChooser.APPROVE_OPTION){ //点击了确定按钮
                file = fileChooser.getSelectedFile();
                _label.setText("选择存储的文件为:"+file.getName());
            }else if (result == JFileChooser.CANCEL_OPTION){ //点击了取消按钮
                _label.setText("没有选择任何文件");
            }else{
                _label.setText("操作出现错误");
            }
            
            if (file!=null){
                try {
                    PrintStream out = new PrintStream(new FileOutputStream(file));
                    out.print(_textArea.getText());
                    out.close();
                }catch (Exception ex){
                    _label.setText("保存文件失败");
                }
            }
        }
    }
    
    public static void main(String[] args) {
        new Note();
        /*获取根目录
        File[] roots = File.listRoots();
        for (File drive : roots) {
           System.out.println(drive.getPath());
          }
        */
    }
}

tmp.gif

你可能感兴趣的:(java 文件选择器)