Java Swing GUI - 如何打开多个对话框,一个接一个? [英] Java Swing GUI - How to open multiple dialog boxes, one after another?

查看:369
本文介绍了Java Swing GUI - 如何打开多个对话框,一个接一个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个应用程序,通过提供一步一步的指示帮助用户浏览网站。

I am building an application which helps a user navigate a website by giving step by step instructions.

说明以对话框的形式给出。我正在使用Java Swing创建GUI对话框。

The instructions are given in the form of dialog boxes. I am using Java Swing to create the GUI dialog boxes.

这是我的代码的结构:

MainClass
{
    //some selenium code to access the website 'Google.com'.....

    InstructionDialog frame1 = new InstructionDialog("Enter 'Hello' in search field");
    frame1.setVisible(true);

    InstructionDialog frame2 = new InstructionDialog("Click 'Search' button");
    frame2.setVisible(true);

    InstructionDialog frame3 = new InstructionDialog("'Hello' is displayed in the results");
    frame3.setVisible(true);


}


class InstructionDialog extends JFrame {


    public String message;
    public static javax.swing.JButton btnOk;


    public InstructionDialog(String message)
    {

        //some code for the content pane //

        msgArea = new JTextArea();
        msgArea.setBounds(12, 13, 397, 68);
        contentPane.add(msgArea);
        msgArea.setEditable(false);
        simpleStepMessage.msgArea.setText(message);


        btnOk = new JButton("OK");
        btnOk.setBounds(323, 139, 97, 25);
        contentPane.add(btnOk);
        btnOk.addActionListener(new java.awt.event.ActionListener() 
        {
            public void actionPerformed(java.awt.event.ActionEvent evt) 
            {
                OkBtnActionPerformed(evt);
            }
        });


    public void OkBtnActionPerformed(java.awt.event.ActionEvent evt)
    {
        this.dispose();

    }


    }

}

当我运行这个问题是所有3个实例的InstructionDialog同时运行。所以我同时弹出所有3个对话框。

The problem when I run this is that all the 3 instances of the InstructionDialog run simultaneously. So I have all the 3 dialog boxes pop up at the same time.

但是我希望他们一个接一个地运行 - 第二个对话框不应该出现,直到OK第一个按钮被按下,第三个对话框不应该出现,直到按下第二个按钮的确定按钮。

But I want them to run one after another - the second dialog box should not appear until the OK button of the first is pressed, the third dialog box should not appear until the OK button of the second one is pressed.

我该如何实现?

任何帮助将不胜感激。谢谢。

Any help will be appreciated. Thanks.

推荐答案

CardLayout 是我用于类似问题的东西。

The CardLayout is something I used for similar problems.

它就像一张卡牌,你可以显示一个另一个。

It is like a card deck and you can display one after another.

这篇关于Java Swing GUI - 如何打开多个对话框,一个接一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