The essence of the algorithm is as follows: when you click on the fill in form button, a new form opens to fill in the previous one, that is, the window becomes 2. As soon as I filled out the form for the second one and want to close it and go to the previous one by clicking on "Return to form", it opens A new window with already entered data, but the problem is that the previous one remains open, that is, the first two forms are obtained, which are filled from the second and this is bad. It is necessary when you click on return to form to close the previous window, but when you click. That is, not when clicking on the button to fill out the form when switching to form2.java from form1.java, namely when moving from form2.java so that the previous form1.java closes and the new form1.java opens. How to implement it I have no ideas. Vindovistener not helped.
There is form1.java
package zapol; import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTextField; public class form1 extends JFrame implements ActionListener{ JButton btn1=new JButton("Open the form"); static JTextField txt1=new JTextField(3); static JTextField txt2=new JTextField(10); JScrollPane scr=new JScrollPane(txt2); public static void main(String[] args) { form1 formw=new form1(); } form1() { setVisible(true); setLayout(new FlowLayout()); setSize(200,150); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); add(btn1); btn1.addActionListener(this); add(txt1); add(txt2); scr.setPreferredSize(new Dimension(450, 110)); add(scr, BorderLayout.CENTER); }public void actionPerformed(ActionEvent e) { if(e.getSource()==btn1) { form2 formt=new form2(); } } } And form2.java
package zapol; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JTextField; public class form2 extends JFrame implements ActionListener{ JButton btn1=new JButton("Return to form"); JTextField txt1=new JTextField(10); JButton btn2=new JButton("Feel form"); public static void main(String[] args) { form2 formw=new form2(); } form2() { setVisible(true); setLayout(new FlowLayout()); setSize(200,150); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); add(btn1); btn1.addActionListener(this); add(btn2); btn2.addActionListener(this); add(txt1); }public void actionPerformed(ActionEvent e) { if(e.getSource()==btn1) { form1 formw=new form1(); dispose(); } if(e.getSource()==btn2) {String anys=form1.txt2.getText(); form1.txt2.setText(form1.txt2.getText()+txt1.getText()+" , "); txt1.setText(""); } } }