随笔-3  评论-1  文章-13  trackbacks-0

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;

public class jfileChooser extends JFrame implements ActionListener {
 Container con;
 JTextPane textpane;
 JButton button;
 JScrollPane scroll;
 FileInputStream filestream;
 JFileChooser choser=new JFileChooser();
 public  jfileChooser(){
  textpane=new JTextPane();
  button=new JButton("ok");
  scroll=new  JScrollPane(textpane);
  con=this.getContentPane();
  con.add(scroll,BorderLayout.CENTER);
  con.add(button,BorderLayout.SOUTH);
  button.addActionListener(this);
  this.setExtendedState(JFrame.MAXIMIZED_BOTH);
  setVisible(true);
 }

 public void actionPerformed(ActionEvent e) {
  // TODO 自动生成方法存根
  if(e.getSource()==button){
   int state= choser.showOpenDialog(null);
   File file=choser.getSelectedFile();
   if(file!=null && state==JFileChooser.APPROVE_OPTION){
    try {
     filestream=new FileInputStream(file);
    } catch (FileNotFoundException e1) {
     // TODO 自动生成 catch 块
     e1.printStackTrace();
    }
    try {
     textpane.read(filestream,this);
    } catch (IOException e1) {
     // TODO 自动生成 catch 块
     e1.printStackTrace();
    }
   }
  }

 }
 public static void main(String para[]){
  jfileChooser chosershili=new jfileChooser();
  //chosershili.pack();
 }

}

posted on 2007-07-16 12:46 lcgxc 阅读(3179) 评论(0)  编辑  收藏 所属分类: java

只有注册用户登录后才能发表评论。


网站导航: