简体   繁体   English

java-选择框架中的所有对象

[英]java - selecting all objects in a frame

i have this: 我有这个:

jLabel1.setBorder(null);
jLabel2.setBorder(null);
jLabel3.setBorder(null);
jLabel4.setBorder(null);
jLabel5.setBorder(null);
jLabel6.setBorder(null);

i want to make it simpler and less noob... any ideas? 我想简化它,减少新手...有什么想法吗?

Try 尝试

Component[] components = frame.getContentPane().getComponents();
for (Component component : components) {
   if (component instanceof JComponent) {
       ((JComponent) component).setBorder(null);
   }
}

If you want only JLabel s, not all components to have a null border, change the instanceof check and the cast to JLabel 如果只希望JLabel而不是所有组件都具有空边框,请更改instanceof check并将其强制转换为JLabel

To include the comment on your answer by camickr, a JLabel doesn't have a border by default, so you don't have to do anything. 为了包括camickr对您的答案的评论, JLabel默认情况下没有边框,因此您无需执行任何操作。 You should do this only in case you have assigned a border at some point and want to get rid of it. 仅当您在某个时候指定了边界并希望摆脱边界时,才应这样做。

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

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