程序发布之后swingworker不能正常工作


一个窗体,初始化的时候需要读取数据库,根据查询结果进行进一步操作。代码如下,使用提示框代表窗体的初始化部分。


 public class TestFrame extends JFrame {
    public TestFrame() {
        this.setTitle("测试窗体");
        initGUI();
    }

    private void initGUI() {

        SwingWorker worker = new SwingWorker<java.util.List<ConditionTerm>, Void>() {
            @Override
            protected java.util.List<ConditionTerm> doInBackground() throws Exception {
                condition.setTypeFlag(ConditionTermType.NORMAL);
                ConditionTermDao dao = new ConditionTermDao();
                return dao.find();
            }

            @Override
            protected void done() {
                FlowLayout layout = new FlowLayout();
                layout.setAlignment(FlowLayout.LEFT);
                setLayout(layout);
                try {
                    JOptionPane.showMessageDialog(new JPanel(), "1");
                    java.util.List<ConditionTerm> conditionTerms = get();
                    JOptionPane.showMessageDialog(new JPanel(), conditionTerms.size());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (ExecutionException e) {
                    e.printStackTrace();
                }
                validate();
                repaint();
            }
        };
        worker.execute();
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            TestFrame t = new TestFrame();
            t.setVisible(true);
        });
    }
}

开发环境为intelliJ idea 13 ,debug模式下运行正常,即:能弹出提示框,显示“1”,然后显示查询结果的条数。打包之后只弹出一次提示框显示“1” ,然后就没有反应了。

更新

问题已经解决

解决思路:使用java -jar xx.jar运行打包之后的程序,查看运行过程,发现有异常抛出,根据异常信息,成功解决问题。

swing java

红色贝雷帽 11 years, 6 months ago

Your Answer