[英]I cannot clear my JTextArea, how can I clear the JTextArea?
這是虛擬治療師的原型,主要用於Java實踐。 我已經嘗試清除此JTextArea
已有2天了。
我已經進行了清理和重建,這使我克服了其他一些障礙,但是我對嘗試的方法一無所知。 setEnabled()
被編碼掉了,因為我只是嘗試使用不同的方法來打開和關閉它。 除了清除按鈕以外的所有東西都可以正常工作。 用JTextField
輸入按Enter后,在文本區域中得到響應。 但這還不清楚。
public void actionPerformed(ActionEvent event)
{
String inp = event.toString(); //this is input in a JTextField
if(inp.contains("sad") || inp.contains("lonely"))
{
txtArea.setText(response1);
}else if(inp.contains(""))
{
txtArea.setText(response2);
}
else if(event.getSource() == clear) //clear is a button
{
//clear.setEnabled(true);
txtArea.setText(""); //I've tried selectAll(), replaceSelection()
}
}
從您的代碼中可以看到,這可能有助於解決明顯的問題。 在您的代碼中,由於始終為true的條件(包含emty字符串)之后的else,因此永遠無法到達clear的if分支。 我將其移至前端-可以訪問。
我不能從您發布的代碼中確定地說,但是event.toString()看起來也很可疑,以及最后一個條件(在下面的更改代碼中),這始終是正確的。
//this looks odd/suspicious to me too!
String inp = event.toString(); //???!!! this is input in a JTextField
/* rather something like
* if(event.getSource() instanceof JTextField){
* inp = ((JTextField)event.getSource()).getText();
* }
*/
if(event.getSource() == clear) { //clear is a button
//clear.setEnabled(true);
txtArea.setText(""); //I've tried selectAll(), replaceSelection()
} else if(inp.contains("sad") || inp.contains("lonely")) {
txtArea.setText(response1);
} else if(inp.contains("")) { //??? always true!! rather: inp.equals("") or inp.isEmpty() ...
txtArea.setText(response2);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.