java-选择帧中的所有对象

java-选择帧中的所有对象,java,user-interface,collections,Java,User Interface,Collections,我有这个: jLabel1.setBorder(null); jLabel2.setBorder(null); jLabel3.setBorder(null); jLabel4.setBorder(null); jLabel5.setBorder(null); jLabel6.setBorder(null); 我想让它更简单,少一些麻烦。。。有什么想法吗?试试看 Component[] components = frame.getContentPane().getComponents(); f

我有这个:

jLabel1.setBorder(null);
jLabel2.setBorder(null);
jLabel3.setBorder(null);
jLabel4.setBorder(null);
jLabel5.setBorder(null);
jLabel6.setBorder(null);
我想让它更简单,少一些麻烦。。。有什么想法吗?

试试看

Component[] components = frame.getContentPane().getComponents();
for (Component component : components) {
   if (component instanceof JComponent) {
       ((JComponent) component).setBorder(null);
   }
}
如果只希望
JLabel
s,而不是所有组件都有空边框,请将
instanceof
复选框和强制转换更改为
JLabel


要包含camickr对您答案的评论,默认情况下,
JLabel
没有边框,因此您无需执行任何操作。只有当您在某个点分配了边界并希望将其删除时,才应执行此操作。

JLabel默认情况下没有边界,因此您无需执行任何操作。谢谢,这肯定会起作用!我确实有边界,我不想用手写出所有的组件。