繁体   English   中英

单击按钮以更改可见性

[英]Clicking a button to change visibility

我试图将单击按钮时的可见性设置为false,但是编译器会显示“不兼容的类型”。 该错误发生在它说if (frame.setVisible(true))我使用了JFrameJButtonJLabelBorderLayoutActionEventActionListener

Object source = event.getSource();

        if (source == changeTextButton)
        {
            if (label.getText().equals(LABEL1))
            {
                label.setText(LABEL2);
            }
            else
            {
                label.setText(LABEL1);
            }
        }  // end of if (source == button)

        if (source == closeButton)
        {
            if (frame.setVisible(true))
            {
                setVisible(false);
            }
        } // end of if (source == closeButton)

frame.setVisible(true)不返回布尔结果,因此不能放置在if块的测试部分内。 请查看该API,您将看到它被声明为返回void -没有任何内容-因此,如果进行布尔检查,请勿将其放入其中。

要重新声明,根据Java API, setVisible方法签名如下所示:

// Window class
public void setVisible(boolean visible)

同样,该方法被声明为返回void,因此您的代码等效于执行以下操作:

if (void) {
  // do something
}

这对编译器没有意义,因为void既不是true也不是false。

您需要使用的是:

if(frame.isVisible()){
fram.setVisible(False);
}

frame.isVisible() returns a boolean (true or false)

您甚至可能不需要if statement ,只在按下closeButton时始终执行frame.setVisible(false)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM