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

如何替换文本域(Text Area)的appendText实现点击更新时覆盖内容?

Solution: Replace Text Area Content Instead of Appending

The fix is straightforward—instead of adding to the existing content with append(), we’ll replace the entire text in txt_View when the "更新" button is clicked. Here’s how to adjust your code:

Step-by-Step Adjustment

  1. Build your full output first: Use a StringBuilder to construct all the text you want to display in one place (this is cleaner and more efficient than multiple append calls).
  2. Replace the text area content: Use setText() instead of append() to overwrite all existing content with your newly built string.

Here’s the modified actionPerformed method:

public void actionPerformed(ActionEvent e) {
    // Create a builder to assemble our output text
    StringBuilder outputText = new StringBuilder();
    
    // Add each piece of input to the builder
    outputText.append(text_Sin.getText()).append("\n");
    outputText.append(text_Name.getText()).append("\n");
    outputText.append(text_Address.getText()).append("\n");
    
    // Add gender selection
    if(r_GenderM.isSelected()) {
        outputText.append(r_GenderM.getText()).append("\n");
    } else {
        outputText.append(r_GenderF.getText()).append("\n");
    }
    
    // Add hobbies
    if(c_hSleeping.isSelected()) {
        outputText.append(c_hSleeping.getText()).append("\n");
    }
    if(c_hStudy.isSelected()) {
        outputText.append(c_hStudy.getText()).append("\n");
    }
    
    // Add religion
    String selectedReligion = c_religion.getSelectedItem().toString();
    switch(selectedReligion) {
        case "Islam":
            outputText.append("Islam\n");
            break;
        case "Christian":
            outputText.append("Christian\n");
            break;
        case "Catholic":
            outputText.append("Catholic\n");
            break;
        default:
            outputText.append("Atheist\n");
            break;
    }
    
    // Add remaining contact details
    outputText.append(text_Email.getText()).append("\n");
    outputText.append(text_Facebook.getText()).append("\n");
    outputText.append(text_Contact.getText());
    
    // Replace the entire content of txt_View
    txt_View.setText(outputText.toString());
}

Quick Notes on Improvements

  • Removed redundant checks: isSelected() == true is unnecessary—isSelected() already returns a boolean, so we can use it directly in the condition.
  • Switch statement: Replaced the long if-else chain for religion with a switch statement to make the code more readable.
  • Efficiency: Using StringBuilder avoids multiple calls to the text area’s UI methods, which is smoother for the user.

If you want a simpler (though slightly less efficient) alternative, you could also clear the text area first with txt_View.setText(""), then keep your original append() calls. Either approach will achieve your goal of replacing content instead of appending.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.29 07:39:33