如何重构重复的代码行 java
How to refactor a repetitive line of code java
我需要用 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))+"% ");
}
});
希望我解释得很好,提前谢谢你。
在我看来,您可以创建一个单独的 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));
重构您的代码,要么扩展 JRadioButton
将 "differences" 传递给构造函数:
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);
我需要用 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))+"% ");
}
});
希望我解释得很好,提前谢谢你。
在我看来,您可以创建一个单独的 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));
重构您的代码,要么扩展 JRadioButton
将 "differences" 传递给构造函数:
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);