文件查看

作者在 2006-08-08 05:16:00 发布以下内容
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
public class MyFileChooser
{
 public static void main(String [] args)
 {
  FileFrame f=new FileFrame();
  f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  f.setVisible(true);
  f.setResizable(true);
 }
}
class FileFrame extends JFrame
{
 public FileFrame()
 {
  text=new JTextArea();
  text.setEditable(false);
  text.setWrapStyleWord(true);
  text.setLineWrap(true);
  scrollPane=new JScrollPane(text);
  setTitle("file");
  setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
  Container con=getContentPane();
  con.add(scrollPane);
  chooser=new JFileChooser();
  chooser.setCurrentDirectory(new File("."));
  JMenuBar menubar=new JMenuBar();
  setJMenuBar(menubar);
  JMenu menu=new JMenu("file");
  menubar.add(menu);
  JMenuItem open=new JMenuItem("Open");
  menu.add(open);
  JMenuItem exit=new JMenuItem("Exit");
  menu.add(exit);
  open.addActionListener(new
   ActionListener()
   {
    public void actionPerformed(ActionEvent e)
    {
     int r=chooser.showOpenDialog(null);
     if(r==JFileChooser.APPROVE_OPTION)
     {
      String name=chooser.getSelectedFile().getPath();
      try
      {
       infile=new BufferedReader(new FileReader(name));
       text.setText("");
       String line;
       while((line=infile.readLine())!=null)
       {
        text.append(line+"\n");
       }
      }catch(Exception e1){}
     }
    }
   });
  exit.addActionListener(new
   ActionListener()
   {
    public void actionPerformed(ActionEvent e)
    {
     System.exit(0);
    }
   });
 }
 private JTextArea text;
 private JScrollPane scrollPane;
 private BufferedReader infile;
 private JFileChooser chooser;
 private static final int DEFAULT_WIDTH=1000;
 private static final int DEFAULT_HEIGHT=600;
}
默认分类 | 阅读 1563 次
文章评论,共0条
游客请输入验证码
文章分类
最新评论