【问题标题】:JFrame not closing when it shouldJFrame在应该关闭的时候没有关闭
【发布时间】:2013-08-17 04:00:00
【问题描述】:

此代码段在 JFrame 的制作中被调用,当它到达 dispose() 行时它不会关闭。我知道它正在进入该块,因为另一个 JFrame 确实打开了,唯一的问题是它没有关闭。有人知道为什么吗?

    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();}          
}

这些只是代码的两个部分,主体是第一部分,反序列化部分是导致问题的部分

我很确定已经到达 dispose() 行,因为我只是在 dispose() 的正下方和下方放置了一个 System.out.print() 并且都打印出来了

【问题讨论】:

  • 听起来好像有东西阻塞了 EDT 或者框架的 DefaultCloseOperation 设置为 DO_NOTHING...
  • 根据代码sn-p很难知道。你可以看看Concurrency in Swing
  • 没关系,无论我如何启动程序,问题都会发生
  • 看起来你的程序控制移动到激活器窗口并且没有出现dispose()。只需做一件事,将dispose() 放在activator 实例之前。对不起我的英语不好。
  • 我会说@VighaneshGursale 是对的。您是否 100% 确定它到达 dispose()?此外,如果您将WindowListener 添加到 JFrame,它可能会阻止关闭。

标签: java swing serialization jframe


【解决方案1】:

在 JFrame 子类的构造函数中,首先调用 deserializeOpen() 来处理框架(没有效果,因为它尚未显示),然后调用 show() 来打开框架。所以你可能打算open-close,但是你的代码close-opens

顺便说一句:show() 方法自 JDK1.5 起已弃用,您应该改用 setVisible(true)。 我建议您在deserializeOpen() 中传播异常并将它们捕获到外面,这样您就可以决定是否打开框架而不是打开和关闭它:

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

在构造函数中:

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

【讨论】:

  • 谢谢你,这很好。而且我不知道,谢谢,我现在不会使用 show()。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-10-20
  • 1970-01-01
  • 2017-09-19
  • 2017-12-28
  • 2011-03-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多