如何重构重复的代码行java

时间:2015-07-06 16:30:25

标签: java refactoring jbutton

我需要使用excel文件中的信息创建大量按钮,每个按钮都有不同的信息,但是现在创建按钮的方法超出了65535字节的限制,所以我想重构创建按钮的方法但我不知道是否可能考虑每个按钮与前一个按钮有点不同,这里是我正在做的一个例子:

JRadioButton rdbtn1IOE1 = new JRadioButton("Cruzamiento con algún Cuerpo de Agua - Sí");
    rdbtn1IOE1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            IOE.set(0,0.8);
            label_IOE.setText("IOE:"+(IOE.get(0)+IOE.get(1)+IOE.get(2)+IOE.get(3)+IOE.get(4)+IOE.get(5)+IOE.get(6)+IOE.get(7)+
                    IOE.get(8)+IOE.get(9)+IOE.get(10)+IOE.get(11)+IOE.get(12)+IOE.get(13)+IOE.get(14)+IOE.get(15)+IOE.get(16)+IOE.get(17)+
                    IOE.get(18)+IOE.get(19)+IOE.get(20)+IOE.get(21))+"% ");

        }
    });
    JRadioButton rdbtn2IOE1 = new JRadioButton("Cruzamiento con algún Cuerpo de Agua - No");
    rdbtn2IOE1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            IOE.set(0,0.0);
            label_IOE.setText("IOE:"+(IOE.get(0)+IOE.get(1)+IOE.get(2)+IOE.get(3)+IOE.get(4)+IOE.get(5)+IOE.get(6)+IOE.get(7)+
                    IOE.get(8)+IOE.get(9)+IOE.get(10)+IOE.get(11)+IOE.get(12)+IOE.get(13)+IOE.get(14)+IOE.get(15)+IOE.get(16)+IOE.get(17)+
                    IOE.get(18)+IOE.get(19)+IOE.get(20)+IOE.get(21))+"% ");

        }
    });
    JRadioButton rdbtnNoDataIOE1 = new JRadioButton("No Data");
    rdbtnNoDataIOE1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            IOE.set(0,0.0);
            label_IOE.setText("IOE:"+(IOE.get(0)+IOE.get(1)+IOE.get(2)+IOE.get(3)+IOE.get(4)+IOE.get(5)+IOE.get(6)+IOE.get(7)+
                    IOE.get(8)+IOE.get(9)+IOE.get(10)+IOE.get(11)+IOE.get(12)+IOE.get(13)+IOE.get(14)+IOE.get(15)+IOE.get(16)+IOE.get(17)+
                    IOE.get(18)+IOE.get(19)+IOE.get(20)+IOE.get(21))+"% ");

        }
    });

    JRadioButton rdbtn1IOE2 = new JRadioButton("< 100 metros");
    rdbtn1IOE2.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            IOE.set(1,0.1);
            label_IOE.setText("IOE:"+(IOE.get(0)+IOE.get(1)+IOE.get(2)+IOE.get(3)+IOE.get(4)+IOE.get(5)+IOE.get(6)+IOE.get(7)+
                    IOE.get(8)+IOE.get(9)+IOE.get(10)+IOE.get(11)+IOE.get(12)+IOE.get(13)+IOE.get(14)+IOE.get(15)+IOE.get(16)+IOE.get(17)+
                    IOE.get(18)+IOE.get(19)+IOE.get(20)+IOE.get(21))+"% ");

        }
    });
    JRadioButton rdbtnNoDataIOE2 = new JRadioButton("No Data");
    rdbtnNoDataIOE2.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            IOE.set(1,0.0);
            label_IOE.setText("IOE:"+(IOE.get(0)+IOE.get(1)+IOE.get(2)+IOE.get(3)+IOE.get(4)+IOE.get(5)+IOE.get(6)+IOE.get(7)+
                    IOE.get(8)+IOE.get(9)+IOE.get(10)+IOE.get(11)+IOE.get(12)+IOE.get(13)+IOE.get(14)+IOE.get(15)+IOE.get(16)+IOE.get(17)+
                    IOE.get(18)+IOE.get(19)+IOE.get(20)+IOE.get(21))+"% ");

        }
    });
    JRadioButton rdbtn2IOE2 = new JRadioButton(">= 100 to <= 200 metros");
    rdbtn2IOE2.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            IOE.set(1,0.05);
            label_IOE.setText("IOE:"+(IOE.get(0)+IOE.get(1)+IOE.get(2)+IOE.get(3)+IOE.get(4)+IOE.get(5)+IOE.get(6)+IOE.get(7)+
                    IOE.get(8)+IOE.get(9)+IOE.get(10)+IOE.get(11)+IOE.get(12)+IOE.get(13)+IOE.get(14)+IOE.get(15)+IOE.get(16)+IOE.get(17)+
                    IOE.get(18)+IOE.get(19)+IOE.get(20)+IOE.get(21))+"% ");

        }
    });

我希望我能很好地解释这一点,提前谢谢你。

2 个答案:

答案 0 :(得分:2)

在我看来,你可以创建一个ActionListener子类,其构造函数接受你传递给IOE.set的两个参数。

public class IOESetActionListener extends ActionListener {
    private final int a;
    private final double b;
    public IOESetActionListener(int a, double b) {
        this.a = a;
        this.b = b;
    }

    public void actionPerformed(ActionEvent e) {
        IOE.set(a, b);
        final StringBuilder builder = new StirngBuilder("IOE:");
        for (int i = 0; i < 22; ++i) {  
          builder.append(IOE.get(i));
        }
        label_IOE.setText(builder.append("% ").toString());
    }
}

然后你的按钮就可以(例如)rdbtn1IOE1.addActionListener(new IOESetActionListener(0,0.8));

答案 1 :(得分:0)

重构您拥有的代码,或者通过&#34;差异扩展JRadioButton&#34;到构造函数:

public class MyJRadioButton extends JRadioButton {
    public MyJRadioButton(String title, final int x, final double y) {
        super(title);
        addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                IOE.set(x, y);
                StringBuilder text = new StringBuilder("IOE:");
                for (int i = 0; i < 22; i++)
                    text.append(IOE.get(i));
                label_IOE.setText(text + "% ");
            }
        });
    }
}

然后使用,例如:

JRadioButton rdbtnNoDataIOE1 = new MyJRadioButton("No Data", 0, 0.0);

或者如果您不想扩展组件,可以使用工厂方法版本:

public static JRadioButton create(String title, final int x, final double y) {
    JRadioButton button = JRadioButton(title);
    button.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
            IOE.set(x, y);
            StringBuilder text = new StringBuilder("IOE:");
            for (int i = 0; i < 22; i++)
                text.append(IOE.get(i));
            label_IOE.setText(text + "% ");
        }
    });
    return button;
}
你使用的

JRadioButton rdbtnNoDataIOE1 = create("No Data", 0, 0.0);