Advertisement
Guest User

Untitled

a guest
May 20th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.82 KB | None | 0 0
  1. package javaapplication35;
  2.  
  3. import javax.swing.*;
  4. import java.awt.*;
  5. import java.awt.event.*;
  6. import java.io.BufferedReader;
  7. import java.io.BufferedWriter;
  8. import java.io.File;
  9. import java.io.FileReader;
  10. import java.io.FileWriter;
  11.  
  12. public class MySuperFrame extends JFrame implements ActionListener {
  13.  
  14. private JPanel panel;
  15. private JButton open;
  16. private JButton save;
  17. private JTextArea textArea;
  18. static final String OPEN_ACTION = "OPEN";
  19. static final String SAVE_ACTION = "SAVE";
  20.  
  21. public MySuperFrame() {
  22. //super("My super frame!!!");
  23. this.setTitle("My super frame!!!");
  24. this.setDefaultCloseOperation(EXIT_ON_CLOSE);
  25. this.panel = new JPanel();
  26. this.panel.setLayout(new BorderLayout());
  27. JPanel grid = new JPanel();
  28. grid.setLayout(new GridLayout(1, 2));
  29. this.open = new JButton();
  30. this.save = new JButton();
  31. this.open.setActionCommand("OPEN");
  32. this.save.setActionCommand("SAVE");
  33. //this.open.addActionListener(this);
  34. //this.open.addActionListener(new Observer(open));
  35. //this.save.addActionListener(this);
  36. final String file = "tmp.txt";
  37. this.open.addActionListener(new ActionListener() {
  38. @Override
  39. public void actionPerformed(ActionEvent e) {
  40. File f = new File(file);
  41. if (f.exists()) {
  42. try {
  43. FileReader fr = new FileReader(f);
  44. BufferedReader br = new BufferedReader(fr);
  45. String lines = "";
  46. String line = "";
  47. while ((line = br.readLine()) != null) {
  48. lines += line + "\n";
  49. }
  50. br.close();
  51. textArea.setText(lines);
  52. } catch (Exception ex) {
  53. JOptionPane.showMessageDialog(null, ex.toString(), "ERROR!", JOptionPane.ERROR_MESSAGE);
  54. }
  55. } else {
  56. JOptionPane.showMessageDialog(null, "File not present");
  57. }
  58. }
  59. });
  60. this.save.addActionListener(new ActionListener() {
  61. @Override
  62. public void actionPerformed(ActionEvent e) {
  63. try {
  64. FileWriter fw = new FileWriter(file);
  65. BufferedWriter bw = new BufferedWriter(fw);
  66. bw.write(textArea.getText());
  67. bw.flush();
  68. bw.close();
  69. } catch (Exception ex) {
  70. JOptionPane.showMessageDialog(null, ex.toString(), "ERROR!", JOptionPane.ERROR_MESSAGE);
  71. }
  72. }
  73. });
  74. this.open.setText("OPEN");
  75. this.save.setText("SAVE");
  76. this.textArea = new JTextArea();
  77. this.setSize(640, 480);
  78. grid.add(this.open);
  79. grid.add(this.save);
  80. this.panel.add(grid, BorderLayout.NORTH);
  81. this.panel.add(this.textArea, BorderLayout.CENTER);
  82. this.add(this.panel);
  83. }
  84.  
  85. @Override
  86. public void actionPerformed(ActionEvent evt) {
  87. switch (evt.getActionCommand()) {
  88. case MySuperFrame.SAVE_ACTION: {
  89. JButton btn = (JButton) evt.getSource();
  90. btn.setText("You clicked save");
  91. break;
  92. }
  93. case MySuperFrame.OPEN_ACTION: {
  94. JButton btn = (JButton) evt.getSource();
  95. btn.setText("You clicked open");
  96. break;
  97. }
  98. }
  99. }
  100.  
  101. public static void main(String[] args) {
  102. MySuperFrame frame = new MySuperFrame();
  103. frame.setVisible(true);
  104. MySuperFrame frame2 = new MySuperFrame();
  105. frame2.setVisible(true);
  106. }
  107. }
  108. /*
  109. * To change this template, choose Tools | Templates
  110. * and open the template in the editor.
  111. */
  112. package javaapplication35;
  113.  
  114. import java.util.ArrayList;
  115.  
  116. public class Observable {
  117. private ArrayList<IObserver> observers;
  118. public Observable(){
  119. this.observers = new ArrayList<>();
  120. }
  121. public void subscribeObserver(IObserver observer){
  122. this.observers.add(observer);
  123. }
  124. public void onAction(String action){
  125. for(IObserver observer: this.observers){
  126. observer.onEvent(new Event(this,action));
  127. }
  128. }
  129. }
  130. /*
  131. * To change this template, choose Tools | Templates
  132. * and open the template in the editor.
  133. */
  134. package javaapplication35;
  135.  
  136. import java.awt.event.ActionEvent;
  137. import java.awt.event.ActionListener;
  138. import javax.swing.JButton;
  139.  
  140. /**
  141. *
  142. * @author didiamantis
  143. */
  144. public class Observer implements ActionListener{
  145.  
  146. private JButton btn;
  147. public Observer(JButton btn){
  148. this.btn = btn;
  149. }
  150. @Override
  151. public void actionPerformed(ActionEvent e) {
  152. btn.setText("Hello");
  153. System.out.println(e.getActionCommand());
  154. }
  155.  
  156. }
  157. /*
  158. * To change this template, choose Tools | Templates
  159. * and open the template in the editor.
  160. */
  161. package javaapplication35;
  162.  
  163. public interface IObserver {
  164. public void onEvent(Event event);
  165. }
  166. /*
  167. * To change this template, choose Tools | Templates
  168. * and open the template in the editor.
  169. */
  170. package javaapplication35;
  171.  
  172. public class Event {
  173. private Object source;
  174. private String action;
  175. public Event(Object source,String action){
  176. this.action = action;
  177. this.source = source;
  178. }
  179.  
  180. public Object getSource() {
  181. return source;
  182. }
  183.  
  184. public void setSource(Object source) {
  185. this.source = source;
  186. }
  187.  
  188. public String getAction() {
  189. return action;
  190. }
  191.  
  192. public void setAction(String action) {
  193. this.action = action;
  194. }
  195.  
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement