Java 无法在更改的框架中显示面板

Java 无法在更改的框架中显示面板,java,swing,awt,Java,Swing,Awt,我正在开发一个JAVA桌面应用程序,它有两个框架,用户单击框架1上的按钮,基于所选选项的响应将更新到数据库。更新响应时,应显示第2帧 如果在数据库更新之前将第2帧设置为可见,则会显示第2帧,但内容为空,即不显示第2帧的面板。但一旦数据库更新完成,就会显示框架的内容 button1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { frame1

我正在开发一个JAVA桌面应用程序,它有两个框架,用户单击框架1上的按钮,基于所选选项的响应将更新到数据库。更新响应时,应显示第2帧

如果在数据库更新之前将第2帧设置为可见,则会显示第2帧,但内容为空,即不显示第2帧的面板。但一旦数据库更新完成,就会显示框架的内容

button1.addActionListener(new ActionListener()
{
    public void actionPerformed(ActionEvent e)
    {

        frame1.setVisible(false);
                frame2.setVisible(true);
                Utilities.updateToDB("clicked button1");    
            }
        });


第2帧的内容应在数据库更新进行时或之前显示。

对于这种特殊情况,我建议您使用:

A如和中所示

大致如下:

protected String doInBackground() throws Exception {
    try {
        Thread.sleep(5000); //Simulates long running task (your database update)
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    return "Updated"; //Returns the text to be set on your status JLabel once the update is done
}

@Override
protected void done() {
    super.done();
    try {
        // Here update your GUI or do whatever you want to do when the update is done.
        textfield.setText(get()); //Set to the textField the text given from the long running task
    } catch (InterruptedException | ExecutionException e) {
        e.printStackTrace();
    }
}
在您的
ActionListener
上,类似于:

private ActionListener listener = (e -> {
    frame2.setVisible(true); //suggested use of cardlayout and display nextCard here.
    worker.execute(); //Initializes long running task
});

但是我建议您不要使用多个
JFrames
:而是使用一个,

我建议您查看JDesktopPane。如果您仍然需要帮助,请提供一个更具体的例子?有关创建工作示例的提示,请参阅。1)请参阅2)“应在数据库更新进行时或之前显示第2帧。”不要阻止EDT(事件调度线程)。发生这种情况时,GUI将“冻结”。有关详细信息和修复方法,请参阅。