简体   繁体   English

JFrame应该在什么时候关闭

[英]JFrame not closing when it should

This code segment is called upon in the making of the JFrame, and when it reaches the dispose() line it does not close. 在制作JFrame时调用此代码段,当它到达dispose()行时,它不会关闭。 I know it is getting into that block because the other JFrame does open, the only problem is that it is not closing. 我知道它正在进入那个块,因为另一个JFrame确实打开了,唯一的问题是它没有关闭。 Anybody 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 segments of the code, the body is the first part and the deserialize one is the one that is causing the problem 这些只是代码的两个部分,正文是第一部分,反序列化是导致问题的部分

I'm pretty sure that the dispose() line is being reached because I just put a System.out.print() right about and below the dispose() and both printed out 我很确定到达dispose()行是因为我只是将一个System.out.print()放在dispose()的下方和下方,并且都打印出来

In the constructor of your JFrame subclass, you call deserializeOpen() first, which disposes the frame (no effect since it's not shown yet), and then call show() which opens the frame. 在你的JFrame子类的构造函数中,首先调用deserializeOpen() ,它释放帧(没有效果,因为它还没有显示),然后调用show()来打开框架。 So you may have intended to open-close , but instead your code close-opens . 所以你可能打算开关 ,而是你的代码关闭打开

By the way: the show() method is deprecated since JDK1.5, you should use setVisible(true) instead. 顺便说一句:自JDK1.5以来不推荐使用show()方法,你应该使用setVisible(true)代替。 I'd recommend that you propagate the exceptions in deserializeOpen() and catch them outside, that way you can decide whether or not to open the frame instead of opening and closing it: 我建议您在deserializeOpen()传播异常并将其捕获到外部,这样您就可以决定是否打开框架而不是打开和关闭框架:

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

in the constructor: 在构造函数中:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM