evgeniyosipov

SecondFrame.java

Dec 27th, 2014
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.03 KB | None | 0 0
  1. import java.awt.GridLayout;
  2. import java.awt.event.ActionEvent;
  3. import java.awt.event.ActionListener;
  4. import java.io.FileInputStream;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import java.io.ObjectInputStream;
  8. import java.io.ObjectOutputStream;
  9. import java.io.Serializable;
  10. import javax.swing.JButton;
  11. import javax.swing.JFrame;
  12. import javax.swing.JTextField;
  13.  
  14. public class SecondFrame extends JFrame implements ActionListener {
  15.  
  16.     private JTextField jtfSecond1 = null;
  17.     private JButton jbSecond1 = null;
  18.  
  19.     private StringBuffer sb = null;
  20.     private String result = null;
  21.  
  22.     private FirstFrame jframeFirst = null;
  23.  
  24.     public SecondFrame() throws ClassNotFoundException, IOException {
  25.  
  26.         setTitle("Второе окно");
  27.         setBounds(800, 100, 600, 200);
  28.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  29.         setVisible(true);
  30.  
  31.         GridLayout gl = new GridLayout(2, 1);
  32.         setLayout(gl);
  33.  
  34.         jtfSecond1 = new JTextField();
  35.  
  36.         jbSecond1 = new JButton();
  37.         jbSecond1.setText("Отправить текст");
  38.  
  39.         add(jtfSecond1);
  40.         add(jbSecond1);
  41.  
  42.         jbSecond1.addActionListener(this);
  43.  
  44.     }
  45.  
  46.     @Override
  47.     public void actionPerformed(ActionEvent e) {
  48.         try {
  49.             ffM();
  50.             stM(jtfSecond1, jframeFirst.jtfFirst1);
  51.         } catch (ClassNotFoundException | IOException e2) {
  52.             e2.printStackTrace();
  53.         }
  54.     }
  55.  
  56.     private void ffM() throws IOException, ClassNotFoundException {
  57.  
  58.         FileInputStream fis = new FileInputStream("text.ser");
  59.         ObjectInputStream oin = new ObjectInputStream(fis);
  60.         jframeFirst = (FirstFrame) oin.readObject();
  61.         oin.close();
  62.     }
  63.  
  64.     private void stM(JTextField jtfm1, JTextField jtfm2) throws IOException {
  65.         sb = new StringBuffer();
  66.         result = new String();
  67.  
  68.         for (int i = 0; i < 10; i++) {
  69.             result = sb.append("i#" + i + "=" + jtfm1.getText() + "; ")
  70.                     .toString();
  71.             jtfm2.setText(result);
  72.         }
  73.  
  74.         FileOutputStream fos = new FileOutputStream("text.ser");
  75.         ObjectOutputStream oos = new ObjectOutputStream(fos);
  76.  
  77.         oos.writeObject(jframeFirst);
  78.         oos.flush();
  79.         oos.close();
  80.         }
  81.     }
Advertisement
Add Comment
Please, Sign In to add comment