Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/348.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在运行时将JTextFields的文本设置为null?_Java_Swing_Runtime_Jtextfield - Fatal编程技术网

Java 如何在运行时将JTextFields的文本设置为null?

Java 如何在运行时将JTextFields的文本设置为null?,java,swing,runtime,jtextfield,Java,Swing,Runtime,Jtextfield,有人能帮我在运行时将JTextFields的文本设置为null吗, 当长度等于“13”时,我希望文本字段为空。 它将要求用户输入文本(代码的最大大小为13),然后输入将更改为null以用于另一个进程 code = new JextField(15); code.setForeground(new Color(30, 144, 255)); code.setFont(new Font("Tahoma", Font.PLAIN, 16)); code.setHorizontalAlignment(

有人能帮我在运行时将JTextFields的文本设置为null吗, 当长度等于“13”时,我希望文本字段为空。 它将要求用户输入文本(代码的最大大小为13),然后输入将更改为null以用于另一个进程

code = new JextField(15); 
code.setForeground(new Color(30, 144, 255));
code.setFont(new Font("Tahoma", Font.PLAIN, 16));
code.setHorizontalAlignment(SwingConstants.CENTER);   
code.setBounds(351, 76, 251, 38);
panel_2.add(code);

code.getDocument().addDocumentListener(new DocumentListener() {
public void changedUpdate(DocumentEvent e) {
  test();
}
public void removeUpdate(DocumentEvent e) {
  test();
}
public void insertUpdate(DocumentEvent e) {
   test();
}
public void test() {
if(code.getText().length()==13){                  
   code.setText("");                
 }                
}
我得到了nex错误:

java.lang.IllegalStateException: Attempt to mutate in notification
    at javax.swing.text.AbstractDocument.writeLock(Unknown Source)
    at javax.swing.text.AbstractDocument.replace(Unknown Source)
    at javax.swing.text.JTextComponent.setText(Unknown Source)

DocumentListener
不能用于修改
JTextComponent
的底层
文档。使用
文档过滤器

添加:

AbstractDocument d = (AbstractDocument) code.getDocument();
d.setDocumentFilter(new MaxLengthFilter(13));
文档过滤器

 static class MaxLengthFilter extends DocumentFilter {

   private final int maxLength;

   public MaxLengthFilter(int maxLength) {
      this.maxLength = maxLength;
   }

   @Override
   public void replace(DocumentFilter.FilterBypass fb, int offset,
         int length, String text, AttributeSet attrs)
               throws BadLocationException {

      int documentLength = fb.getDocument().getLength();
      if (documentLength >= maxLength) {
         super.remove(fb, 0, documentLength);
      } else {
         super.replace(fb, offset, length, text, attrs);
      }
   }
}

DocumentListener
不能用于修改
JTextComponent
的底层
文档。使用
文档过滤器

添加:

AbstractDocument d = (AbstractDocument) code.getDocument();
d.setDocumentFilter(new MaxLengthFilter(13));
文档过滤器

 static class MaxLengthFilter extends DocumentFilter {

   private final int maxLength;

   public MaxLengthFilter(int maxLength) {
      this.maxLength = maxLength;
   }

   @Override
   public void replace(DocumentFilter.FilterBypass fb, int offset,
         int length, String text, AttributeSet attrs)
               throws BadLocationException {

      int documentLength = fb.getDocument().getLength();
      if (documentLength >= maxLength) {
         super.remove(fb, 0, documentLength);
      } else {
         super.replace(fb, offset, length, text, attrs);
      }
   }
}

无法从DocumentListener中更新文档。将代码包装在invokeLater()中,以便将代码添加到EDT的末尾

SwingUtilities.invokeLater(new Runnable()
{
    public void run()
    {
        if (code.getDocument().getLength() >= 13)
        {                  
            code.setText("");                
        }
    }
});

无法从DocumentListener中更新文档。将代码包装在invokeLater()中,以便将代码添加到EDT的末尾

SwingUtilities.invokeLater(new Runnable()
{
    public void run()
    {
        if (code.getDocument().getLength() >= 13)
        {                  
            code.setText("");                
        }
    }
});