Advertisement
Guest User

Untitled

a guest
May 20th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.20 KB | None | 0 0
  1. /*
  2. * To change this template, choose Tools | Templates
  3. * and open the template in the editor.
  4. */
  5. package javaapplication34;
  6.  
  7. import java.awt.BorderLayout;
  8. import java.awt.Color;
  9. import java.awt.GridLayout;
  10. import java.awt.event.ActionEvent;
  11. import java.awt.event.ActionListener;
  12. import java.io.BufferedReader;
  13. import java.io.BufferedWriter;
  14. import java.io.File;
  15. import java.io.FileReader;
  16. import java.io.FileWriter;
  17. import javax.swing.JButton;
  18. import javax.swing.JFrame;
  19. import javax.swing.JOptionPane;
  20. import javax.swing.JPanel;
  21. import javax.swing.JTextArea;
  22.  
  23. public class MySuperFrame extends JFrame implements ActionListener {
  24.  
  25. final String SAVE_JBUTTON = "save";
  26. final String OPEN_JBUTTON = "open";
  27. private JPanel panel;
  28. private JButton save;
  29. private JButton open;
  30. private JTextArea textArea;
  31.  
  32. public MySuperFrame() {
  33. super("Super Frame!!!");
  34. this.setSize(620, 480);
  35. this.panel = new JPanel();
  36. this.save = new JButton("Save");
  37. this.open = new JButton("Open");
  38. this.save.setActionCommand("save");
  39. this.textArea = new JTextArea();
  40. this.panel.setBackground(Color.black);
  41. this.panel.setLayout(new BorderLayout());
  42. JPanel buttons = new JPanel();
  43. buttons.setLayout(new GridLayout(1, 2));
  44. buttons.add(save);
  45. buttons.add(open);
  46. final String fileName = "tmp.txt";
  47. //this.save.addActionListener(this);
  48. //this.open.addActionListener(this);
  49. this.open.addActionListener(new ActionListener() {
  50. @Override
  51. public void actionPerformed(ActionEvent e) {
  52. //open.setBackground(Color.red);
  53. File f = new File(fileName);
  54. if (f.exists()) {
  55. try {
  56. FileReader fr = new FileReader(f);
  57. BufferedReader br = new BufferedReader(fr);
  58. String lines = "";
  59. String line = "";
  60. while ((line = br.readLine()) != null) {
  61. lines += line + "\n";
  62. }
  63. textArea.setText(lines);
  64. br.close();
  65. } catch (Exception ex) {
  66. JOptionPane.showMessageDialog(null, "File not found");
  67. }
  68. } else {
  69. JOptionPane.showMessageDialog(null, "File not found");
  70. }
  71.  
  72. }
  73. });
  74.  
  75. this.save.addActionListener(new ActionListener() {
  76. @Override
  77. public void actionPerformed(ActionEvent e) {
  78. File f = new File(fileName);
  79. try {
  80. FileWriter fw = new FileWriter(f);
  81. BufferedWriter bw = new BufferedWriter(fw);
  82. bw.write(textArea.getText());
  83. bw.flush();
  84. bw.close();
  85. } catch (Exception ex) {
  86.  
  87. JOptionPane.showMessageDialog(null, ex.toString());
  88. }
  89. }
  90. });
  91. this.open.setActionCommand("open");
  92. //Observer ob = new Observer();
  93. //this.save.addActionListener(ob);
  94. this.panel.add(this.textArea, BorderLayout.CENTER);
  95. this.panel.add(buttons, BorderLayout.NORTH);
  96.  
  97. this.setDefaultCloseOperation(EXIT_ON_CLOSE);
  98. this.add(this.panel);
  99.  
  100. }
  101.  
  102. public static void main(String[] args) {
  103. MySuperFrame frame = new MySuperFrame();
  104. frame.setVisible(true);
  105. }
  106.  
  107. @Override
  108. public void actionPerformed(ActionEvent e) {
  109. switch (e.getActionCommand()) {
  110. case SAVE_JBUTTON: {
  111. JButton btn = (JButton) e.getSource();
  112. btn.setText("You clicked me");
  113. this.textArea.setText("Hello world!");
  114. break;
  115. }
  116. case OPEN_JBUTTON: {
  117. JButton btn = (JButton) e.getSource();
  118. btn.setText("Hello world!");
  119. this.textArea.setText("You clicked me");
  120. break;
  121. }
  122. default:
  123. }
  124.  
  125. }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement