1 回答

TA貢獻1827條經(jīng)驗 獲得超4個贊
據(jù)我了解,您ActionListener是在某個類的范圍內(nèi)創(chuàng)建的。在不查看類的整個代碼的情況下,很難提出重構(gòu)代碼的最佳方法。
但是為了實現(xiàn)您的目標,您可以將 的原始值保存getText()在類實例字段中,然后在偵聽器的每次新調(diào)用中更新它:
public class Main {
private String formula;
ActionListener yesListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (formula == null) {
formula = textArea.getText();
}
int i = StringUtils.countMatches(formula, "?");
if (i > 1) {
formula = findTrue(formula);
questionLabel.setText(askQuestion(formula));
} else {
questionLabel.setText("The formula will return" + findTrue(formula));
}
}
};
添加回答
舉報