Java 程序在启动子窗体时没有响应

Java 程序在启动子窗体时没有响应,java,swing,concurrency,Java,Swing,Concurrency,这是一个启动新窗口(子窗体)的应用程序。提交此子表单时,应该处理返回的数据并更新父表单。但是,当启动子窗体时,程序不响应。有人能解释一下为什么会发生这种情况,我该如何解决 public class FormCondition { private JLabel nameLabel; private JTextField name; private JButton create; private Lock lock; private Condition co

这是一个启动新窗口(子窗体)的应用程序。提交此子表单时,应该处理返回的数据并更新父表单。但是,当启动子窗体时,程序不响应。有人能解释一下为什么会发生这种情况,我该如何解决

public class FormCondition 
{
    private JLabel nameLabel;
    private JTextField name;
    private JButton create;
    private Lock lock;
    private Condition cond;
    private String str;
    public FormCondition()
    {   
        lock = new ReentrantLock();
        cond = lock.newCondition();
        create = new JButton("Create");
        nameLabel = new JLabel("Name");
        name = new JTextField();
        name.setEditable(false);
        create.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                if (e.getSource() == create)
                {   
                    try
                    {
                        lock.lock();
                        try
                        {
                            Runnable r = new Runnable()
                            {
                                @Override
                                public void run() 
                                {
                                    System.out.println("Starting a new Thread");
                                    new newForm();
                                }
                            };

                            new Thread(r).start();
                            System.out.println("about to wait");
                            cond.await();
                            name.setText(str);
                        }
                        finally
                        {
                            lock.unlock();
                        }
                    }
                    catch (InterruptedException e1)
                    {
                        // empty
                    }
                }
            }
        });
        JFrame frame = new JFrame("Main form");
        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(0, 2));
        panel.add(nameLabel);
        panel.add(name);
        panel.add(create);
        frame.add(panel);
        frame.setLocationRelativeTo(null);
        frame.setSize(200, 200);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   
    }

    public void setName(String name)
    {
        str = name;
        System.out.println("Name received is: " + str);
    }

    class newForm implements ActionListener
    {
        private JLabel nameLabel;
        private JTextField name;
        private JButton go;
        public newForm()
        {
            nameLabel = new JLabel("Name");
            name = new JTextField();
            go = new JButton("Go");
            JFrame frame=new JFrame("Second form");
            JPanel panel=new JPanel();
            panel.setLayout(new GridLayout(0,2));
            panel.add(nameLabel);
            panel.add(name);
            panel.add(go);
            frame.add(panel);
            frame.setLocationRelativeTo(null);
            frame.setSize(200,200);
            frame.setVisible(true);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
        public void actionPerformed(ActionEvent e) 
        {
            if (e.getSource() == go)
            {
                lock.lock();
                try
                {
                    String string = name.getText();
                    System.out.println("Name entered is: " + str);
                    setName(str);
                    cond.signal();
                    System.out.println("Signalled");
                }
                finally
                {
                    lock.unlock();
                }

            }
        }
    }

    public static void main(String[] str)
    {
        new FormCondition();    
    }
}

您不应该调用像
cond.await()这样的东西(在事件调度线程上)。这就是UI挂起的原因。此外,即使这没有关系,也不要像捕获
中断异常时那样使用空的catch块,正如Peter在从UI线程中删除等待部分并移动到BlockingMethod()时回答的那样它是有效的。而且锁应该在该方法中重新获得,并且应该在处理完成后释放

 public class FormCondition 
    {
        private JLabel nameLabel;
        private JTextField name;
        private JButton create;
        private ReentrantLock lock;
        private Condition cond;
        private String str;
        public FormCondition()
    {   
        lock=new ReentrantLock();
        cond=lock.newCondition();
        create=new JButton("Create");
        nameLabel=new JLabel("Name");
        name=new JTextField();
        name.setEditable(false);
        create.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent e) {
                if(e.getSource()==create)
                {   
                    try
                    {
                        lock.lock();
                        System.out.println("Lock acquired");
                        System.out.println("Starting a new Thread");
                        System.out.println("Sleep for two minutes");
                        Thread.sleep(2000);
                        System.out.println("Awake now");
                        new newForm();
                        lock.unlock();
                        blockingMethod();




                    }
                    catch(InterruptedException e1)
                    {
                    e1.getMessage() ;

                    }


                }

            }});
        JFrame frame=new JFrame("Main form");
        JPanel panel=new JPanel();
        panel.setLayout(new GridLayout(0,2));
        panel.add(nameLabel);
        panel.add(name);
        panel.add(create);
        frame.add(panel);
        frame.setLocationRelativeTo(null);
        frame.setSize(400,200);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   
    }



    public void blockingMethod()
    {
        Runnable r2=new Runnable(){

            @Override
            public void run() 
            {

                try 
                {   lock.lock(); 
                    System.out.println("about to wait");
                    cond.await();//lock released temporarily
                    System.out.println("waiting done...");
                    //invoke setName after reaquiring lock
                    lock.unlock();
                }
                catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }


            }};
            try
            {
                    lock.lock();
                    System.out.println("Still has the Lock ");
                    new Thread(r2).start();
                    lock.unlock();


            }
            catch(Exception e1)
            {
            e1.getMessage() ;

            }

    }

    public void setName(String name)
    {

    str=name;
    this.name.setText(str);
    System.out.println("Name received is: "+str);
    }

    class newForm  implements ActionListener
    {
    private JLabel nameLabel;
    private JTextField name;
    private JButton go;
    public newForm()
    {
    nameLabel=new JLabel("Name");
    name=new JTextField();
    go=new JButton("Go");
    go.addActionListener(this);
    JFrame frame=new JFrame("Second form");
    JPanel panel=new JPanel();
    panel.setLayout(new GridLayout(0,2));
    panel.add(nameLabel);
    panel.add(name);
    panel.add(go);
    frame.add(panel);
    frame.setLocationRelativeTo(null);
    frame.setSize(300,200);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


    }
    public void actionPerformed(ActionEvent e) 
    {

        if(e.getSource()==go)
        {
            System.out.println("waiting for the lock");
             lock.lock();
             System.out.println("Lock acquired");

                 try
                 {

                  String string= name.getText();
                  System.out.println("Name entered is: "+string);
                  setName(string);
                  cond.signalAll();
                  System.out.println("Signalled");
                 }

                 finally
                 {
                     lock.unlock();
                     System.out.println("Lock released");

                 }




        }

    }
    }

    public static void main(String[] str)
    {
    new FormCondition();    
    }
    }

看来有人需要仔细阅读这本书。您只能从EDT创建和操作Swing类。这段代码违反了这一点。请看,您似乎试图获得
JDialog
的行为,尽管我建议将其设置为模态,以实际阻止用户访问主窗体,直到对话框被取消。