有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何检查JFrame是否存在

我想创建一个jbutton,其中包含调用jframe的函数,但是如果jframe已经被调用并且存在,jbutton必须能够停止调用它。我们怎么能做到呢

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

        //step 1 - check if jframe already open/called or not

        //step 1.a - if true ignore, jbutton do nothing

        //step 1.2 - else if false,jbutton called for jframe

        ClassWithJFrame cwjFrame = new ClassWithJFrame()
        if(cwjFrame is exist){

          //DO NOTHING
        }
        else if(cwjFrame not exist){

         cwjFrame.setVisible(true);
        }
        }
    });

共 (1) 个答案

  1. # 1 楼答案

    使用^{}

    cwjFrame.isVisible() will return true if frame is already visible 
    

    if(cwjFrame.isVisible()){
    
          //DO NOTHING
     }
    else if(!cwjFrame.isVisible()){
    
         cwjFrame.setVisible(true);
    
    }