When you press the Register button again, the elements are duplicated. Probyval through dispose, but all the same. How to fix this error?

package Client; import Client.packet.PackerAuthorize; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.DataInputStream; import java.io.IOException; public class Authorization extends JFrame { static JTextField smallField; JTextField bigField; public Authorization() { smallField = new JTextField(12); smallField.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(Authorization.this, "Ваше слово: " + smallField.getText()); } }); final JPasswordField password = new JPasswordField(12); password.setEchoChar('*'); JPanel contents = new JPanel(new FlowLayout(FlowLayout.CENTER)); contents.add(smallField); contents.add(password); setContentPane(contents); setSize(400, 100); setLocation(800, 200); setTitle("authorization"); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); JPanel jpBotton = new JPanel(new FlowLayout(FlowLayout.LEFT)); add(jpBotton, BorderLayout.SOUTH); JButton jbStart = new JButton("Login"); JButton jbExite = new JButton("Register"); jpBotton.add(jbStart); jpBotton.add(jbExite); jbStart.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { ClientLoader.sendPacket(new PackerAuthorize(smallField.getText(), password.getText())); try { DataInputStream dis = null; try { dis = new DataInputStream(ClientLoader.getSocket().getInputStream()); } catch (IOException e1) { e1.printStackTrace(); } if (dis.available() <= 0) { if (dis.readUTF().equals("error")) { JOptionPane.showMessageDialog(null, "Error!"); System.out.println("error"); } else { new Chat(); setVisible(false); System.out.println("successful"); } try { Thread.sleep(10); } catch (InterruptedException ex) {} } } catch (IOException ex) {} } }); jbExite.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { Register log = new Register(); setVisible(false); } }); setVisible(true); } public static String getNickname() { return smallField.getText(); } } 

 package Client; import Client.packet.PackerRegistration; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.DataInputStream; import java.io.IOException; import java.util.HashMap; import java.util.Map; public class Register { public static JFrame frame = new JFrame("BoxLayoutTest"); public static void createUI(Container container) { final JButton button = new JButton("Registration"); //static что бы закрыть JButton calc = new JButton("Calc_test"); //static что бы закрыть button.setAlignmentY(Component.CENTER_ALIGNMENT); calc.setAlignmentY(Component.CENTER_ALIGNMENT); final JTextField smallField, password, repitPassword; smallField = new JTextField(12); password = new JTextField(12); repitPassword = new JTextField(12); container.setLayout(new BoxLayout(container, BoxLayout.Y_AXIS)); container.add(new JLabel("Logim:")); container.add(smallField); container.add(new JLabel("Password :")); container.add(password); container.add(new JLabel("Repit password :")); container.add(repitPassword); container.add(button); container.add(calc); calc.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { new Chat(); } }); button.addActionListener(new ActionListener() { @Override public int hashCode() { return super.hashCode(); } @Override public void actionPerformed(ActionEvent e) { if (e.getSource() == button) { Map < Integer, String > states = new HashMap < Integer, String > (); states.put(1, smallField.getText()); states.put(2, password.getText()); states.put(3, repitPassword.getText()); System.out.println(ClientLoader.getSocket()); ClientLoader.sendPacket(new PackerRegistration(states.get(1), states.get(2), states.get(3))); try { DataInputStream dis = new DataInputStream(ClientLoader.getSocket().getInputStream()); if (dis.available() <= 0) { if (dis.readUTF().equals("error")) { JOptionPane.showMessageDialog(null, "Error!"); System.out.println("error"); } else { frame.setVisible(false); Authorization p = new Authorization(); } try { Thread.sleep(10); } catch (InterruptedException ex) {} } } catch (IOException ex) {} } } }); } public Register() { frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); createUI(frame.getContentPane()); SwingUtilities.invokeLater(new Runnable() { public void run() { // Открытие окна frame.setLocation(800, 200); frame.setTitle("authorization"); frame.pack(); frame.setVisible(true); } }); } } 

enter image description here

enter image description here

  • clicked on the registration button, registered. Wanted to register another entry, and the window is duplicated - Kostya Chernyaev
  • you have in vain hooked on the createui method, it is not for that. put a bryak there, he invokes you many times - Stranger in the Q
  • And what will happen if you press the button with a cross? - Roman C

0