简体   繁体   English

如何避免使用Java中的JCheckBoxes进行冗余编码

[英]How to avoid redundant coding with JCheckBoxes in Java

I have a set of classes that implement a particular interface and I have a set of checkboxes. 我有一组实现特定接口的类,并且有一组复选框。 I want to throw an error if no checkboxes are selected. 如果未选中任何复选框,我想抛出一个错误。 If atleast one or more checkboxes are selected, then it should create objects associated with that checkbox. 如果至少选中一个或多个复选框,则它应创建与该复选框关联的对象。

This is how I done. 这就是我做的。

interface U { ... }

class A implements U { ... }
class B implements U { ... }
class C implements U { ... }

class Main {
    //.... 
    //....
    public void findSelectedCheckBoxesAndCreateObjects() {
        if(!(checkboxA.isSelected() || checkboxB.isSelected() || checkboxC.isSelected()) {
            System.out.println("No checkboxes selected");
            return;
        }

        //if any selected, create associated object
        if(checkboxA.isSelected()) new A(file);
        if(checkboxB.isSelected()) new B(file);
        if(checkboxC.isSelected()) new C(file);
    }
}

Now I have 3 problems. 现在我有3个问题。

  1. This is just a sample code. 这只是一个示例代码。 Original has 8 checkboxes and classes with more coming. 原始的有8个复选框和类,还有更多其他版本。
  2. I can't keep adding || checkboxD.isSelected() 我无法继续添加|| checkboxD.isSelected() || checkboxD.isSelected() every time I have a new class for checking it. 每当我有一个新类进行检查时,都将使用|| checkboxD.isSelected()
  3. Same thing. 一样。 I can't keep adding if(checkboxD.isSelected()) new D(file); 我不能继续添加if(checkboxD.isSelected()) new D(file); for every class. 每堂课

It is very inelegant. 这是非常优雅的。 Can I have some kind of loop that removes the redundant code? 我可以使用某种循环来删除冗余代码吗?

Please give me your suggestions. 请给我您的建议。 Thank you. 谢谢。

You should use a collection structure to hold your checkboxes and those related classes. 您应该使用集合结构来保存您的复选框和那些相关的类。 Using a Map you could do something like this: 使用地图,您可以执行以下操作:

Map <JCheckBox,Class<U>> uCheck = new HashMap<JCheckBox,Class<U>>( ); Map <JCheckBox,Class<U>> uCheck = new HashMap<JCheckBox,Class<U>>( );

// add your checkboxes and U-classes to the map //将您的复选框和U类添加到地图

uCheck.put(checkBoxA, A.class);

Now, it's quite easy to get a collection of the classes that need to be instantiated based on the checkbox status: 现在,很容易获得需要基于复选框状态实例化的类的集合:

public Collection<Class<U>>  getEnabledClasses(<JCheckBox,Class<U>> checkMap) {
    List<Class<U>> result = new LinkedList<Class<U>>();
    for (Map.Entry<JCheckBox,Class<U>> entry:checkMap.entrySet()) {
        if (entry.getKey().isSelected()) {
            result.add(entry.getValue());
        }
    }
}

Now, a call to getEnabledUs(uCheck) returns a collection of the selected classes. 现在,对getEnabledUs(uCheck)的调用将返回所选类的集合。 If the collection is empty, there's no selection, hence nothing to do. 如果集合为空,则没有选择,因此无事可做。

for (Class<U> u:getEnabledClasses(...)) {
    Constructor<U> cons = u.getConstructor(...);
    U instance = cons.newInstance(fileparameter);
    instance.doSomething(...);
}

That should get you started. 那应该让您开始。 (*) Disclaimer: this is non-tested code. (*)免责声明:这是未经测试的代码。 Rather pseudo-code with crisp detail only where needed. 只是在需要时才使用伪造细节的伪代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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