在 Java 中使用堆栈实现撤消功能的问题

问题描述 投票:0回答:1

我对 Java 编程还比较陌生。我正在尝试制作一个充当预算计算器的应用程序,并且我想实现一个撤消功能,我可以连续执行多个撤消操作以撤消在各自字段中输入的任何数字。截至目前,代码一次只能执行一项撤消操作。如果我尝试连续撤消多次,它只会撤消一次并阻止我进一步撤消。我在尝试解决此问题时观察到,用户界面中的最后一个文本字段确实按预期工作,如果我继续按撤消按钮,它将撤消整个文本字段。这让我觉得我错误地实现了我的堆栈,但我还没有找到解决方案。以下是撤销功能涉及的方法。

public void saveState() {
    Map<String, Double> incomeValues = new HashMap<>();
    Map<String, Double> spendingValues = new HashMap<>();

    // Get current values from income fields
    for (Map.Entry<String, JTextField> entry : incomeFields.entrySet()) {
        String fieldName = entry.getKey();
        JTextField field = entry.getValue();
        String text = field.getText();
        double value = text.isEmpty() ? 0 : Double.parseDouble(text);
        incomeValues.put(fieldName, value);
    }

    // Get current values from spending fields
    for (Map.Entry<String, JTextField> entry : spendingFields.entrySet()) {
        String fieldName = entry.getKey();
        JTextField field = entry.getValue();
        String text = field.getText();
        double value = text.isEmpty() ? 0 : Double.parseDouble(text);
        spendingValues.put(fieldName, value);
    }

    stateStack.push(new BudgetState(new HashMap<>(incomeValues), new HashMap<>(spendingValues)));
}

public void undo(int count) {
    while (count > 0 && !stateStack.isEmpty()) {
        stateStack.pop();
        count--;
    }

    BudgetState prevState = stateStack.isEmpty() ? null : stateStack.peek();
    restoreState(prevState);
    updateTotals();
}

public void restoreState(BudgetState state) {
    Map<String, Double> incomeValues = state.getIncomeValues();
    Map<String, Double> spendingValues = state.getSpendingValues();

    // Set values in text fields
    for (Map.Entry<String, Double> entry : incomeValues.entrySet()) {
        String fieldName = entry.getKey();
        double value = entry.getValue();
        JTextField field = incomeFields.get(fieldName);
        field.setText(String.valueOf(value)); // Set text field value
    }

    for (Map.Entry<String, Double> entry : spendingValues.entrySet()) {
        String fieldName = entry.getKey();
        double value = entry.getValue();
        JTextField field = spendingFields.get(fieldName);
        field.setText(String.valueOf(value)); // Set text field value
    }
}

这是另一个类,它充当容器来存储和表示预算计算器应用程序中的收入和支出值的状态。

package Budget;
import java.util.*;

public class BudgetState {
    private Map<String, Double> incomeValues;
    private Map<String, Double> spendingValues;

    public BudgetState(Map<String, Double> incomeValues, Map<String, Double> spendingValues) {
        this.incomeValues = new HashMap<>(incomeValues);
        this.spendingValues = new HashMap<>(spendingValues);
    }

    public Map<String, Double> getIncomeValues() {
        return incomeValues;
    }

    public Map<String, Double> getSpendingValues() {
        return spendingValues;
    }
}
java undo
1个回答
0
投票

undo
方法中,您使用
peek
从堆栈中获取先前的状态。
peek
方法用于检查顶部元素,但不会删除它。请使用
pop
来代替。

请注意,当堆栈为空时,您将 null 传递给

restoreState
,但该方法对 null 情况没有特殊处理。

© www.soinside.com 2019 - 2024. All rights reserved.