setVisible(false) to a group of JTextField and JLabel

与世无争的帅哥 提交于 2019-12-11 08:46:01

问题


I have a group of JTextField and JLabel. I want them to initially not be visible so I thought to initialize my applet with a method which calls setVisible(false) for each of the components.

Is it possible to create a method setVisible(false) which will set the visibility of all the components to false. Finally if I have 40 components in the applet, is it possible to do this with only one command instead of 40 commands?


回答1:


Add your buttons and labels to a JPanel and then you can simply make your JPanel invisible to hide them all with one call.

jPanel.setVisible(false);

Alternatively, add your buttons and labels to a JComponent list, and then loop through it:

List<JComponent> list = new ArrayList<JComponent>();
list.add(button);
list.add(label);
for(JComponent c : list){
    c.setVisible(false);
}


来源:https://stackoverflow.com/questions/5168030/setvisiblefalse-to-a-group-of-jtextfield-and-jlabel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!