简体   繁体   English

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

[英]Change attributes of all objects of a certain type in Java

I have a class that includes javax.swing.JButton , javax.swing.JTextField 我有一个包含javax.swing.JButtonjavax.swing.JTextField

But, after the class is instantiated, I need to change the attributes only of certain type. 但是,在实例化该类之后,我仅需要更改某些类型的属性。

I was trying with this.getComponents() but, is returning null . 我正在尝试使用this.getComponents()但是返回null

Here my code: 这是我的代码:

    /*
     * 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                   
    }

Output: 输出:

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

I want to obtain something like: 我想获得类似的东西:

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

How? 怎么样?

PD: I'm working with Netbeans... PD:我正在与Netbeans合作...

EDIT 1: The VGR's suggestion was attempted 编辑1:尝试了VGR的建议

EDIT 2 : The Solution by WillShackleford , Thank you!!!: 编辑2WillShackleford的解决方案,谢谢!!!:

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);
    }
  }
}

The GUI editor in Netbeans makes it quite easy to select any set of components and change all their properties in one step. Netbeans中的GUI编辑器使选择任何一组组件并一步更改其所有属性变得非常容易。

  1. Go to the Design view for the Frame or Panel. 转到框架或面板的设计视图。
  2. Select all the components you want to change. 选择所有要更改的组件。 This can be done either with a graphical selection rectangle in the editor window or with in the Navigator window with a tree view in the lower left. 可以在编辑器窗口中使用图形选择矩形,也可以在导航器窗口中使用左下方的树形视图来完成此操作。 In the navigator window you can see the names and types to quickly select all of a given type. 在导航器窗口中,您可以看到名称和类型,以快速选择所有给定类型。 Normal selection rules apply. 正常选择规则适用。 Hold-shift to select all components between that last selected and the next click or hold control to add/subtract one element from the set. 按住Shift键以选择上一个所选元素与下一个单击或按住控件之间的所有组件,以从集合中添加/减去一个元素。
  3. Right-click for the pop-up menu and choose properties. 右键单击弹出菜单,然后选择属性。
  4. With multiple components selected only properties they have in common will be visible. 选择了多个组件后,只有它们共有的属性才可见。
  5. Change any property you want and all selected components will have that property set. 更改所需的任何属性,所有选定的组件都将设置该属性。

If you still want a list of all components this seems to work for me: 如果您仍然想要所有组件的列表,这似乎对我有用:

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);
        }
    }
}

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

相关问题 如何检索列表中具有某些属性的所有对象? - How can I retrieve all Objects in a List that have certain attributes? Java-返回特定类型T的所有对象的集合,其中T传递给方法 - Java - Return set of all objects of certain type T where T is passed to the method Java:不可能使用迭代器来迭代对象的HashMap并更改对象的属性吗? - Java: Is it not possible to use an iterator to iterate a HashMap of objects and change attributes of objects? Java nashorn比较java对象是否属于某种java类型 - Java nashorn compare if a java objects is of a certain java type 如何在java文件中搜索某种类型的属性/参数 - How to search through java files for attributes/ parameters of a certain type 需要帮助在 object 中运行 if 语句以更改对象属性 (JAVA) - Need help to run an if statement inside an object to change that objects attributes (JAVA) 查找Java中给定类型的子对象的所有对象 - Finding All Objects that are a Child of a Given Type in Java 对特定类型的所有对象的Java调用方法 - Java calling method on all objects of specific type Java jUnit:如何断言列表中的所有对象都来自某个类? - Java jUnit: how to assert that all objects in a list are from a certain class? Java-更改时区我班上的所有属性 - Java - change time zone all attributes in my class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM