【问题标题】:Is there a way to shorten this repetitive code? (adding JPanel Components)有没有办法缩短这个重复的代码? (添加 JPanel 组件)
【发布时间】:2015-05-17 20:33:20
【问题描述】:

有没有办法缩短这段代码?这是非常重复的。我在想也许有一种方法可以在我的 Widget 类中创建一个返回对象数组的方法,这样我就可以将代码缩短为一行:

ecoPanel.add(economy.getObjects);

economystandardadvancedexceptional 都是我创建的 Widget 类中的 Widget 对象

    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

【问题讨论】:

  • 大概所有的面板都是同一类型的。想必所有advancedexceptional等都是同一种类型。
  • 除了外观和代码维护之外,您所拥有的没有任何问题。如果您想走那条路,下面戴维的回答是一个不错的选择。但是,您可能只想使用像 IntelliJ 附带的那种不错的 GUI 设计器。它将为您维护所有构造函数代码。

标签: java class oop methods jpanel


【解决方案1】:

也许将标签和文本字段放入单独的 ArrayList 中,然后使用 for 循环添加它们会起作用。此外,如果你想把它们全部按顺序排列,你可以创建一个对象数组列表,然后使用

找出它们是什么类
//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
}

并继续以这种方式添加它们。

【讨论】:

    【解决方案2】:

    像这样在 Widget 类中添加方法怎么样:

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

    那就这样用吧

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

    【讨论】:

    • 这就是我要找的。谢谢!
    • 不错的方法!但是,是 Component JComponent 还是它们不同?
    • @Raheel138 组件是一个 java.awt 类,它是 JComponent 的超类。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-17
    • 1970-01-01
    相关资源
    最近更新 更多