Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.50 KB | None | 0 0
  1. package notatnik;
  2.  
  3. import java.awt.*;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. import java.awt.event.KeyEvent;
  7. import java.awt.event.KeyListener;
  8. import java.io.*;
  9. import javax.swing.*;
  10.  
  11. public class Notatnik extends JFrame implements ActionListener, KeyListener {
  12.  
  13. private int ile;
  14. public JDialog wybor = new JDialog();
  15. public int bufor1, bufor2;
  16. public int czcionkaWielkosc = 14;
  17. public String wybranaCzcionka = "Arial";
  18. public Font czcionkaGlowna = new Font(wybranaCzcionka, Font.PLAIN, czcionkaWielkosc);
  19. final JLabel licznik = new JLabel("0");
  20. final JTextArea tekst = new JTextArea();
  21.  
  22. void setIle(int ile) {
  23. this.ile = ile;
  24. }
  25.  
  26. int getIle() {
  27. return ile;
  28. }
  29.  
  30. public Notatnik() {
  31. tekst.setWrapStyleWord(true);
  32. tekst.setLineWrap(true);
  33. tekst.addKeyListener(this);
  34. tekst.setFont(czcionkaGlowna);
  35. Toolkit tk = Toolkit.getDefaultToolkit();
  36. Dimension roz = tk.getScreenSize();
  37. int sz = roz.width;
  38. int wys = roz.height;
  39. bufor1 = sz;
  40. bufor2 = wys;
  41.  
  42. setSize(sz / 2, wys / 2);
  43. setLocation(sz / 4, wys / 4);
  44. setTitle("Notatnik");
  45. setResizable(true);
  46. setJMenuBar(menu());
  47.  
  48. JScrollPane scroll = new JScrollPane(tekst, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
  49. setLayout(new BorderLayout());
  50. add(scroll, BorderLayout.CENTER);
  51. add(dolny(), BorderLayout.SOUTH);
  52. setIle(tekst.getText().length());
  53.  
  54. }
  55.  
  56. public JPanel dolny() {
  57. JPanel dolny = new JPanel(new FlowLayout(FlowLayout.LEFT));
  58. JLabel iloscZnakow = new JLabel("Ilość znaków :");
  59. dolny.add(iloscZnakow);
  60. dolny.add(licznik);
  61. return dolny;
  62. }
  63.  
  64. public JMenuBar menu() {
  65. JMenuBar menu = new JMenuBar();
  66. JMenu plik = new JMenu("Plik");
  67. menu.add(plik);
  68. plik.setMnemonic('P');
  69. JMenu edycja = new JMenu("Edycja");
  70. menu.add(edycja);
  71. edycja.setMnemonic('E');
  72. JMenu format = new JMenu("Format");
  73. menu.add(format);
  74. format.setMnemonic('F');
  75. JMenu autor = new JMenu("O autorze");
  76. menu.add(autor);
  77. JMenuItem otworz = new JMenuItem("Otwórz");
  78. otworz.setMnemonic('O');
  79. JMenuItem zapisz = new JMenuItem("Zapisz");
  80. zapisz.setMnemonic('S');
  81. JMenuItem zakoncz = new JMenuItem("Zakończ");
  82. plik.add(otworz);
  83. otworz.setActionCommand("1");
  84. otworz.addActionListener(this);
  85. plik.add(zapisz);
  86. zapisz.setActionCommand("2");
  87. zapisz.addActionListener(this);
  88. plik.addSeparator();
  89. plik.add(zakoncz);
  90. zakoncz.setActionCommand("3");
  91. zakoncz.addActionListener(this);
  92. JMenuItem szukaj = new JMenuItem("Wyszukaj");
  93. JMenuItem zamien = new JMenuItem("Zamień");
  94. JMenu czcionka = new JMenu("Czcionka");
  95. JMenuItem czcionkaRozmiar = new JMenuItem("Rozmiar");
  96. JMenuItem czcionkaStyl = new JMenuItem("Wybór czcionki");
  97. czcionkaRozmiar.setActionCommand("4");
  98. czcionkaRozmiar.addActionListener(this);
  99. czcionka.add(czcionkaRozmiar);
  100. czcionka.add(czcionkaStyl);
  101. czcionkaStyl.setActionCommand("5");
  102. czcionkaStyl.addActionListener(this);
  103. JMenuItem wyczysc = new JMenuItem("Wyczyść");
  104. JMenu styl = new JMenu("Styl");
  105. JMenuItem test = new JMenuItem("test");
  106.  
  107. edycja.add(wyczysc);
  108. edycja.add(szukaj);
  109. edycja.add(zamien);
  110. format.add(czcionka);
  111. format.add(styl);
  112. styl.add(test);
  113. otworz.setAccelerator(KeyStroke.getKeyStroke("ctrl O"));
  114. zapisz.setAccelerator(KeyStroke.getKeyStroke("ctrl S"));
  115. szukaj.setAccelerator(KeyStroke.getKeyStroke("ctrl F"));
  116. zamien.setAccelerator(KeyStroke.getKeyStroke("ctrl H"));
  117. wyczysc.setAccelerator(KeyStroke.getKeyStroke("ctrl N"));
  118.  
  119. return menu;
  120. }
  121.  
  122. public static void main(String[] args) {
  123. Notatnik note = new Notatnik();
  124. note.setVisible(true);
  125. note.setDefaultCloseOperation(EXIT_ON_CLOSE);
  126.  
  127. }
  128.  
  129. public void actionPerformed(ActionEvent wydarzenie) {
  130. switch (Integer.parseInt(wydarzenie.getActionCommand())) {
  131. case 1: {
  132. JFileChooser pliki = new JFileChooser(".");
  133. if (JFileChooser.APPROVE_OPTION == pliki.showOpenDialog(this)) {
  134. try {
  135. File f = pliki.getSelectedFile();
  136. setTitle("Notatnik " + f.getAbsolutePath());
  137. BufferedReader br = new BufferedReader(new FileReader(f));
  138. String temp = "";
  139. while (br.ready()) {
  140. temp += br.readLine() + "\n";
  141. }
  142. tekst.setText(temp);
  143. } catch (IOException e1) {
  144. }
  145. }
  146. break;
  147. }
  148.  
  149. case 2: {
  150. JFileChooser pliki = new JFileChooser(".");
  151. if (JFileChooser.APPROVE_OPTION == pliki.showSaveDialog(this)) {
  152. try {
  153. File f = pliki.getSelectedFile();
  154. try (BufferedWriter bw = new BufferedWriter(new FileWriter(f + ".txt"))) {
  155. bw.write(tekst.getText());
  156. bw.flush();
  157. }
  158. } catch (IOException e2) {
  159. }
  160. }
  161. }
  162. case 3: {
  163. {
  164. System.exit(0);
  165. }
  166. break;
  167. }
  168. case 4: {
  169. {
  170. try {
  171. int czcionkaWielkosc = Integer.parseInt(JOptionPane.showInputDialog(null,
  172. "Rozmiar",
  173. "Proszę wpisać rozmiar czcionki",
  174. JOptionPane.INFORMATION_MESSAGE));
  175. Font czcionkaNowa = new Font(wybranaCzcionka, Font.PLAIN, czcionkaWielkosc);
  176. tekst.setFont(czcionkaNowa);
  177.  
  178. } catch (NumberFormatException e2) {
  179. JOptionPane.showMessageDialog(null, "Proszę podać wartość liczbową", "Błąd", JOptionPane.INFORMATION_MESSAGE);
  180. }
  181. }
  182. break;
  183. }
  184. case 5: {
  185.  
  186. {
  187. Object[] opcje = new Object[]{};
  188. JOptionPane bufor = new JOptionPane("Proszę wybrać czcionkę",
  189. JOptionPane.QUESTION_MESSAGE,
  190. JOptionPane.DEFAULT_OPTION,
  191. null, opcje, null);
  192. String czcionki[]
  193. = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
  194.  
  195. JComboBox combo1 = new JComboBox(czcionki);
  196. combo1.setEditable(true);
  197. bufor.add(combo1);
  198.  
  199.  
  200. wybor.pack();
  201. wybor.setSize(new Dimension(250, 155));
  202. wybor.setResizable(false);
  203. wybor.setLocation((bufor1 / 2) - 100, (bufor2 / 2) - 100);
  204. wybor.getContentPane().add(bufor);
  205. wybor.setVisible(true);
  206. wybor.setLayout(new FlowLayout());
  207.  
  208. JButton ok = new JButton("OK");
  209. wybor.add(ok);
  210. ok.setActionCommand("6");
  211. ok.addActionListener(this);
  212.  
  213. String wybranaCzcionka = combo1.getSelectedItem().toString();
  214. Font czcionkaNowa = new Font(wybranaCzcionka, Font.PLAIN, czcionkaWielkosc);
  215. tekst.setFont(czcionkaNowa);
  216.  
  217. }
  218. }
  219. break;
  220.  
  221. case 6: {
  222. wybor.dispose();
  223. }
  224. break;
  225. }
  226. }
  227.  
  228. @Override
  229. public void keyTyped(KeyEvent ke) {
  230.  
  231. }
  232.  
  233. @Override
  234. public void keyReleased(KeyEvent ke) {
  235.  
  236. licznik.setText(Integer.toString(tekst.getText().length()));
  237.  
  238. }
  239.  
  240. @Override
  241. public void keyPressed(KeyEvent ke) {
  242.  
  243. }
  244.  
  245. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement