现在我无法弄清楚是什么导致了这个问题。
我写了一个小的(也许很傻!)字典程序,它加载一个序列化的哈希映射对象(其中存储了词义对)并显示用户输入的词的含义。
最初我让它完全加载对象然后显示 GUI,但加载对象需要 3 秒,所以我认为显示 gui 并让用户输入单词会更好,同时对象单独加载线程,如果用户更快地键入单词(即在加载对象之前),则会出现一个对话框,显示“正在加载..”,并且在加载对象并向用户显示含义时关闭。
问题是 - 只出现对话框,消息 - “正在加载......”没有出现,好像它是透明的......
继承人的代码:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.io.*;
class Dict implements Runnable {
HashMap hm = null;
JTextField ji;
JTextArea jo;
JFrame jfobj;
public void run() {
try {
FileInputStream fis = new FileInputStream("serialhm");
ObjectInputStream ois = new ObjectInputStream(fis);
Thread.currentThread().sleep(3000);// on an average it takes 3 secs to read the object, i made it sleep because the serialhm file i provided has only one word - "break"
hm = (HashMap) ois.readObject();
ois.close();
} catch (Exception e) {
System.exit(0);
}
}
public void print_meaning() {
final JDialog dial = new JDialog();
dial.setEnabled(true);
dial.setModal(false);
dial.setSize(200, 200);
dial.setLocationRelativeTo(null);
dial.getContentPane().add(new JLabel("loading......please wait"));
dial.pack();
if (hm == null) {
dial.setVisible(true);//is not showing the message,only the dialog box is shown
while (true) {
if (hm != null) {
dial.setVisible(false);
dial.dispose();
break;
}
}
}
String word = ji.getText();
String meaning = "";
ArrayList al1 = (ArrayList) hm.get(word);
if (al1 == null) {
JOptionPane.showMessageDialog(jfobj, "word unavailable");
ji.setText("");
jo.setText("");
} else {
for (int i = 0; i < al1.size(); i++) {
meaning = meaning + "\n" + (String) al1.get(i);
}
jo.setText(meaning + "\n");
ji.selectAll();
}
}
public void gui() {
jfobj = new JFrame();
Font font1 = new Font(Font.SERIF, Font.PLAIN, 17);
jo = new JTextArea(15, 30);
jo.setEditable(false);
jo.setFont(font1);
jo.setLineWrap(true);
jo.setWrapStyleWord(true);
JScrollPane scrollPane = new JScrollPane(jo, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
ji = new JTextField(30);
ji.setFont(font1);
GridBagLayout gridBag = new GridBagLayout();
Container contentPane = jfobj.getContentPane();
contentPane.setLayout(gridBag);
GridBagConstraints gridCons1 = new GridBagConstraints();
gridCons1.gridwidth = GridBagConstraints.REMAINDER;
gridCons1.fill = GridBagConstraints.HORIZONTAL;
contentPane.add(ji, gridCons1);
GridBagConstraints gridCons2 = new GridBagConstraints();
gridCons2.weightx = 1.0;
gridCons2.weighty = 1.0;
contentPane.add(scrollPane, gridCons2);
ji.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
print_meaning();
}
});
jfobj.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
jfobj.pack();
jfobj.setVisible(true);
}
public static void main(String[] args) throws Exception {
Dict dobj = new Dict();
Thread t = new Thread(dobj);
t.start();
dobj.gui();
}
}
这是具有序列化哈希图对象的文件('serialhm'): http ://www.mediafire.com/?rc5nda0qs8891xv
为简单起见,它只是一个单字哈希图,而这本词典中唯一的词是 - “break”
另外,请原谅我糟糕的设计。谢谢。
如果有人需要完整的 hashmap 对象序列化文件,我也会上传,它有 203000 字。