简体   繁体   中英

Static variables in Netbeans GUI builder

Ive developed a Java application using Netbeans and the GUI builder, its quite a complicated GUI and figured this would be easier than any other way and it certainly has been so far. Now my application is all but complete i'm looking to tidy up the code and I noticed that i've had to make a number of variables static for them to be passed to methods in the code. The problem is the number of read only code generated by the GUI builder, this means I cant pass the variables to methods using them but I cant see a way around it at all.

For example:

private void increaseSaturationButton(java.awt.event.ActionEvent evt) {                                         
increaseSaturation(colouredImage);
}

I cant edit the line private void increaseSaturationButton(java.awt.event.ActionEvent evt) { If colouredImage is not static then theres no way for me to pass it to this method and call it using increaseSaturation(); . Is there any way I can get around this or is this just a disadvantage of using the GUI builder? Or, is it just me being stupid?

Many thanks in advance

You CAN access non-static variables like colouredImage , as long as they are in the same class. Here is an example.

public class MyClass {

    // Non-static field
    private int value;
    public static int otherValue;

    // Constructor
    public MyClass(int value, int otherValue) { 
        this.value = value;
        MyClass.otherValue = otherValue;
    }

    private void increaseValuesButton(java.awt.event.ActionEvent evt) {
        value++; // Works just fine
        MyClass.otherValue++; // Also works fine
    }

}

You can also access non-private, static methods of other classes, so if you are unable to pass the colouredImage in due a limitation of your GUI builder, you can simply ask another class for it, like OtherClass.getColouredImage() .

You can also access static public fields of other classes, but that breaks encapsulation.

So yes, this is what I use to maximize my code customization. Replace the 'Variable' with the name of the Variable you want the action performed on. If you need a different action performed, like mouselistener: just let NetBeans add the action performed, copy and paste it, copy then remove the variable-component from the 'design' area in the GUI, and paste the design again.

public Terminal() {
            initComponents();  //default init components - this is the one generated by netbeans
            cinit(); // custom init components - this is the custom one
        }

        private void cinit() /* custom init components */ {
            Variable.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    VariableActionPerformed(evt);
                }
            });
        }

        public void VariableActionPerformed(java.awt.event.ActionEvent evt) {
            // to do code goes here
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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