如何设置文本字段在框架中不可见

问题描述:

我正在使用 Swing 框架,我有一个问题.

I am using Swing framework, and I have one question.

地址面板是动态添加到主框架的.我想从地址面板上的主框架调用 visible(false) 方法.

The Address panel is dynamically added to the main frame. I want to call the visible(false) method from the main frame on the Address Panel.

您需要做的是将 JTextField 存储为 AddressPanel 的私有成员.并且,在AddressPanel 中,添加一个名为hideTextField() 的方法.然后,在该方法中调用私有 JTextField 成员上的 setVisible(false) 方法.

What you need to do is store the JTextField as a private member of the AddressPanel. And, in AddressPanel, add a method called hideTextField(). Then, in that method call the setVisible(false) method on the private JTextField member.

代码可能类似于以下内容:

The code may look similar to the following:

public class AddressPanel {

    private JTextField textFieldToHide;

    public void hideTextField(){
        textFieldToHide.setVisible(false);
    }
}

然后,在主框架中像这样使用它:

Then, in the main frame use it like so:

addressPanel.hideTextField();