Advertisement
Guest User

Untitled

a guest
May 23rd, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.io.*;
  5.  
  6. class Strumienie extends JFrame implements ActionListener {
  7. JButton read, save1;
  8. TextArea textArea;
  9. JPanel panel;
  10.  
  11. Strumienie(){
  12. super("Strumienie");
  13. setBounds(100,100,300,300);
  14. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  15. panel = new JPanel(null);
  16. panel.setBackground(Color.GREEN);
  17.  
  18. read = new JButton("Odczytaj");
  19. save1 = new JButton("Zapisz");
  20. textArea = new TextArea();
  21.  
  22. read.setBounds(20,200,100,25);
  23. read.addActionListener(this);
  24.  
  25. save1.setBounds(160,200,100,25);
  26. save1.addActionListener(this);
  27.  
  28. textArea.setBounds(20,50,240,100);
  29. panel.add(read);
  30. panel.add(save1);
  31. panel.add(textArea);
  32. setContentPane(panel);
  33. setVisible(true);
  34. }
  35.  
  36. public void actionPerformed(ActionEvent e) {
  37. Object ob = e.getSource();
  38.  
  39. if(ob==save1){
  40. try {
  41. panel.setBackground(Color.CYAN);
  42. FileWriter plik = new FileWriter("tekst123.txt");
  43. plik.write(textArea.getText());
  44. plik.close();
  45. }
  46. catch(IOException x){
  47. textArea.setText("Err0r!@#"+x.toString());
  48. }
  49. }
  50.  
  51. if(ob==read){
  52. try {
  53. panel.setBackground(Color.YELLOW);
  54. FileReader plik = new FileReader("tekst123.txt");
  55. BufferedReader bufor = new BufferedReader(plik);
  56. String line = bufor.readLine();
  57. textArea.setText(line+"\n");
  58. }
  59.  
  60. catch(IOException x) {
  61. textArea.setText("Err0r!@#"+x.toString());
  62. }
  63. }
  64. }
  65.  
  66. public static void main (String arg[]){
  67. new Strumienie();
  68. }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement