首页 > 解决方案 > Java:如何暂停线程执行,直到 JFrame 被释放

问题描述

我需要在 JFrame 的帮助下处理错误情况:

catch (FormatterException e) {
   displayJFrame();
   System.out.println("Execution continues");
}

基本上,在 JFrame 中,我会手动输入在 textarea 中失败的数据并单击 JButton(为简洁起见,这只是 JFrame 和 JButton):

static void displayJFrame() {
    JFrame frame = new JFrame("JFrame recovery window");

    // create our jbutton
    JButton showDialogButton = new JButton("Submit");

    // add the listener to the jbutton to handle the "pressed" event
    showDialogButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            frame.dispose();
        }
    });

    // put the button on the frame
    frame.getContentPane().setLayout(new FlowLayout());
    frame.add(showDialogButton);

    // set up the jframe, then display it
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame.setPreferredSize(new Dimension(300, 200));
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
} 

我需要的是,当按下 Button 时,会释放框架并继续执行主线程。不幸的是,它不是那样工作的。主线程在处理帧之前打印“执行继续”。有没有办法在 Swing/AWT API 中实现这一点?

谢谢

标签: javaswing

解决方案


推荐阅读