How do I add new functionality to pre-existing Java components?

To explain what I mean, I'll use the following code example Imagine you have this function

private void fadeButton(JButton b,int timeToFade) {
    //Fade code goes here
}

How do you implement it as a function that can run

JButton b = new JButton("Press Me");
b.fadeButton(20000);

Fadebutton now looks like

private void fadeButton(int timeToFade) {
    //Fade code goes here
}

Because the function is declared on the button itself

Solution

You can extend JButton with a new class to inherit JButton's methods and add your own code:

public class FadingButton extends JButton {

    //Constructors go here

    private void fadeButton(int timeToFade) {
        //Fade code goes here
    }
}

You can also decorate JButton with another class:

public class JButtonDecorator {
    private JButton btn;

    //Constructor here

    private void fadeButton(int timeToFade) {
        //Fade code goes here,hiding the held button
    }

    //getter and setter method for button
}

Or, if you want many different ways to influence your UI, you can create a utility class similar to the above:

//You Could use a factory pattern to make this a singleton instead of having static methods
public abstract class UIUtils {

    private UIUtils{} //Don't instantiate this class

    public static void fadeComponent(JComponent toFade) {
        //Fade code goes here
    }

    //Other static utility methods
}

Edit: use these modes The extension class is self-evident and an example of simple inheritance, so it is just a JButton problem. BTN = new fadingbutton(); For example Here are others:

To use the decorator, instantiate it to the same range as the button you are using now For example:

JButton myButton = new JButton();
//Customize button and add to UI
JButtonDecorator jbDec = new JButtonDecorator(myButton);

jbDec.fadeButton(20000);

Although the button is a field of the decorator, it will work normally in the UI Decorators simply wrap classes in useful ways, such as the fadebutton method

There are two ways to use utility classes One is to create an abstract class with static methods (as mentioned above). Some people think it is a bad form, but it is good for simple programs:

UIUtils.fadeComponent(myButton); //It's just that simple!
//The UIUtils class itself is never instantiated.
//All the methods are static,so no instances are needed.

Or, if you want more advanced methods, set the utility class as a singleton This changes the utility class to:

public class UIUtils {

    UIUtils singleton;

    private UIUtils{} //Don't instantiate this class publicly

    public static UIUtils getInstance() {
        if(singleton==null) //This is the first time the method is called
             singleton = new UIUtils();
        return singleton; //Return the one instance of UIUtils
    }

    public void fadeComponent(JComponent toFade) {
        //Fade code goes here
    }

    //Other utility methods
}

You will then declare the uiutils object at the class level for use in the UI:

UIUtils uiUtil = UIUtils.getInstance();

Somewhere in the code:

uiUtil.fadeComponent(myButton);

This pattern is more memory efficient and object - oriented, but I personally don't think it is very suitable for utility classes

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>