标题:如何让我读出的txt文件显示在JTextArea中
只看楼主
ch。浩
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2015-11-19
结帖率:100%
 问题点数:0 回复次数:0 
如何让我读出的txt文件显示在JTextArea中
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();
    }

}
搜索更多相关主题的帖子: private package public import 如何 
2016-06-29 19:23



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-466497-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.085352 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved