【发布时间】: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