在Java中更改特定类型的所有对象的属性

时间:2015-08-28 19:55:57

标签: java swing netbeans properties

我的课程包含javax.swing.JButtonjavax.swing.JTextField

但是,在实例化类之后,我需要更改某些类型的属性。

我正在尝试使用this.getComponents(),但正在返回null

这是我的代码:

    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package test;
    import java.awt.Component;
    import java.awt.Font;

    /**
     *
     * @author Usuario
     */
    public class NewJFrame extends javax.swing.JFrame {

        /**
         * Creates new form NewJFrame
         */
        public NewJFrame() {
          initComponents();
          Component[] component = this.getComponents();
          System.out.println("this.getName():"+this.getName());
          System.out.println("this.getClass().getName():"+this.getClass().getName());
          System.out.println("component.length:"+component.length);
          for(int i=0; i<component.length; i++) {
            System.out.println("--component[i].getName()"+component[i].getName());
            if (component[i] instanceof javax.swing.JTextField) {
              javax.swing.JTextField jtf = (javax.swing.JTextField)component[i];
              System.out.println(jtf.getName());
              jtf.setFont(new Font("Serif",Font.PLAIN, 36));
            }
          }
          UIManager.getLookAndFeelDefaults().put("TextField.font", new Font("Serif",Font.PLAIN, 36));
          //UIManager.getDefaults().put("TextField.font", new Font("Serif",Font.PLAIN, 36));
          //UIManager.put("TextField.font", new Font("Serif",Font.PLAIN, 36));
          //this.updateUI();  // Is not suitable for javax.swing.JFrame
          this.pack();
        }

        /**
         * This method is called from within the constructor to initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is always
         * regenerated by the Form Editor.
         */
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
        private void initComponents() {

            jtpBot = new javax.swing.JTabbedPane();
            jpOne = new javax.swing.JPanel();
            jpOneInner = new javax.swing.JPanel();
            jbFirst = new javax.swing.JButton();
            jtfOne = new javax.swing.JTextField();
            jpOnePlot = new javax.swing.JPanel();
            jpTwo = new javax.swing.JPanel();
            jpTwoInner = new javax.swing.JPanel();
            jbTwo = new javax.swing.JButton();
            jtfTwo = new javax.swing.JTextField();
            jpTwoPlot = new javax.swing.JPanel();
            jpThree = new javax.swing.JPanel();
            jpThreeInner = new javax.swing.JPanel();
            jbThree = new javax.swing.JButton();
            jtfThree = new javax.swing.JTextField();
            jpThreePlot = new javax.swing.JPanel();

            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            setBackground(new java.awt.Color(255, 255, 255));

            jtpBot.setBackground(new java.awt.Color(0, 0, 0));
            jtpBot.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));

            jpOne.setBackground(new java.awt.Color(0, 191, 191));

            jpOneInner.setBackground(new java.awt.Color(255, 0, 0));
            jpOneInner.setBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED));

            jbFirst.setText("First");
            jbFirst.setMaximumSize(new java.awt.Dimension(70, 23));
            jbFirst.setMinimumSize(new java.awt.Dimension(70, 23));
            jbFirst.setPreferredSize(new java.awt.Dimension(70, 23));

            jtfOne.setMaximumSize(new java.awt.Dimension(80, 20));
            jtfOne.setMinimumSize(new java.awt.Dimension(80, 20));
            jtfOne.setPreferredSize(new java.awt.Dimension(80, 20));

            jpOnePlot.setBackground(new java.awt.Color(191, 191, 191));
            jpOnePlot.setBorder(javax.swing.BorderFactory.createEtchedBorder());

            javax.swing.GroupLayout jpOnePlotLayout = new javax.swing.GroupLayout(jpOnePlot);
            jpOnePlot.setLayout(jpOnePlotLayout);
            jpOnePlotLayout.setHorizontalGroup(
                jpOnePlotLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 181, Short.MAX_VALUE)
            );
            jpOnePlotLayout.setVerticalGroup(
                jpOnePlotLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 0, Short.MAX_VALUE)
            );

            javax.swing.GroupLayout jpOneInnerLayout = new javax.swing.GroupLayout(jpOneInner);
            jpOneInner.setLayout(jpOneInnerLayout);
            jpOneInnerLayout.setHorizontalGroup(
                jpOneInnerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jpOneInnerLayout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jbFirst, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(jtfOne, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(jpOnePlot, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addContainerGap())
            );
            jpOneInnerLayout.setVerticalGroup(
                jpOneInnerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jpOneInnerLayout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(jpOneInnerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jpOneInnerLayout.createSequentialGroup()
                            .addGroup(jpOneInnerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jbFirst, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jtfOne, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGap(0, 117, Short.MAX_VALUE))
                        .addComponent(jpOnePlot, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addContainerGap())
            );

            javax.swing.GroupLayout jpOneLayout = new javax.swing.GroupLayout(jpOne);
            jpOne.setLayout(jpOneLayout);
            jpOneLayout.setHorizontalGroup(
                jpOneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jpOneLayout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jpOneInner, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addContainerGap())
            );
            jpOneLayout.setVerticalGroup(
                jpOneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jpOneLayout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jpOneInner, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addContainerGap())
            );

            jtpBot.addTab("One", jpOne);

            jpTwo.setBackground(new java.awt.Color(191, 0, 191));

            jpTwoInner.setBackground(new java.awt.Color(0, 255, 0));
            jpTwoInner.setBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED));

            jbTwo.setText("Two");
            jbTwo.setMaximumSize(new java.awt.Dimension(70, 23));
            jbTwo.setMinimumSize(new java.awt.Dimension(70, 23));
            jbTwo.setPreferredSize(new java.awt.Dimension(70, 23));

            jtfTwo.setMaximumSize(new java.awt.Dimension(80, 20));
            jtfTwo.setMinimumSize(new java.awt.Dimension(80, 20));
            jtfTwo.setPreferredSize(new java.awt.Dimension(80, 20));

            jpTwoPlot.setBackground(new java.awt.Color(191, 191, 191));
            jpTwoPlot.setBorder(javax.swing.BorderFactory.createEtchedBorder());

            javax.swing.GroupLayout jpTwoPlotLayout = new javax.swing.GroupLayout(jpTwoPlot);
            jpTwoPlot.setLayout(jpTwoPlotLayout);
            jpTwoPlotLayout.setHorizontalGroup(
                jpTwoPlotLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 181, Short.MAX_VALUE)
            );
            jpTwoPlotLayout.setVerticalGroup(
                jpTwoPlotLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 0, Short.MAX_VALUE)
            );

            javax.swing.GroupLayout jpTwoInnerLayout = new javax.swing.GroupLayout(jpTwoInner);
            jpTwoInner.setLayout(jpTwoInnerLayout);
            jpTwoInnerLayout.setHorizontalGroup(
                jpTwoInnerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jpTwoInnerLayout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jbTwo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(jtfTwo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(jpTwoPlot, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addContainerGap())
            );
            jpTwoInnerLayout.setVerticalGroup(
                jpTwoInnerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jpTwoInnerLayout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(jpTwoInnerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jpTwoPlot, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(jpTwoInnerLayout.createSequentialGroup()
                            .addGroup(jpTwoInnerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jbTwo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jtfTwo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGap(0, 117, Short.MAX_VALUE)))
                    .addContainerGap())
            );

            javax.swing.GroupLayout jpTwoLayout = new javax.swing.GroupLayout(jpTwo);
            jpTwo.setLayout(jpTwoLayout);
            jpTwoLayout.setHorizontalGroup(
                jpTwoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jpTwoLayout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jpTwoInner, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addContainerGap())
            );
            jpTwoLayout.setVerticalGroup(
                jpTwoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jpTwoLayout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jpTwoInner, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addContainerGap())
            );

            jtpBot.addTab("Two", jpTwo);

            jpThree.setBackground(new java.awt.Color(191, 191, 0));

            jpThreeInner.setBackground(new java.awt.Color(0, 0, 255));
            jpThreeInner.setBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED));

            jbThree.setText("Three");
            jbThree.setMaximumSize(new java.awt.Dimension(70, 23));
            jbThree.setMinimumSize(new java.awt.Dimension(70, 23));
            jbThree.setPreferredSize(new java.awt.Dimension(70, 23));

            jtfThree.setMaximumSize(new java.awt.Dimension(80, 20));
            jtfThree.setMinimumSize(new java.awt.Dimension(80, 20));
            jtfThree.setPreferredSize(new java.awt.Dimension(80, 20));

            jpThreePlot.setBackground(new java.awt.Color(191, 191, 191));
            jpThreePlot.setBorder(javax.swing.BorderFactory.createEtchedBorder());

            javax.swing.GroupLayout jpThreePlotLayout = new javax.swing.GroupLayout(jpThreePlot);
            jpThreePlot.setLayout(jpThreePlotLayout);
            jpThreePlotLayout.setHorizontalGroup(
                jpThreePlotLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 181, Short.MAX_VALUE)
            );
            jpThreePlotLayout.setVerticalGroup(
                jpThreePlotLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 0, Short.MAX_VALUE)
            );

            javax.swing.GroupLayout jpThreeInnerLayout = new javax.swing.GroupLayout(jpThreeInner);
            jpThreeInner.setLayout(jpThreeInnerLayout);
            jpThreeInnerLayout.setHorizontalGroup(
                jpThreeInnerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jpThreeInnerLayout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jbThree, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(jtfThree, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(jpThreePlot, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addContainerGap())
            );
            jpThreeInnerLayout.setVerticalGroup(
                jpThreeInnerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jpThreeInnerLayout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(jpThreeInnerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jpThreePlot, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(jpThreeInnerLayout.createSequentialGroup()
                            .addGroup(jpThreeInnerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jbThree, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jtfThree, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGap(0, 117, Short.MAX_VALUE)))
                    .addContainerGap())
            );

            javax.swing.GroupLayout jpThreeLayout = new javax.swing.GroupLayout(jpThree);
            jpThree.setLayout(jpThreeLayout);
            jpThreeLayout.setHorizontalGroup(
                jpThreeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jpThreeLayout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jpThreeInner, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addContainerGap())
            );
            jpThreeLayout.setVerticalGroup(
                jpThreeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jpThreeLayout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jpThreeInner, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addContainerGap())
            );

            jtpBot.addTab("Three", jpThree);

            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jtpBot)
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(69, 69, 69)
                    .addComponent(jtpBot)
                    .addContainerGap())
            );

            pack();
        }// </editor-fold>                        

        /**
         * @param args the command line arguments
         */
        public static void main(String args[]) {
            /* Set the Nimbus look and feel */
            //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
            /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
             * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
             */
            try {
                for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                    if ("Nimbus".equals(info.getName())) {
                        javax.swing.UIManager.setLookAndFeel(info.getClassName());
                        break;
                    }
                }
            } catch (ClassNotFoundException ex) {
                java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            }
            //</editor-fold>

            /* Create and display the form */
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new NewJFrame().setVisible(true);
                }
            });
        }

        boolean bCanIuseOne = false;
        boolean bCanIuseTwo = false;
        boolean bThreadFree = true;
        boolean bMouseFree = false;

        // Variables declaration - do not modify                     
        private javax.swing.JButton jbFirst;
        private javax.swing.JButton jbThree;
        private javax.swing.JButton jbTwo;
        private javax.swing.JPanel jpOne;
        private javax.swing.JPanel jpOneInner;
        private javax.swing.JPanel jpOnePlot;
        private javax.swing.JPanel jpThree;
        private javax.swing.JPanel jpThreeInner;
        private javax.swing.JPanel jpThreePlot;
        private javax.swing.JPanel jpTwo;
        private javax.swing.JPanel jpTwoInner;
        private javax.swing.JPanel jpTwoPlot;
        private javax.swing.JTextField jtfOne;
        private javax.swing.JTextField jtfThree;
        private javax.swing.JTextField jtfTwo;
        private javax.swing.JTabbedPane jtpBot;
        // End of variables declaration                   
    }

