Guest User

Untitled

a guest
May 6th, 2015
355
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.51 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.util.Scanner;
  5. import java.io.*;
  6.  
  7. public class Simplenotepad extends JFrame implements ActionListener {
  8.  
  9. // private TextArea textArea = new TextArea("", 0, 0,
  10. // TextArea.SCROLLBARS_VERTICAL_ONLY);
  11.  
  12. private JTextArea textArea = new JTextArea(10, 15);
  13.  
  14. private JScrollPane scrollPane = new JScrollPane(textArea);
  15.  
  16. private MenuBar menuBar = new MenuBar(); // first, create a MenuBar item
  17.  
  18. private Font f = new Font("Verdana", Font.PLAIN, 10);
  19.  
  20. private Menu file = new Menu();
  21. private MenuItem open = new MenuItem();
  22. private MenuItem save = new MenuItem();
  23. private MenuItem exit = new MenuItem();
  24.  
  25. private Menu format = new Menu();
  26. private MenuItem wrap = new MenuItem();
  27. private MenuItem noWrap = new MenuItem();
  28. private Menu font = new Menu();
  29. private MenuItem s10 = new MenuItem();
  30. private MenuItem s12 = new MenuItem();
  31. private MenuItem s14 = new MenuItem();
  32. private MenuItem s16 = new MenuItem();
  33. private MenuItem s18 = new MenuItem();
  34. private MenuItem s20 = new MenuItem();
  35.  
  36. private Menu edit = new Menu();
  37. private MenuItem clear = new MenuItem();
  38.  
  39. private Menu help = new Menu();
  40. private MenuItem about = new MenuItem();
  41.  
  42. private String saveTest = "";
  43.  
  44. public Simplenotepad() {
  45.  
  46. this.setSize(250, 250);
  47. this.textArea.setFont(f);
  48. this.setTitle("A Simple Notepad");
  49. this.textArea.setLineWrap(true);
  50.  
  51. this.scrollPane.add(textArea);
  52. setDefaultCloseOperation(EXIT_ON_CLOSE);
  53.  
  54. this.getContentPane().setLayout(new BorderLayout());
  55. this.getContentPane().add(textArea);
  56. this.getContentPane().add(scrollPane, BorderLayout.EAST);
  57.  
  58. this.scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
  59.  
  60. this.setMenuBar(this.menuBar);
  61. this.menuBar.add(this.file);
  62. this.menuBar.add(this.format);
  63. this.menuBar.add(this.edit);
  64. this.menuBar.add(this.help);
  65.  
  66. this.file.setLabel("File");
  67. this.open.setLabel("Open");
  68. this.save.setLabel("Save");
  69. this.exit.setLabel("Exit");
  70. this.format.setLabel("Format");
  71. this.wrap.setLabel("Text-wrap");
  72. this.noWrap.setLabel("No text wrap");
  73. this.font.setLabel("Font size");
  74. this.s10.setLabel("10");
  75. this.s12.setLabel("12");
  76. this.s14.setLabel("14");
  77. this.s16.setLabel("16");
  78. this.s18.setLabel("18");
  79. this.s20.setLabel("20");
  80. this.edit.setLabel("Edit");
  81. this.clear.setLabel("Clear");
  82. this.help.setLabel("Help");
  83. this.about.setLabel("About");
  84.  
  85. this.open.addActionListener(this);
  86. this.save.addActionListener(this);
  87. this.exit.addActionListener(this);
  88. this.wrap.addActionListener(this);
  89. this.noWrap.addActionListener(this);
  90. this.font.addActionListener(this);
  91. this.s10.addActionListener(this);
  92. this.s12.addActionListener(this);
  93. this.s14.addActionListener(this);
  94. this.s16.addActionListener(this);
  95. this.s18.addActionListener(this);
  96. this.s20.addActionListener(this);
  97. this.clear.addActionListener(this);
  98. this.about.addActionListener(this);
  99.  
  100. this.file.add(this.open);
  101. this.file.add(this.save);
  102. this.file.add(this.exit);
  103. this.help.add(this.about);
  104. this.edit.add(this.clear);
  105. this.format.add(this.wrap);
  106. this.format.add(this.noWrap);
  107. this.format.add(this.font);
  108. this.font.add(this.s10);
  109. this.font.add(this.s12);
  110. this.font.add(this.s14);
  111. this.font.add(this.s16);
  112. this.font.add(this.s18);
  113. this.font.add(this.s20);
  114.  
  115. }
  116.  
  117. public void actionPerformed(ActionEvent e) {
  118.  
  119. if (e.getSource() == this.about) {
  120.  
  121. JOptionPane
  122. .showMessageDialog(null,
  123. "Denne applikasjonen er laget av MEG \n Dette er en enkel tekstbehandler");
  124.  
  125. } else if (e.getSource() == this.open) {
  126. JFileChooser openFile = new JFileChooser();
  127. int filePath = openFile.showOpenDialog(this);
  128. if (filePath == JFileChooser.APPROVE_OPTION) {
  129. this.textArea.setText(""); // clears the current text when
  130. // opening a new file
  131. try {
  132. Scanner scan = new Scanner(new FileReader(openFile
  133. .getSelectedFile().getPath()));
  134. while (scan.hasNext())
  135. this.textArea.append(scan.nextLine() + "\n\r");
  136. scan.close();
  137. } catch (Exception ex) {
  138. System.out.println(ex.getMessage());
  139. }
  140.  
  141. }
  142. } else if (e.getSource() == this.save) {
  143. JFileChooser saveFile = new JFileChooser();
  144. int filePath = saveFile.showSaveDialog(this);
  145.  
  146. if (filePath == JFileChooser.APPROVE_OPTION) {
  147.  
  148. try {
  149.  
  150. BufferedWriter writer = new BufferedWriter(new FileWriter(
  151. saveFile.getSelectedFile().getPath()));
  152. writer.write(this.textArea.getText());
  153. writer.close();
  154.  
  155. saveTest = this.textArea.getText();
  156. } catch (Exception ex) {
  157.  
  158. System.out.println(ex.getMessage());
  159.  
  160. }
  161.  
  162. }
  163. } else if (e.getSource() == this.noWrap) {
  164. textArea.setLineWrap(false);
  165.  
  166. } else if (e.getSource() == this.wrap) {
  167. textArea.setLineWrap(true);
  168. } else if (e.getSource() == this.s10) {
  169. this.f = new Font("Verdana", Font.PLAIN, 10);
  170. textArea.setFont(f);
  171. } else if (e.getSource() == this.s12) {
  172. this.f = new Font("Verdana", Font.PLAIN, 12);
  173. textArea.setFont(f);
  174. } else if (e.getSource() == this.s14) {
  175. this.f = new Font("Verdana", Font.PLAIN, 14);
  176. textArea.setFont(f);
  177. } else if (e.getSource() == this.s16) {
  178. this.f = new Font("Verdana", Font.PLAIN, 16);
  179. textArea.setFont(f);
  180. } else if (e.getSource() == this.s18) {
  181. this.f = new Font("Verdana", Font.PLAIN, 18);
  182. textArea.setFont(f);
  183. } else if (e.getSource() == this.s20) {
  184. this.f = new Font("Verdana", Font.PLAIN, 20);
  185. textArea.setFont(f);
  186. } else if (e.getSource() == this.exit) {
  187. if (saveTest == this.textArea.getText()) {
  188. this.dispose();
  189. } else {
  190.  
  191. int choice = JOptionPane.showOptionDialog(null,
  192. "Vill du lagre før du lukker applikasjonen=", "Exit",
  193. JOptionPane.YES_NO_CANCEL_OPTION,
  194. JOptionPane.INFORMATION_MESSAGE, null, null, null);
  195. if (choice == 0) {
  196. // Yes was choosen
  197.  
  198. JFileChooser saveFile = new JFileChooser();
  199. int filePath = saveFile.showSaveDialog(this);
  200.  
  201. if (filePath == JFileChooser.APPROVE_OPTION) {
  202.  
  203. try {
  204.  
  205. BufferedWriter writer = new BufferedWriter(
  206. new FileWriter(saveFile.getSelectedFile()
  207. .getPath()));
  208. writer.write(this.textArea.getText());
  209. writer.close();
  210.  
  211. saveTest = this.textArea.getText();
  212. } catch (Exception ex) {
  213.  
  214. System.out.println(ex.getMessage());
  215.  
  216. }
  217.  
  218. }
  219.  
  220. } else if (choice == 1) {
  221. // No was choosen
  222. this.dispose();
  223. }
  224. }
  225.  
  226. } else if (e.getSource() == this.clear) {
  227. int choice = JOptionPane.showOptionDialog(null,
  228. "Vill du lagre før du lukker applikasjonen=", "Exit",
  229. JOptionPane.YES_NO_CANCEL_OPTION,
  230. JOptionPane.INFORMATION_MESSAGE, null, null, null);
  231. if (choice == 0) {
  232. JFileChooser saveFile = new JFileChooser();
  233. int filePath = saveFile.showSaveDialog(this);
  234.  
  235. if (filePath == JFileChooser.APPROVE_OPTION) {
  236.  
  237. try {
  238.  
  239. BufferedWriter writer = new BufferedWriter(
  240. new FileWriter(saveFile.getSelectedFile()
  241. .getPath()));
  242. writer.write(this.textArea.getText());
  243. writer.close();
  244.  
  245. saveTest = this.textArea.getText();
  246. } catch (Exception ex) {
  247.  
  248. System.out.println(ex.getMessage());
  249.  
  250. }
  251. }
  252.  
  253. } else if (choice == 1) {
  254. this.textArea.setText("");
  255. }
  256. }
  257. }
  258.  
  259. public static void main(String args[]) {
  260.  
  261. Simplenotepad app = new Simplenotepad();
  262.  
  263. app.setVisible(true);
  264.  
  265. }
  266.  
  267. }
Advertisement
Add Comment
Please, Sign In to add comment