Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- import java.io.*;
- public class Window extends JFrame implements ActionListener{
- private JPanel p1=new JPanel();
- private JPanel p2=new JPanel();
- private JButton b1=new JButton("OPEN");
- private JButton b2=new JButton("SAVE");
- private JTextArea ta=new JTextArea();
- private BufferedReader b;
- public Window(FileReader f){
- super("My window");
- this.b=new BufferedReader(f);
- this.setSize(600,600);
- this.setLayout(new GridLayout(2,1));
- this.setVisible(true);
- this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
- this.p1.setSize(300,300);
- this.p1.setLayout(new GridLayout(1,2));
- this.p1.add(this.b1);
- this.p1.add(this.b2);
- this.add(this.p1);
- this.p2.setSize(300,300);
- //this.ta.setHorizontalAlignment(JTextArea.CENTER);
- this.p2.setLayout(new GridLayout(1,1));
- this.p2.add(this.ta);
- this.add(this.p2);
- b1.addActionListener(this);
- b2.addActionListener(this);
- }
- public void actionPerformed(ActionEvent e){
- String line="";
- if(e.getSource()==b1){
- while(line!=null){
- try{
- line=this.b.readLine();
- this.ta.append(line);
- this.ta.append("\n");
- }
- catch(IOException exception){
- exception.printStackTrace();
- }
- }
- }
- if(e.getSource()==b2){
- try{
- FileWriter fw=new FileWriter("C:\\Users\\user.LAB2PC1\\Desktop\\test.txt");
- BufferedWriter bw=new BufferedWriter(fw);
- String data=this.ta.getText();
- bw.write(data);
- bw.close();
- fw.close();
- }
- catch(IOException ex1){
- ex1.printStackTrace();
- }
- }
- }
- public static void main(String[] args){
- try{
- FileReader f=new FileReader("C:\\Users\\user.LAB2PC1\\Desktop\\test.txt");
- Window m=new Window(f);}
- catch(IOException e2){
- e2.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement