fork download
  1. package topica.edu.vn.ui;
  2.  
  3. import java.awt.BorderLayout;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. import java.io.BufferedReader;
  7. import java.io.File;
  8. import java.io.FileInputStream;
  9. import java.io.FileOutputStream;
  10. import java.io.IOException;
  11. import java.io.InputStreamReader;
  12. import java.io.OutputStreamWriter;
  13.  
  14. import javax.swing.BoxLayout;
  15. import javax.swing.JFileChooser;
  16. import javax.swing.JFrame;
  17. import javax.swing.JMenu;
  18. import javax.swing.JMenuBar;
  19. import javax.swing.JMenuItem;
  20. import javax.swing.JOptionPane;
  21. import javax.swing.JScrollPane;
  22. import javax.swing.JTextArea;
  23. import javax.swing.filechooser.FileFilter;
  24.  
  25. public class HocJOptionPaneVaJFileChoose {
  26. JFrame frame;
  27. JMenuBar menubar;
  28. JMenu file;
  29. JMenuItem FileOpen,FileSave,FileExit;
  30. JTextArea jtxArea;
  31. JFileChooser chooser ;
  32.  
  33. public HocJOptionPaneVaJFileChoose() {
  34. // TODO Auto-generated constructor stub
  35. initContainer();
  36.  
  37. initComponent();
  38.  
  39. addComponent();
  40.  
  41. setAction();
  42.  
  43. frame.setVisible(true);
  44.  
  45. }
  46.  
  47. private void setAction() {
  48. // TODO Auto-generated method stub
  49. FileExit.addActionListener(new ActionListener() {
  50.  
  51. @Override
  52. public void actionPerformed(ActionEvent e) {
  53. // TODO Auto-generated method stub
  54. XuLyThoatPhanMem();
  55. }
  56. });
  57.  
  58. FileOpen.addActionListener(new ActionListener() {
  59.  
  60. @Override
  61. public void actionPerformed(ActionEvent e) {
  62. // TODO Auto-generated method stub
  63. XuLyMoFile();
  64. }
  65. });
  66.  
  67. FileSave.addActionListener(new ActionListener() {
  68.  
  69. @Override
  70. public void actionPerformed(ActionEvent e) {
  71. // TODO Auto-generated method stub
  72. XyLyLuuFile();
  73. }
  74. });
  75.  
  76. }
  77.  
  78.  
  79.  
  80. protected void XyLyLuuFile() {
  81. // TODO Auto-generated method stub
  82. /*
  83. *Khi đã đọc được từ file có sẵn trong hệ thống và hiển thị nó lên JtextArea rồi
  84. *Mình muốn chỉnh sửa và lưu nó vào file gốc. (thêm sửa xóa)
  85. */
  86. if(chooser.showSaveDialog(null)==JFileChooser.APPROVE_OPTION)
  87. {
  88. String data = jtxArea.getText();
  89. try {
  90. fos = new FileOutputStream(chooser.getSelectedFile());
  91. osw = new OutputStreamWriter(fos,"UTF-8");
  92. osw.write(data);
  93. /*
  94. * Đoạn này nếu cho thêm
  95. * fos.close(); thì chương trình báo lỗi stream closed trong khi code mẫu vấn chạy được
  96. * tham khảo tại sao lại có lỗi này
  97. http://g...content-available-to-author-only...e.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/io/BufferedWriter.java#BufferedWriter.close%28%29
  98. http://g...content-available-to-author-only...e.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/io/BufferedReader.java#BufferedReader.close%28%29
  99. */
  100.  
  101. osw.close();
  102.  
  103.  
  104. JOptionPane.showMessageDialog(null,"Lưu file thành công");
  105.  
  106. } catch (Exception e) {
  107. // TODO: handle exception
  108. System.out.println(e.getMessage());
  109. }
  110.  
  111. }
  112.  
  113. }
  114.  
  115. protected void XuLyMoFile() {
  116. /*
  117. * Ở hàm này mình muốn mở file có sẵn rồi đọc , dữ liệu sẽ được ghi ra JtextArea
  118. * - Để những file trong danh sách mình chọn đã được lọc chỉ có file txt ta sử dụng hàm setFileFilter
  119. */
  120. // TODO Auto-generated method stub
  121.  
  122. chooser.setFileFilter(new FileFilter() {
  123.  
  124. @Override
  125. public String getDescription() {
  126. // TODO Auto-generated method stub
  127. return "File.TXT";
  128. }
  129.  
  130. @Override
  131. public boolean accept(File f) {
  132. // TODO Auto-generated method stub
  133. return f.getAbsolutePath().endsWith(".txt");
  134. }
  135. });
  136.  
  137. /*
  138. * Nếu bạn tiếp tục muốn lọc nhiều loại file hơn thì tiếp tục sử dụng hàm setFileFilter kết hợp với toán tử ||
  139. */
  140. chooser.setFileFilter(new FileFilter() {
  141.  
  142. @Override
  143. public String getDescription() {
  144. // TODO Auto-generated method stub
  145. return "Word2000,Word2010,Word2003";
  146. }
  147.  
  148. @Override
  149. public boolean accept(File f) {
  150. // TODO Auto-generated method stub
  151. return f.getAbsolutePath().endsWith(".doc")||f.getAbsolutePath().endsWith(".docx");
  152. }
  153. });
  154.  
  155.  
  156.  
  157. if(chooser.showOpenDialog(null)==JFileChooser.APPROVE_OPTION)
  158. {
  159. try
  160. {
  161. File slectedFile = chooser.getSelectedFile();
  162. FileInputStream fis = new FileInputStream(slectedFile);
  163. InputStreamReader isr = new InputStreamReader(fis,"UTF-8");
  164. String line = br.readLine();
  165. StringBuilder builer = new StringBuilder();
  166. /*
  167. * Thằng builer này sẽ được gán giá trị bởi thằng line (line là mỗi dòng)
  168. * line chỉ đọc được 1 dòng rồi sau đó gán giá trị đó cho builer , tiếp tục next đến dòng tiếp theo
  169. */
  170. while(line!=null)
  171. {
  172. builer.append(line);
  173. line = br.readLine();
  174. }
  175.  
  176. jtxArea.setText(builer.toString());
  177.  
  178. }
  179. catch(Exception ex)
  180. {
  181.  
  182. }
  183.  
  184.  
  185.  
  186. }
  187.  
  188.  
  189. }
  190.  
  191. protected void XuLyThoatPhanMem() {
  192. // TODO Auto-generated method stub
  193. /*
  194. * Khi chúng ta bấm thoát thì hiện ra của sổ confirm đây gọi là cửa sổ xác nhận nó sẽ trả về 1 số nguyên
  195. * Khi nhân vào nút thoát nó sẽ xuất hiện cửa số YES_NO
  196. * Sự kiện được xử lý khi bạn nhấn YES hoặc No (nó sẽ trả về 1 số nguyên như nói ở trên)
  197. */
  198. int ref =JOptionPane.showConfirmDialog(null,
  199. "Bạn có chắc chắn muốn thoát",
  200. "Xác nhận thoát",JOptionPane.YES_NO_OPTION);
  201.  
  202. if(ref==JOptionPane.YES_OPTION)
  203. {
  204. System.exit(0);
  205.  
  206. }
  207.  
  208. }
  209.  
  210. private void addComponent() {
  211. // TODO Auto-generated method stub
  212. frame.add(jsp,BorderLayout.CENTER);
  213.  
  214. }
  215.  
  216. private void setMenuBar() {
  217. // TODO Auto-generated method stub
  218. menubar = new JMenuBar();
  219. frame.setJMenuBar(menubar);
  220. file = new JMenu("Hệ thống");
  221. menubar.add(file);
  222. FileSave = new JMenuItem("Lưu tập tin");
  223. FileOpen = new JMenuItem("Mở tập tin");
  224. FileExit = new JMenuItem("Thoát");
  225. file.add(FileSave);
  226. file.add(FileOpen);
  227. file.add(FileExit);
  228. }
  229.  
  230. private void initComponent() {
  231. // TODO Auto-generated method stub
  232. setMenuBar();
  233. /*
  234. * Chú ý khi sử dụng JTextArea
  235. * + Đơn giản đây chỉ là 1 trường để ta nhập text vào nhưng kết hợp với JScrollPane
  236. * + Khi sử dụng JtextArea ta cần sử dụng lineWrap(dùng để xuống dòng) và setWrapStyleWord (Dùng để xuống dòng nhưng từ không bị chia ra)
  237. */
  238. jtxArea = new JTextArea(50,50);
  239. jtxArea.setLineWrap(true);
  240. jtxArea.setWrapStyleWord(true);
  241. jsp = new JScrollPane(jtxArea,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
  242. chooser = new JFileChooser();
  243.  
  244.  
  245.  
  246. }
  247.  
  248. private void initContainer() {
  249. // TODO Auto-generated method stub
  250. frame = new JFrame("My Windows");
  251. frame.setLayout(new BorderLayout());
  252. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  253. frame.setSize(400, 600);
  254. frame.setLocationRelativeTo(null);
  255.  
  256.  
  257. }
  258.  
  259. public static void main(String[] args) {
  260. new HocJOptionPaneVaJFileChoose();
  261. }
  262.  
  263. }
  264.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:25: error: class HocJOptionPaneVaJFileChoose is public, should be declared in a file named HocJOptionPaneVaJFileChoose.java
public class HocJOptionPaneVaJFileChoose {
       ^
1 error
stdout
Standard output is empty