Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.23 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.io.*;
  5.  
  6. public class Window  extends JFrame implements ActionListener{
  7.     private JPanel p1=new JPanel();
  8.      private JPanel p2=new JPanel();
  9.     private JButton b1=new JButton("OPEN");
  10.     private JButton b2=new JButton("SAVE");
  11.     private JTextArea ta=new JTextArea();
  12.     private BufferedReader b;
  13.    
  14.     public Window(FileReader f){
  15.        
  16.     super("My window");
  17.     this.b=new BufferedReader(f);
  18.     this.setSize(600,600);
  19.     this.setLayout(new GridLayout(2,1));
  20.     this.setVisible(true);
  21.     this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
  22.     this.p1.setSize(300,300);
  23. this.p1.setLayout(new GridLayout(1,2));
  24. this.p1.add(this.b1);
  25. this.p1.add(this.b2);
  26. this.add(this.p1);
  27. this.p2.setSize(300,300);
  28. //this.ta.setHorizontalAlignment(JTextArea.CENTER);
  29. this.p2.setLayout(new GridLayout(1,1));
  30. this.p2.add(this.ta);
  31. this.add(this.p2);
  32. b1.addActionListener(this);
  33. b2.addActionListener(this);
  34.    
  35.     }
  36.     public void actionPerformed(ActionEvent e){
  37.         String line="";
  38.         if(e.getSource()==b1){
  39.             while(line!=null){
  40.                 try{
  41.                    
  42.                
  43.                line=this.b.readLine();
  44.                 this.ta.append(line);
  45.                 this.ta.append("\n");
  46.                 }
  47.                 catch(IOException exception){
  48.                    exception.printStackTrace();
  49.                 }
  50.             }
  51.            
  52.         }
  53.         if(e.getSource()==b2){
  54.             try{
  55.                FileWriter fw=new FileWriter("C:\\Users\\user.LAB2PC1\\Desktop\\test.txt");
  56.                BufferedWriter bw=new BufferedWriter(fw);
  57.                String data=this.ta.getText();
  58.                bw.write(data);
  59.                bw.close();
  60.                fw.close();
  61.                
  62.             }
  63.             catch(IOException ex1){
  64.                 ex1.printStackTrace();
  65.             }
  66.            
  67.         }
  68.        
  69.     }
  70.     public static void main(String[] args){
  71.         try{
  72.         FileReader f=new FileReader("C:\\Users\\user.LAB2PC1\\Desktop\\test.txt");
  73.         Window m=new Window(f);}
  74.         catch(IOException e2){
  75.             e2.printStackTrace();
  76.         }
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement