java - 按下“确定”后如何打开新窗口?

标签 java swing user-interface

我有一段代码,一旦用户单击按钮,就会打开 JOptionPane 对话框。我想做的第一件事是在用户点击其中一个按钮后立即关闭第一个 JFrame。我尝试过这样做

setVisible(false); // Delete visibility 
        dispose(); //Delete window

但是按下按钮后原始 JFrame 不会立即关闭。目标是有两个按钮。当按下一个按钮时,显示一个 JOptionPane 框,同时同时关闭第一个 JFrame 窗口。我该如何做到这一点?接下来,在新的 JOptionPane 中按下“确定”后,我不想刺激 Action 监听器。我通过调用方法来做到这一点

sinceyoupressedthecoolbutton();

就在我的 JOptionPane 声明之后。这是第二个问题开始的地方,它完美地显示了 JOptionPane,但没有转到方法

sinceyoupressedthecoolbutton();

不知道问题是出在方法调用上还是方法内容上。基本上,在 JOptionPane 上按下 ok 后,我不想移动到另一个打开新 JLabel 的方法。

这是代码:

package Buttons;

import java.awt.Dimension;
import java.awt.FlowLayout; //layout proper
import java.awt.event.ActionListener; //Waits for users action
import java.awt.event.ActionEvent; //Users action
import javax.swing.JFrame; //Window
import javax.swing.JLabel;
import javax.swing.JButton; //BUTTON!!!
import javax.swing.JDialog;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane; //Standard dialogue box

public class ButtonClass extends JFrame {

private JButton regular;
private JButton custom;

public ButtonClass() { // Constructor
    super("The title"); // Title
    setLayout(new FlowLayout()); // Default layout

    regular = new JButton("Regular Button");
    add(regular);


    custom = new JButton("Custom", b);

    add(custom);

    Handlerclass handler = new Handlerclass();
    Otherhandlerclass original = new Otherhandlerclass();
    regular.addActionListener(handler);
    custom.addActionListener(original);

   //THIS WAS MY FIRST PROBLEM, I WANT TO CLOSE THE FIRST JFRAME WINDOW AS THE USER HITS OK
    setVisible(false); // Close the show message dialog box
    dispose();

}

public class Handlerclass implements ActionListener { // This class is
                                                        // inside the other
                                                        // class

    public void actionPerformed(ActionEvent eventvar) { // This will happen
                                                        // when button is
                                                        // clicked
        JOptionPane.showMessageDialog(null, String.format("%s", eventvar.getActionCommand())); //opens a new window with the name of the button
    }
}

public class Otherhandlerclass implements ActionListener {

    public void actionPerformed(ActionEvent e) {

        JOptionPane.showMessageDialog(null,"Since you pressed that button, I will open a new window when you press ok, okay?");

        //Code works up until here 

        sinceyoupressedthecoolbutton(); //THIS METHOD SHOULD OPEN A NEW WINDOW! 
    }

    public void sinceyoupressedthecoolbutton() {

        JDialog YES = new JDialog();
        JLabel label = new JLabel("Here is that new window I promised you!");
        add(label);
        YES.setDefaultCloseOperation(JDialog.EXIT_ON_CLOSE);
        YES.setSize(500, 500);
        YES.setVisible(true);

    }

    public class okay implements ActionListener {
        public void actionPerformed(ActionEvent ok) {

        }

    }

}

}

帮助将不胜感激!

最佳答案

您正在事件驱动的环境中工作,而不是线性处理环境。这意味着您运行一些代码,然后等待(等待已经为您完成)某个事件的发生,然后您对其做出响应...

在用户按下自定义按钮之前不会发生任何事情,因此在此之前尝试关闭框架是没有意义的

当您的 OtherhandlerclassactionPerformed 被触发时,显然您会显示 JOptionPane Pane ,这将阻止执行流程,直到它被触发为止。关闭。此时,您应该有机会处理原始窗口。

因此,与其在构造函​​数中调用 setVisible(false)dispose,最好将其移至 Otherhandlerclass

public class Otherhandlerclass implements ActionListener {

    public void actionPerformed(ActionEvent e) {

        dispose();
        JOptionPane.showMessageDialog(null, "Since you pressed that button, I will open a new window when you press ok, okay?");

        sinceyoupressedthecoolbutton();
    }

    public void sinceyoupressedthecoolbutton() {

        JDialog YES = new JDialog();
        JLabel label = new JLabel("Here is that new window I promised you!");
        YES.add(label);
        YES.setSize(500, 500);
        YES.setVisible(true);

    }

    public class okay implements ActionListener {

        public void actionPerformed(ActionEvent ok) {

        }

    }

}

话虽如此,我鼓励您阅读 The Use of Multiple JFrames, Good/Bad Practice?也许可以考虑使用类似 How to Use CardLayout 的东西相反

关于java - 按下“确定”后如何打开新窗口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32085208/

相关文章:

java - 更新进度条

Java 9 : Possible to have 2 modules with same name on module path

javascript - 使用ajax更新服务器从一个列表中选择对象并将它们移动到另一个列表中的好方法是什么?

java - 如何在运行时更改 JTextArea 的位置?

user-interface - NetBeans功能如何关闭

c++ - wxWidgets:如何改变和显示sizer的内容

Java单元测试用于不同的输入数据

java - 将对象的副本添加到 ArrayList

java - 更改流程布局中的面板大小

java - 如何停止和重新启动线程内的循环?