gpt4 book ai didi

java - 如何重构重复的一行代码java

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:18:58 24 4
gpt4 key购买 nike

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

关于java - 如何重构重复的一行代码java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31251030/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com