JFileChooser

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.event.*;
import java.io.*;
import javax.swing.filechooser.*;
import javax.swing.filechooser.FileFilter;

public class JFileChooserDemo1 implements ActionListener{
	JFrame jf = null;
	JLabel label = null;
	JFileChooser fileChooser = null;
	JButton openButton,saveButton ;
	JTextArea theArea = null;
	
	public JFileChooserDemo1(){
		jf = new JFrame("JFileChooserDemo1");
		theArea = new JTextArea();
		label = new JLabel("請選擇文件");
		Container contentPane = jf.getContentPane();
		openButton = new JButton("open file");
		saveButton = new JButton("save file");
		openButton.addActionListener(this);
		saveButton.addActionListener(this);
		Box vBox = Box.createHorizontalBox();
		vBox.add(openButton);
		vBox.add(Box.createGlue());
		vBox.add(saveButton);
//		JPanel panel = new JPanel();
		contentPane.add(vBox, BorderLayout.SOUTH);
		contentPane.add(label,BorderLayout.NORTH);
		contentPane.add(theArea,BorderLayout.CENTER);
//		將openButton設置為默認按鈕,按回車即可響應
		jf.getRootPane().setDefaultButton(openButton);
		jf.setSize(300,300);
		jf.setVisible(true);
		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	
	public static void main(String...args){
		new JFileChooserDemo1();
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		File file = null;
		int result ;
		System.out.println(e.getActionCommand());
		
		if(e.getSource()==openButton){
			fileChooser = new JFileChooser(".");
			/*fileChooser.setDialogTitle("選取文件");
			fileChooser.setApproveButtonText("打開");*/
			try{
				UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
				System.out.println("Windows L&F");
			}catch(Exception ne){
				System.out.println("L&F error");
			}
	//		設置文件的選擇目錄
			fileChooser.addChoosableFileFilter(new JAVAFileFilter("java"));
			fileChooser.addChoosableFileFilter(new JAVAFileFilter("class"));
			fileChooser.setAcceptAllFileFilterUsed(false);
			
	//		設置文件的顯示視圖
			fileChooser.setFileView(new MyFileView());
			result = fileChooser.showOpenDialog(jf);
			if(result==JFileChooser.APPROVE_OPTION){
				file = fileChooser.getSelectedFile();
				label.setText("你選擇了:"+file.getName());
			}else if(result==JFileChooser.CANCEL_OPTION){
				label.setText("你沒有選擇任何文檔");
				label.setForeground(Color.red);
			}
			FileInputStream fis = null;
			if(file!=null){
				try{
					fis = new FileInputStream(file);
					
				}catch(FileNotFoundException fnde){
					label.setText("文件未找到");
					return ;
				}
				int readbyte;
				try{
					while((readbyte=fis.read())!=-1){
						theArea.append(String.valueOf((char)readbyte));
					}
				}catch(IOException ioe){
					label.setText("文件讀取發生錯誤");
				}
				finally{
					try{
						if(fis!=null){
							fis.close();
						}
					}catch(IOException ioe){
						
					}
				}
			}
		}
		if(e.getSource()==saveButton){
			fileChooser = new JFileChooser(".");
			try{
				UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
				System.out.println("Windows L&F");
			}catch(Exception ne){
				System.out.println("L&F error");
			}
			result = fileChooser.showSaveDialog(jf);
			file = null;
			String fileName;
			if(result==JFileChooser.APPROVE_OPTION){
				file = fileChooser.getSelectedFile();
				label.setText("你選擇的存儲文件名稱為:"+file.getName());
			}else if(result==JFileChooser.CANCEL_OPTION){
				label.setText("你沒有選擇文件存儲名稱");
			}
			FileOutputStream fos = null;
			if(file!=null){
				try{
					fos = new FileOutputStream(file);
				}catch(FileNotFoundException fnde){
					label.setText("File Not Found");
					return ;
				}
				String content = theArea.getText();
				try{
					fos.write(content.getBytes());
				}catch(IOException ioe){
					label.setText("文件寫入錯誤");
				}finally{
					try{
						if(fos!=null){
							fos.close();
						}
					}catch(IOException ioe){
						
					}
				}
			}
		}
	}
	
	class JAVAFileFilter extends FileFilter{
		String ext;
		public JAVAFileFilter(String ext){
			this.ext = ext;
		}
		
		@Override
		public boolean accept(File file){
			if(file.isDirectory()){
				return true;
			}
			String fileName = file.getName();
			int index = fileName.lastIndexOf('.');
			if(index>0 && index<fileName.length()-1){
				String extension = fileName.substring(index+1).toLowerCase();
				if(extension.equals(ext)){
					return true;
				}
			}
			return false;
		}
		@Override
		public String getDescription(){
			if(ext.equals("java")){
				return "JAVA Source File(*.java)";
			}
			if(ext.equals("class")){
				return "JAVA Class File(*.class)";
			}
			return "";
		}
	}
	
	class MyFileView extends FileView{
	/*	@Override
		public String getName(File file){
//			java look and feel 功能會處理掉這個項目,一般而言使用jf.getName()當返回值
			return null;
		}
		@Override
		public String getDescription(File file){
//			java look and feel 功能會處理掉這個項目,一般而言使用jf.getName()當返回值
			return null;
		}
		@Override
		public String getTypeDescription(File file){
			String extension = getExtensionName(file);
			if(extension.equals("java")){
				return "JAVA Source File";
			}
			if(extension.equals("class")){
				return "JAVA Class File";
			}
			return "";
		}*/
		@Override
		public Icon getIcon(File file){
			String extension = getExtensionName(file);
				if(extension.equals("java")){
					return new ImageIcon(".\\Icons\\home.jpg");
				}
				if(extension.equals("class")){
					return new ImageIcon(".\\Icons\\refresh.jpg");
				}
				return null;
		}
		/*@Override
		public Boolean isTraversable(File file){
//			java look and feel 功能會處理掉這個項目,一般而言使用jf.getName()當返回值
			return null;
		}*/
		
		public String getExtensionName(File file){
			String extension = "";
			String fileName = file.getName();
			int index = fileName.lastIndexOf('.');
			if(index>0 && index<fileName.length()-1){
				extension = fileName.substring(index+1).toLowerCase();
			}
			return extension;
		}
		
	}
}


參考:深入淺出Java Swing程序設計

你可能感兴趣的:(java,windows,swing,ext,sun)