You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java实现PIN码验证程序clear按钮清除不彻底问题求助

问题排查与修复方案

核心问题原因

  • 数字键盘区CLEAR按钮的清空逻辑不完整:触发清空时仅修改了局部变量值、仅更新了星号展示的display标签,没有同步把存储实际输入的displayExtra标签的内容设为空,下次取组件内容时还是会拿到清除前的历史输入。
  • 底部额外添加的独立Clear按钮没有对应事件处理逻辑,点击后不会触发任何清空操作。

修复后的代码(仅需替换actionPerformed方法即可)

public void actionPerformed(ActionEvent e) {
    String textThere = display.getText(), textThereExtra = displayExtra.getText();
    String additionalText = "";
    for (int a = 0; a < buttonList.size(); a++) {
        if (e.getSource().equals(buttonList.get(a)) && a!=11 && a!=9) {
            additionalText = buttonList.get(a).getText();
        }
    }
    int flag = 0;
    // 同时处理数字键盘CLEAR和底部独立Clear按钮的触发
    if (e.getSource().equals(buttonList.get(9)) || e.getSource().equals(clr)) {
        textThere = "";
        textThereExtra = "";
        flag = 1;
    }
    if(flag != 1) {
        display.setText(textThere.concat("*"));
        displayExtra.setText(textThereExtra.concat(additionalText));
    } else {
        // 清空时同步更新两个标签的内容
        display.setText(textThere);
        displayExtra.setText(textThereExtra);
    }

    // 同时处理数字键盘ENTER和底部独立Enter按钮的触发
    if (e.getSource().equals(buttonList.get(11)) || e.getSource().equals(etr)) {
        System.out.println(textThereExtra);
        System.out.println(textThere);
        if(textThereExtra.equals(PASSWORD)) {
            Authorized();
        } else {
            Unauthorized();
        }
        // 可选优化:校验完成后自动清空输入
        display.setText("");
        displayExtra.setText("");
    }
}

补充说明

修复后同时兼容数字键盘的CLEAR/ENTER按钮和底部独立的Clear、Enter按钮的触发逻辑,按下清空按钮后两个标签的内容都会被完全重置,后续输入不会再出现历史数字。

内容的提问来源于stack exchange,提问作者eiriinjye

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.29 22:36:05