输出:

    run:
    this.getName():frame0
    this.getClass().getName():test.NewJFrame
    component.length:1
    --component[i].getName()null
    BUILD SUCCESSFUL (total time: 19 seconds)

我想获得类似的东西:

javax.swing.JTextField jtfOne;
javax.swing.JTextField jtfThree;
javax.swing.JTextField jtfTwo;
javax.swing.JButton jbFirst;
javax.swing.JButton jbThree;
javax.swing.JButton jbTwo;

如何吗

PD:我正在与Netbeans合作......

编辑1:尝试了VGR的建议

编辑2 WillShackleford 的解决方案,谢谢!!!:

public NewJFrame() {
  Color ACTIVE_COLOUR = Color.DARK_GRAY;
  Color INACTIVE_COLOUR = Color.LIGHT_GRAY;
  Font theFont = new Font("Serif",Font.PLAIN, 11);
  UIManager.put("TextField.font", theFont);
  UIManager.put("Button.font", theFont);
  UIManager.put("ComboBox.font", theFont);
  //UIManager.put("TextField.background", new ColorUIResource(ACTIVE_COLOUR));
  UIManager.put("TextField.background", ACTIVE_COLOUR);
  UIManager.put("TextField.foreground", INACTIVE_COLOUR);

  initComponents();

  List<Component> l = new ArrayList<>();
  addComponents(this, l);
  for(int i = 0; i < l.size(); i++) {
    if (l.get(i) instanceof javax.swing.JTextField) {
      javax.swing.JTextField jtf = (javax.swing.JTextField)l.get(i);
      jtf.setFont(new Font("Serif",Font.PLAIN, 24));
      jtf.setPreferredSize(new Dimension(jtf.getWidth(), jtf.getHeight()+10));
      System.out.println("JTextField:"+", Width:"+jtf.getWidth()+", Height:"+jtf.getHeight());
    }
    if (l.get(i) instanceof javax.swing.JButton) {
      javax.swing.JButton jb = (javax.swing.JButton)l.get(i);
      jb.setPreferredSize(new Dimension(jb.getWidth(), jb.getHeight()+10));
      System.out.println("JButton:"+", Width:"+jb.getWidth()+", Height:"+jb.getHeight());
    }
  }
}

