CI 2019 von Daniel, Eugen und Michael
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

199 lines
5.5 KiB

  1. // Java Program to create a text editor using java
  2. import java.awt.*;
  3. import javax.swing.*;
  4. import java.io.*;
  5. import java.awt.event.*;
  6. import javax.swing.plaf.metal.*;
  7. import javax.swing.text.*;
  8. class Geburtstag extends JFrame implements ActionListener {
  9. // Text component
  10. JTextArea t;
  11. // Frame
  12. JFrame f;
  13. // Constructor
  14. Geburtstag()
  15. {
  16. // Create a frame
  17. f = new JFrame("editor");
  18. try {
  19. // Set metl look and feel
  20. UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
  21. // Set theme to ocean
  22. MetalLookAndFeel.setCurrentTheme(new OceanTheme());
  23. }
  24. catch (Exception e) {
  25. }
  26. // Text component
  27. t = new JTextArea();
  28. // Create a menubar
  29. JMenuBar mb = new JMenuBar();
  30. // Create amenu for menu
  31. JMenu m1 = new JMenu("File");
  32. // Create menu items
  33. JMenuItem mi1 = new JMenuItem("New");
  34. JMenuItem mi2 = new JMenuItem("Open");
  35. JMenuItem mi3 = new JMenuItem("Save");
  36. JMenuItem mi9 = new JMenuItem("Print");
  37. // Add action listener
  38. mi1.addActionListener(this);
  39. mi2.addActionListener(this);
  40. mi3.addActionListener(this);
  41. mi9.addActionListener(this);
  42. m1.add(mi1);
  43. m1.add(mi2);
  44. m1.add(mi3);
  45. m1.add(mi9);
  46. // Create amenu for menu
  47. JMenu m2 = new JMenu("Edit");
  48. // Create menu items
  49. JMenuItem mi4 = new JMenuItem("cut");
  50. JMenuItem mi5 = new JMenuItem("copy");
  51. JMenuItem mi6 = new JMenuItem("paste");
  52. // Add action listener
  53. mi4.addActionListener(this);
  54. mi5.addActionListener(this);
  55. mi6.addActionListener(this);
  56. m2.add(mi4);
  57. m2.add(mi5);
  58. m2.add(mi6);
  59. JMenuItem mc = new JMenuItem("close");
  60. mc.addActionListener(this);
  61. mb.add(m1);
  62. mb.add(m2);
  63. mb.add(mc);
  64. f.setJMenuBar(mb);
  65. f.add(t);
  66. f.setSize(500, 500);
  67. f.show();
  68. }
  69. // If a button is pressed
  70. public void actionPerformed(ActionEvent e)
  71. {
  72. String s = e.getActionCommand();
  73. if (s.equals("cut")) {
  74. t.cut();
  75. }
  76. else if (s.equals("copy")) {
  77. t.copy();
  78. }
  79. else if (s.equals("paste")) {
  80. t.paste();
  81. }
  82. else if (s.equals("Save")) {
  83. // Create an object of JFileChooser class
  84. JFileChooser j = new JFileChooser("f:");
  85. // Invoke the showsSaveDialog function to show the save dialog
  86. int r = j.showSaveDialog(null);
  87. if (r == JFileChooser.APPROVE_OPTION) {
  88. // Set the label to the path of the selected directory
  89. File fi = new File(j.getSelectedFile().getAbsolutePath());
  90. try {
  91. // Create a file writer
  92. FileWriter wr = new FileWriter(fi, false);
  93. // Create buffered writer to write
  94. BufferedWriter w = new BufferedWriter(wr);
  95. // Write
  96. w.write(t.getText());
  97. w.flush();
  98. w.close();
  99. }
  100. catch (Exception evt) {
  101. JOptionPane.showMessageDialog(f, evt.getMessage());
  102. }
  103. }
  104. // If the user cancelled the operation
  105. else
  106. JOptionPane.showMessageDialog(f, "the user cancelled the operation");
  107. }
  108. else if (s.equals("Print")) {
  109. try {
  110. // print the file
  111. t.print();
  112. }
  113. catch (Exception evt) {
  114. JOptionPane.showMessageDialog(f, evt.getMessage());
  115. }
  116. }
  117. else if (s.equals("Open")) {
  118. // Create an object of JFileChooser class
  119. JFileChooser j = new JFileChooser("f:");
  120. // Invoke the showsOpenDialog function to show the save dialog
  121. int r = j.showOpenDialog(null);
  122. // If the user selects a file
  123. if (r == JFileChooser.APPROVE_OPTION) {
  124. // Set the label to the path of the selected directory
  125. File fi = new File(j.getSelectedFile().getAbsolutePath());
  126. try {
  127. // String
  128. String s1 = "", sl = "";
  129. // File reader
  130. FileReader fr = new FileReader(fi);
  131. // Buffered reader
  132. BufferedReader br = new BufferedReader(fr);
  133. // Initilize sl
  134. sl = br.readLine();
  135. // Take the input from the file
  136. while ((s1 = br.readLine()) != null) {
  137. sl = sl + "\n" + s1;
  138. }
  139. // Set the text
  140. t.setText(sl);
  141. }
  142. catch (Exception evt) {
  143. JOptionPane.showMessageDialog(f, evt.getMessage());
  144. }
  145. }
  146. // If the user cancelled the operation
  147. else
  148. JOptionPane.showMessageDialog(f, "the user cancelled the operation");
  149. }
  150. else if (s.equals("New")) {
  151. t.setText("");
  152. }
  153. else if (s.equals("close")) {
  154. f.setVisible(false);
  155. }
  156. }
  157. // Main class
  158. public static void main(String args[])
  159. {
  160. fenster e = new fenster();
  161. }
  162. }