JFrame does not close when it should

This code segment is called when the JFrame is created, and when it reaches the dispose () line, it does not close. I know that it falls into this block because another JFrame really opens, the only problem is that it does not close. Does anyone know why?

public LogIn(String title) { super(title); checker = new Open(""); deserializeOpen(); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(300, 300); getContentPane().setLayout(new BorderLayout()); getContentPane().setBackground(Color.orange); Login = new JButton("Login"); Create = new JButton("New Profile"); Login.addActionListener(this); Create.addActionListener(this); buttons = new JPanel(); buttons.setBackground(Color.orange); buttons.setLayout(new GridLayout(0,2)); buttons.add(Login); buttons.add(Create); Title = new JLabel("Scrambler"); Title.setFont(new Font("Times New Roman", Font.BOLD, 24)); Name = new JTextField(4); name = new JLabel("Name:"); password = new JPasswordField(4); pass = new JLabel("Password:"); Text = new JPanel(); Text.setLayout(new GridLayout(6,0)); Text.setBackground(Color.orange); Text.add(Title); Text.add(name); Text.add(Name); Text.add(pass); Text.add(password); getContentPane().add(Text, BorderLayout.CENTER); getContentPane().add(buttons, BorderLayout.SOUTH); show(); } public void deserializeOpen() { try { FileInputStream door = null; try { door = new FileInputStream("Check.ser"); } catch (FileNotFoundException e) { new Activator(); dispose(); } if(door!=null) { ObjectInputStream reader = new ObjectInputStream(door); checker = (Open) reader.readObject(); } } catch (IOException e){e.printStackTrace();} catch (ClassNotFoundException e){e.printStackTrace();} } 

these are just two code segments, the body is the first part, and deserialization is the one that causes the problem.

I am sure the dispose () line has been reached because I just placed System.out.print () directly and below the dispose () object and printed

+4
source share
1 answer

In the constructor of your JFrame subclass, deserializeOpen deserializeOpen() is first called, which provides the frame (no effect, since it has not yet been shown), and then calls show() , which opens the frame. That way, you might have intended to open-close, but instead, your code closes.

By the way: the show() method is deprecated with JDK1.5, use setVisible(true) instead. I recommend that you throw exceptions in deserializeOpen() and catch them externally, so you can decide whether to open or not open a frame, rather than opening and closing it:

 public void deserializeOpen() throws Exception { ... } 

in the constructor:

 try { deserializeOpen(); setVisible(true); } catch(Exception e) { e.printStacktrace(); // or any other error handling } 
0
source

Source: https://habr.com/ru/post/1497148/


All Articles