简体   繁体   中英

Is there a way to shorten this repetitive code? (adding JPanel Components)

Is there a way to shorten this code? It's very repetitive. I was thinking maybe there was a way to create a method in my Widget class that returns an array of objects so then I could shorten my code to one line:

ecoPanel.add(economy.getObjects);

economy , standard , advanced , exceptional are all Widget objects from the Widget class I created

    ecoPanel.add(economy.styleLB); //adds economy textFiield to our ecoPanel
    ecoPanel.add(economy.redLB); //adds economy red label to our ecoPanel
    ecoPanel.add(economy.redTF); //adds red textfield to show inventory amount
    ecoPanel.add(economy.greenLB); //adds economy green label to our ecoPanel
    ecoPanel.add(economy.greenTF); //adds green textfield to show inventory amount
    ecoPanel.add(economy.blueLB);  //adds economy blue label to our ecoPanel
    ecoPanel.add(economy.blueTF); //adds blue textfield to show inventory amount
    ecoPanel.add(economy.yellowLB); //adds economy yellow label to our ecoPanel
    ecoPanel.add(economy.yellowTF); //adds yellow textfield to show inventory amount
    ecoPanel.add(economy.totalLB); //adds economy total label to our ecoPanel
    ecoPanel.add(economy.totalTF); //adds total textfield to show inventory amount

    stdPanel.add(standard.styleLB); //adds standard textFiield to our stdPanel
    stdPanel.add(standard.redLB); //adds standard red label to our stdPanel
    stdPanel.add(standard.redTF); //adds red textfield to show inventory amount
    stdPanel.add(standard.greenLB); //adds standard green label to our stdPanel
    stdPanel.add(standard.greenTF); //adds green textfield to show inventory amount
    stdPanel.add(standard.blueLB);  //adds standard blue label to our stdPanel
    stdPanel.add(standard.blueTF); //adds blue textfield to show inventory amount
    stdPanel.add(standard.yellowLB); //adds standard yellow label to our stdPanel
    stdPanel.add(standard.yellowTF); //adds yellow textfield to show inventory amount
    stdPanel.add(standard.totalLB); //adds standard total label to our stdPanel
    stdPanel.add(standard.totalTF); //adds total textfield to show inventory amount

    advPanel.add(advanced.styleLB); //adds advanced textFiield to our advPanel
    advPanel.add(advanced.redLB); //adds advanced red label to our advPanel
    advPanel.add(advanced.redTF); //adds red textfield to show inventory amount
    advPanel.add(advanced.greenLB); //adds advanced green label to our advPanel
    advPanel.add(advanced.greenTF); //adds green textfield to show inventory amount
    advPanel.add(advanced.blueLB);  //adds advanced blue label to our advPanel
    advPanel.add(advanced.blueTF); //adds blue textfield to show inventory amount
    advPanel.add(advanced.yellowLB); //adds advanced yellow label to our advPanel
    advPanel.add(advanced.yellowTF); //adds yellow textfield to show inventory amount
    advPanel.add(advanced.totalLB); //adds advanced total label to our advPanel
    advPanel.add(advanced.totalTF); //adds total textfield to show inventory amount

    excPanel.add(exceptional.styleLB); //adds exceptional textFiield to our excPanel
    excPanel.add(exceptional.redLB); //adds exceptional red label to our excPanel
    excPanel.add(exceptional.redTF); //adds red textfield to show inventory amount
    excPanel.add(exceptional.greenLB); //adds exceptional green label to our excPanel
    excPanel.add(exceptional.greenTF); //adds green textfield to show inventory amount
    excPanel.add(exceptional.blueLB);  //adds exceptional blue label to our excPanel
    excPanel.add(exceptional.blueTF); //adds blue textfield to show inventory amount
    excPanel.add(exceptional.yellowLB); //adds exceptional yellow label to our excPanel
    excPanel.add(exceptional.yellowTF); //adds yellow textfield to show inventory amount
    excPanel.add(exceptional.totalLB); //adds exceptional total label to our excPanel
    excPanel.add(exceptional.totalTF); //adds total textfield to show inventory amount

Perhaps putting your labels and textfields into seperate ArrayLists then using a for loop to add them would work. Also, if you want to put them all in order, you could make one arraylist of objects then figure out what class they are by using

//add a for loop here
String str = arrList.get(i).getClass.getName();
if(str.equals("JLabel"))
{
   panel.add(arrList.get(i));
}
else
{
   // other objects added
}

and keep adding them that way.

What about adding a methods in the Widget class like this:

public Component[] getObjects()
{
    return new Component[]{styleLB,redLB,redTF,greenLB,greenTF,blueLB,blueTF,yellowLB,yellowTF,totalLB,totalTF};
}
public void addAllToPanel(JPanel p)
{
    Component[] arr = getObjects();
    for(Component c:arr)
        p.add(c);
}

Then use it like this

economy.addAllToPanel(ecoPanel);
standard.addAllToPanel(stdPanel);
advanced.addAllToPanel(advPanel);
exceptional.addAllToPanel(excPanel);

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