如何重构重复的java代码行

如何重构重复的java代码行,java,refactoring,jbutton,Java,Refactoring,Jbutton,我需要用excel文件中的信息创建很多按钮,每个按钮都有不同的信息,但目前创建按钮的方法超过了65535字节的限制,因此我考虑重构创建按钮的方法,但考虑到每个按钮与前一个按钮略有不同,我不知道是否可能,下面是我正在做的一个示例: JRadioButton rdbtn1IOE1 = new JRadioButton("Cruzamiento con algún Cuerpo de Agua - Sí"); rdbtn1IOE1.addActionListener(new ActionLis

我需要用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))+"% ");

        }
    });
JRadioButton rdbtn1IOE1=新的JRadioButton(“Cruzamiento con algún Cuerpo de Agua-Sí”);
rdbtn1IOE1.addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件e){
IOE.set(0,0.8);
标签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=新的JRadioButton(“Cruzamiento con algún Cuerpo de Agua-No”);
rdbtn2IOE1.addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件e){
IOE.set(0,0.0);
标签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=新的JRadioButton(“无数据”);
rdbtnNoDataIOE1.addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件e){
IOE.set(0,0.0);
标签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=新JRadioButton(“<100米”);
rdbtn1IOE2.addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件e){
IOE.set(1,0.1);
标签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=新的JRadioButton(“无数据”);
rdbtnNoDataIOE2.addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件e){
IOE.set(1,0.0);
标签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”)在我看来,您可以创建一个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());
    }
}
公共类IOESetractionListener扩展了ActionListener{
私人终审法院;
私人决赛双b;
公共IOESetActionListener(内部a,双b){
这个a=a;
这个.b=b;
}
已执行的公共无效操作(操作事件e){
IOE.set(a,b);
最终StringBuilder=新StirngBuilder(“IOE:”);
对于(int i=0;i<22;++i){
builder.append(IOE.get(i));
}
label_IOE.setText(builder.append(“%”).toString();
}
}

然后您的按钮可以是(例如)
rdbtn1IOE1.addActionListener(新的IOESetActionListener(0,0.8));
重构您的代码,或者扩展
JRadioButton
将“差异”传递给构造函数:

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 + "% ");
            }
        });
    }
}
或者,如果您不想扩展组件,这里有一个factory方法版本:

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

你能更具体地描述一下“小差异”吗?也许你可以编写一个自定义的
ActionListener
/
JRadioButton
类,它接受一些参数?你的字符串在'label_IOE.setText(“X”)中“将仅采用上述格式吗?@Marvin每个按钮都有不同的名称,并且它们被划分为按钮组,因此假设一个组中有5个按钮,该组在数组列表中分配了一个位置,并且该位置在数组列表中的值根据所选按钮进行设置selected@karthik是的,用于labe的setText方法l对于每个按钮都是一样的你到底想重构什么?只有
label\u IOE.setText
还是完整的按钮创建?非常感谢Daniel,有了这个构造函数,我也可以设置按钮的名称吗?因为我需要手动设置每个按钮的名称。如果是这样,你会希望扩展AbstractAction instead,并将其传递给JButton的构造函数,而不是addActionListener。很少有很好的理由扩展JComponents的具体子类,而且随着时间的推移,它至少会导致非常糟糕的设计。@Daniel这是一个意见问题,但因为我感觉慷慨,我提供了一个工厂方法选项。@Bohemian在更改了某些组件的某些部分后你的工厂方法我终于可以让它工作了非常感谢你!!!
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);