注册 登录
编程论坛 JAVA论坛

如何让我读出的txt文件显示在JTextArea中

ch。浩 发布于 2016-06-29 19:23, 1629 次点击
package com.jiemian;

import java.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import
import
import

import

import javax.swing.JButton;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;

public class Jilu extends JFrame implements ActionListener{
    JTextArea f;
    JButton r1,r2,r3;
    //private Component frame;
    public Jilu(){
        JFrame j=new JFrame();
        
        j.setLayout(null);
        

        
        f =new JTextArea();
        r1 = new JButton("导出文件");
        r2 = new JButton("清除");
        r3 = new JButton("退出");
        j.add(f);
        f.setBounds(20, 30, 280, 120);
        j.add(r1);
        r1.setBounds(20, 160, 90, 30);
        j.add(r2);
        r2.setBounds(170, 160, 60, 30);
        j.add(r3);
        r3.setBounds(240, 160, 60, 30);
        r1.addActionListener(this);
        r2.addActionListener(this);
        r3.addActionListener(this);
        j.setTitle("聊天记录");
        j.setSize(330, 250);
        j.setVisible(true);
        j.setLocationRelativeTo(null);
        j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        j.setResizable(false);
      
    }
   
   
   
   

    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        Object o=e.getSource();
         
         if(o==r1){
              try {
                    String encoding="GBK";
                    File file=new File("e:\\java","abc.txt");
                    if(file.isFile() && file.exists()){
                        InputStreamReader read = new InputStreamReader(
                        new FileInputStream(file),encoding);
                        BufferedReader bufferedReader = new BufferedReader(read);
                        String lineTxt = null;
                        while((lineTxt = bufferedReader.readLine()) != null){
                           
                            System.out.println(lineTxt);
                          
                        }
                        read.close();
            }else{
                JOptionPane.showMessageDialog(null, "找不到指定的文件", "错误", JOptionPane.ERROR_MESSAGE);
               // System.out.println("找不到指定的文件");
            }
            } catch (Exception e1) {
                JOptionPane.showMessageDialog(null, "读取文件错误", "错误", JOptionPane.ERROR_MESSAGE);
                e1.printStackTrace();
            }
            //File f1=new File( "e:\\java","abc.txt");
            ///System.out.println("");
         }
         if(o==r2){
            
            /* JOptionPane.showConfirmDialog(null,
                     "choose one", "choose one", JOptionPane.YES_NO_OPTION);

if(selectedValue == null){
   
}*/

            Object[] options = { "确认", "取消" };
             JOptionPane.showOptionDialog(null, "确认要清除所有聊天记录?", "Warning",
             JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE,
             null, options, options[0]); /**/
            
             /*JOptionPane.showInternalConfirmDialog(frame,
                     "please choose one", "information",
                     JOptionPane.YES_NO_CANCEL_OPTION,
                     JOptionPane.INFORMATION_MESSAGE); */


             {
             File f1=new File("e:/java/abc.txt");
             f1.delete();
             }
            
         // else
          //{System.exit(0); }
         }
         if(o==r3){
             System.exit(0);
         }
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        new Jilu();
    }

}
0 回复
1