【问题标题】:Java Swing GUI - How to open multiple dialog boxes, one after another?Java Swing GUI - 如何一个接一个地打开多个对话框?
【发布时间】:2023-03-18 03:11:01
【问题描述】:

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

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

这是我的代码结构:

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();

    }


    }

}

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

但我希望它们一个接一个地运行——直到按下第一个对话框的 OK 按钮后才会出现第二个对话框,直到按下第二个对话框的 OK 按钮后才会出现第三个对话框。

我怎样才能做到这一点?

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

【问题讨论】:

    标签: java swing user-interface dialog


    【解决方案1】:

    CardLayout 是我用来解决类似问题的。

    它就像一副牌,你可以一个接一个地展示。

    【讨论】:

    【解决方案2】:

    前段时间我遇到了类似的问题。我开发了小型库UiBooster。使用 UiBooster,您可以创建阻止对话框来询问用户不同的输入。

    String opinion = new UiBooster().showTextInputDialog("What do you think?");
    

    【讨论】:

      猜你喜欢
      • 2013-01-18
      • 2013-02-09
      • 2016-12-23
      • 1970-01-01
      • 1970-01-01
      • 2015-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多