调用setVisible时,组件不会显示(true)

时间:2012-05-30 11:07:08

标签: java swing

现在我无法弄清楚是什么导致了这个问题。

我写了一个小的(也许是愚蠢的!)字典程序,它加载了一个序列化的哈希映射对象(其中存有单词 - 意义对),并显示用户输入的单词的含义。

最初我让它完全加载对象然后显示GUI,但是加载对象需要3秒钟,所以我认为最好显示gui并让用户输入单词,同时加载对象在一个单独的线程中,如果用户输入更快的单词(即在加载对象之前),会出现一个对话框,说“正在加载...”,当对象加载并显示用户的含义时,它会关闭。

问题是 - 只出现对话框,消息 - “loading ...”没有出现,好像它是透明的......

下面是代码:

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

    }
}

这是包含序列化hashmap对象的文件('serialhm'): http://www.mediafire.com/?rc5nda0qs8891xv

为简单起见,它只是一个单词的hashmap而且这个词典中唯一的单词是 - “break”

另外,请原谅我糟糕的设计。谢谢。

如果有人需要完整的hashmap对象序列化文件,我也会上传它,它有203000个单词。

1 个答案:

答案 0 :(得分:0)

好的,将while(true)块移动到新线程并进行微小更改,现在很好,正如我想要的那样运行。