簡體   English   中英

Java-JFormattedTextField不允許在首次嘗試時輸入

[英]Java - JFormattedTextField does not allow input on first attempt

我正在用GUI制作一個數獨游戲,並將MaskFormatter用於JFormattedTextFields:

 formatter = new MaskFormatter(s);
 formatter.setValidCharacters("123456789");

但是我的問題是,當窗口打開且我單擊這些字段之一時,無法鍵入內容,
它僅適用於第二次嘗試,即當我單擊另一個字段然后返回到第一個字段時。

是否需要先失去焦點才能激活?

如果需要我的一些代碼,請告訴我。

這是它的樣子

在此處輸入圖片說明

編輯:問題在這里:

if (guessMatrix[i][j] == 0) {
    tfM[j][i].setBackground(Color.yellow);
    tfM[j][i].setText("");

如果沒有setText(“”),它可以正常工作。

我認為您在創建MaskFormatter的對象時遇到問題。 您可以使用遮罩模式創建對象,然后設置有效字符。

 MaskFormatter formatter = new MaskFormatter("#");
 formatter.setValidCharacters("123456789");
 JFormattedTextField txt = new JFormattedTextField(formatter);

當您單擊文本字段並鍵入任何數字(僅1-9)時,它可以正常工作,但您鍵入任何非數字則不允許。

謝謝,Jignesh Gothadiya

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM