How to make swing controls in netbeans by default protected or public?

老子叫甜甜 提交于 2020-01-24 09:27:09

问题


My first question is - can we make the swing controls (like textfield or combobox) public or protected in netbeans? When we drag n drop the components in a frame the code is autogenerated and the components are by default private. We cannot use them in other classes.

Second one is - Is there any other way by which we can use the components of one frame in other class?

For example - In one frame we have one textfield one button and one label only. We want to set the text of the label same as written in the textfield on clicking the button. but the code must be written in some other class.

package javaapplication10;

import javax.swing.JLabel;
public class NewJFrame extends javax.swing.JFrame {

    public NewJFrame() {
        initComponents();
    }


    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        jTextField1 = new javax.swing.JTextField();
        label1 = new java.awt.Label();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jButton1.setText("jButton1");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        label1.setText("label1");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(100, 100, 100)
                        .addComponent(label1, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 31, Short.MAX_VALUE)
                .addComponent(jButton1)
                .addGap(129, 129, 129))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(134, 134, 134)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1))
                .addGap(56, 56, 56)
                .addComponent(label1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(67, Short.MAX_VALUE))
        );

        pack();
    }

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

        label1.setText(jTextField1.getText());
    }
    public static void main(String args[]) {
        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);
        }



        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new NewJFrame().setVisible(true);
            }
        });
    }

    private javax.swing.JButton jButton1;
    private javax.swing.JTextField jTextField1;
    private java.awt.Label label1;

}

I have written only

label1.setText(jTextField1.getText());

but this is in the same class. I want this line to be written in some other class. but as textfield and labell is private we can not refer to them in other class.

How can i do that?

I am doing it for the learning purpose.

Please help.


回答1:


Right click on the component->properties->code:

Click on Variable Modifiers,choose which ever you want.

Is there any other way by which we can use the components of one frame in other class?

Yes,

  • 1) classname.variablename
  • 2) Create the object of the class, call variables through the object.



回答2:


I must downvote the accepted answer because it doesn't actually answer the question posed in the title: "How to make swing controls in netbeans by default protected or public?" If the controls are by default protected, you would just drag a control into your form and it would be protected without any further action on your part. The accepted answer requires you to change each control to protected or public, which can be a big nuisance.

To make swing controls protected or public by default

  1. open the Design Navigator window,
  2. right click the topmost node (e.g. the Form node if you are creating a form),
  3. click Properties, and under Code Generation,
  4. change the Variables Modifier to protected or public.

To make this change for all future GUI forms,

  1. go to Tools - Options,

  2. select the tabs Java, then GUI builder,

  3. and change Variables Modifier.




回答3:


Simpliy add getter for the needed private components to your class.

private JTextField text1 = new JTextField();
public JTextField getText1()
{ 
  return text1;
}


来源:https://stackoverflow.com/questions/15717292/how-to-make-swing-controls-in-netbeans-by-default-protected-or-public

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