자바 파일 선택 기

4904 단어
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("
"); // while(scan.hasNext()){ _textArea.append(scan.next()); _textArea.append("
"); } 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

좋은 웹페이지 즐겨찾기