文件的打开保存另存为功能代码实现

雨点打透心脏的1/2处 2022-08-22 15:18 280阅读 0赞

Center(效果图)

import java.awt.BorderLayout;
public class NotePad
{
private JFrame jf;

  1. private JMenuBar menu;
  2. private JMenuItem open;

public class NotePad
{
private JFrame jf;

  1. private JMenuBar menu;
  2. private JMenuItem open;
  3. private JMenuItem save;
  4. private JMenuItem saveAs;
  5. private JMenu file;
  6. private JMenu help;
  7. private JMenuItem about;
  8. private JTextArea textArea;
  9. private String filePath;
  10. public void show()
  11. \{
  12. init();
  13. setStyle();
  14. registHandler();
  15. jf.setDefaultCloseOperation(JFrame.EXIT\_ON\_CLOSE);
  16. jf.setVisible(true);
  17. \}
  18. //表单控件初始化
  19. private void init()
  20. \{
  21. jf = new JFrame();
  22. menu = new JMenuBar();
  23. open = new JMenuItem("打开");
  24. save = new JMenuItem("保存");
  25. saveAs = new JMenuItem("另存为");
  26. file = new JMenu("文件");
  27. help = new JMenu("帮助");
  28. about = new JMenuItem("关于");
  29. textArea = new JTextArea();
  30. \}
  31. //表单控件的添加
  32. private void setStyle()
  33. \{
  34. jf.setSize(600, 600);
  35. menu.add(file);
  36. menu.add(help);
  37. file.add(open);
  38. file.add(save);
  39. file.add(saveAs);
  40. help.add(about);
  41. jf.add(menu, BorderLayout.NORTH);
  42. JScrollPane jsp = new JScrollPane(textArea);
  43. jf.add(jsp);
  44. \}

交互事件

  1. private void registHandler()
  2. \{ //给打开按钮添加事件监听(方法实现)
  3. open.addActionListener(new ActionListener()
  4. \{
  5. @Override
  6. public void actionPerformed(ActionEvent e)
  7. \{
  8. FileDialog fd = new FileDialog(jf);
  9. fd.setVisible(true);
  10. if (fd.getFile() != null)
  11. \{
  12. filePath = fd.getDirectory() + fd.getFile();
  13. BufferedReader br = null;
  14. try
  15. \{
  16. br = new BufferedReader(new InputStreamReader(new FileInputStream(filePath)));
  17. String line = null;
  18. while ((line = br.readLine()) != null)
  19. \{
  20. textArea.append(line + "\\n");
  21. \}
  22. \}
  23. catch (Exception e2)
  24. \{
  25. e2.printStackTrace();
  26. \}
  27. finally
  28. \{
  29. if (br != null)
  30. \{
  31. try
  32. \{
  33. br.close();
  34. \}
  35. catch (IOException e1)
  36. \{
  37. e1.printStackTrace();
  38. \}
  39. \}
  40. \}
  41. \}
  42. \}
  43. \});
  44. //给保存按钮添加事件监听(方法实现)
  45. save.addActionListener(new ActionListener()
  46. \{
  47. @Override
  48. public void actionPerformed(ActionEvent e)
  49. \{
  50. // 如果filePath为null
  51. save(false);
  52. \}
  53. \});
  54. //给另存为按钮添加事件监听(方法实现)
  55. saveAs.addActionListener(new ActionListener()
  56. \{
  57. @Override
  58. public void actionPerformed(ActionEvent e)
  59. \{
  60. save(true);
  61. \}
  62. \});
  63. \}
  64. private void save(boolean isSaveAs)
  65. \{
  66. while(filePath == null || isSaveAs)
  67. \{
  68. FileDialog fd = new FileDialog(jf);
  69. fd.setVisible(true);
  70. if(fd.getFile()==null)
  71. \{
  72. continue;
  73. \}
  74. filePath = fd.getDirectory() + fd.getFile();
  75. \}
  76. PrintWriter pw = null;
  77. try
  78. \{
  79. pw = new PrintWriter(filePath);
  80. pw.println(textArea.getText());
  81. \}
  82. catch (Exception e)
  83. \{
  84. e.printStackTrace();
  85. \}finally\{
  86. if(pw != null)
  87. \{
  88. pw.close();
  89. \}
  90. \}
  91. \}
  92. //主函数调用菜单界面
  93. public static void main(String\[\] args)
  94. \{
  95. new NotePad().show();
  96. \}

}

发表评论

表情:
评论列表 (有 0 条评论,280人围观)

还没有评论,来说两句吧...

相关阅读