java实现极简单的 TXT小说阅读器

  1. public class Display_txt extends JFrame {  
  2.       
  3.     JFrame jframe = new JFrame();  
  4.     JTextArea content;  
  5.     boolean flag = true;  
  6.     String str_filePath = null;  
  7.       
  8.     public Display_txt(){  
  9.         //输入框  
  10.         content = new JTextArea(10,50);  
  11.         content.setAutoscrolls(true);  
  12.         JScrollPane contentScroll = new JScrollPane(content);  
  13.         content.setBorder(BorderFactory.createBevelBorder(1));  
  14.         JPanel upper = new JPanel(new BorderLayout());  
  15.         upper.add(contentScroll);  
  16.         //按钮  
  17.         JButton filePath = new JButton("打开文件");  
  18.         filePath.addActionListener(new ActionListener(){  
  19.             public void actionPerformed(ActionEvent e){  
  20.                 try{  
  21.                     JFileChooser jfc = new JFileChooser();  
  22.                     if(jfc.showOpenDialog(jframe)==JFileChooser.APPROVE_OPTION ){  
  23.                         str_filePath = jfc.getSelectedFile().getAbsolutePath();  
  24.                     }  
  25.                     BufferedReader bufferedReader = new BufferedReader(new FileReader(str_filePath));  
  26.                     String str_line;  
  27.                     while((str_line=bufferedReader.readLine())!=null){  
  28.                         if(flag){  
  29.                             content.setText(str_line);  
  30.                             flag = false;  
  31.                         }  
  32.                         else{  
  33.                             content.setText(content.getText()+"\n"+str_line);  
  34.                         }  
  35.                     }  
  36.                     bufferedReader.close();  
  37.                 }catch(FileNotFoundException e1){  
  38.                     e1.printStackTrace();  
  39.                 }catch(IOException e2){  
  40.                     e2.printStackTrace();  
  41.                 }  
  42.             }  
  43.         });  
  44.         JPanel buttonp = new JPanel();  
  45.         buttonp.add(filePath);  
  46.         JPanel all = new JPanel(new GridLayout(1,1));  
  47.         all.add(upper);  
  48.         jframe.add(buttonp,BorderLayout.SOUTH);  
  49.         jframe.add(all,BorderLayout.CENTER);  
  50.         jframe.pack();  
  51.         Toolkit tool = Toolkit.getDefaultToolkit();  
  52.         Dimension screen = tool.getScreenSize();  
  53.         jframe.setLocation(screen.width/2-jframe.getWidth()/2,screen.height/2-jframe.getHeight()/2);  
  54.         jframe.setTitle("TXT小说阅读器");  
  55.         jframe.setVisible(true);  
  56.         jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  57.     }  
  58.       
  59.     public static void main(String args[]){  
  60.         Display_txt display_demo = new Display_txt();  
  61.     }  
  62.   
  63. }  

猜你喜欢

转载自blog.csdn.net/qq_31546841/article/details/80249782