public void addComponents(Container c, List<Component> l) {
  Component ca[] = c.getComponents();
  l.addAll(Arrays.asList(ca));
  for (int i = 0; i < ca.length; i++) {
    Component component = ca[i];
    if (Container.class.isAssignableFrom(component.getClass())) {
        addComponents((Container) component, l);
    }
  }
}

1 个答案:

答案 0 :(得分:2)

Netbeans中的GUI编辑器可以很容易地选择任何组件集并一步更改所有属性。

  1. 转到框架或面板的设计视图。
  2. 选择要更改的所有组件。这可以通过编辑器窗口中的图形选择矩形或在导航器窗口中使用左下角的树视图来完成。在导航器窗口中,您可以看到名称和类型,以快速选择所有给定类型。正常选择规则适用。按住Shift键选择最后一个选定内容和下一个单击或保持控件之间的所有组件,以添加/减去该组中的一个元素。
  3. 右键单击弹出菜单,然后选择属性。
  4. 如果只选择了多个组件,则只能看到它们共有的属性。
  5. 更改所需的任何属性,并且所有选定的组件都将设置该属性。
  6. 如果您仍想要所有组件的列表,这似乎对我有用:

    public NewJFrame() {
        initComponents();
        List<Component> l = new ArrayList<>();
        addComponents(this, l);
        System.out.println("l = " + l);
    }
    
    public void addComponents(Container c, List<Component> l) {
        Component ca[] = c.getComponents();
        l.addAll(Arrays.asList(ca));
        for (int i = 0; i < ca.length; i++) {
            Component component = ca[i];
            if (Container.class.isAssignableFrom(component.getClass())) {
                addComponents((Container) component, l);
            }
        }
    }