JTextField的setText方法不能从KeyListener中工作 [英] JTextField's setText method doesn't work from a KeyListener

查看:207
本文介绍了JTextField的setText方法不能从KeyListener中工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我来自KeyListener时,我很困惑为什么JTextField似乎没有通过使用setText()方法来清除。它可以在ActionListener中正常工作,但最令人惊讶的是,如果KeyListener方法尝试使用虚拟动作事件(动态创建为简单测试)来调用ActionListener方法,它仍会保留已键入的文本。 / p>

换句话说,当你从命令行运行它时,如果你在字段中键入一个3,你会看到setText(test )方法不会像我期望的那样消灭3,而是将它留在原地。然后,您将在显示屏中看到test3。我注意到这一行有评论。单击JButton将正确删除文本。 JButton和JLabel将正确地更改文本。但是JTextField不会。如果然后按下按钮,您将看到action事件正确清除JTextField。现在,如果您切换注释掉的行,您可以看到尝试从KeyTyped方法调用actionPerformed方法!而且,当您在文本字段中键入3时,它将不会被删除。我希望setText()方法清除它,它不会。即使keyTyped()方法正在调用与JTextButton相同的actionPerformed()方法,这也是如此。



这里的动机可能会有所帮助。我需要捕获一个特定的热键,它将在输入时清除JTextField,就像按下清除按钮一样。这似乎不起作用。



之前我没有用Swing做过那么多,但这非常令人费解。



我的SSCCE代码如下:

  import java.awt。*; 
import java.awt.event。*;
import javax.swing。*;
import javax.swing.event。*;

类P2实现KeyListener,ActionListener
{
JTextField fld;
JButton btn;
JLabel lbl;

P2()
{
JFrame frm = new JFrame(Test);
fld = new JTextField(10);
JPanel pnl = new JPanel();
btn =新的JButton(清除它);
lbl = new JLabel(这是一个测试);

fld.addKeyListener(this);

btn.addActionListener(this);

frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frm.setSize(400,400);
frm.setLayout(new FlowLayout());
pnl.add(fld);
pnl.add(btn);
pnl.add(lbl);
frm.getContentPane()。add(pnl);
frm.setVisible(true);
}
public void keyPressed(KeyEvent ke){}
public void keyReleased(KeyEvent ke){}
public void keyTyped(KeyEvent ke)
{
System.out.println(得到一个按下的键);

//这是应该擦除字段注释的setText方法:
this.fld.setText(test);
this.btn.setText(再试一次);
this.lbl.setText(有一个预定的密钥);


//切换此注释以查看动作事件的调用:
// this.actionPerformed(new ActionEvent(new Object(),2,test) );

}
public void actionPerformed(ActionEvent ae)
{
fld.setText();
fld.selectAll();
}
public static void main(String [] args)
{
SwingUtilities.invokeLater

new Runnable()
{
public void run()
{
new P2();
}
}
);
}
}


解决方案

这行为是由于在 KeyListener 被触发后,字段将处理 KeyEvent 。您可以通过

  ke.consume();来消费该事件。 

您的方法 keyTyped



根据您的要求,另一种方法是将清算调用封装在 SwingUtilities.invokeLater 中,这将在您当前处理后处理事件,因此在更新后清除字段。


I'm puzzled as to why a JTextField doesn't seem to just "clear out" by using the setText("") method on it, when this is coming from a KeyListener. It works fine from an ActionListener, except that, most amazingly, if the KeyListener method tries to invoke the ActionListener method, with a dummy action event (created on the fly as a simple test), it still leaves the typed text in place.

In other words, when you run it from the command line, if you type, for example, a "3" into the field, you will see the setText("test") method does not wipe out the 3, as I would expect and desire, but rather leaves it in place. You will then see "test3" in the display. I have noted this line with a comment. Clicking the JButton will wipe out the text properly. The JButton and JLabel will change text properly. but the JTextField won't. If you then press the button, you will see that the action event clears out the JTextField properly. Now, if you toggle the commented out line, you can see an attempt to invoke the actionPerformed method from the KeyTyped method!!! And still, when you type a "3" into the text field, it will not get wiped out. I would expect the setText("") method to clear it out, which it won't. And this is even when the keyTyped() method is invoking the same actionPerformed() method as the JTextButton.

Motivation here may help a little. I have a need to trap one particular hot-key which will clear out the JTextField at the moment it is typed, just as if you pressed the "clear" button. And this doesn't seem to work.

I haven't done that much with Swing before, but this is quite puzzling.

My SSCCE code follows:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

class P2 implements KeyListener, ActionListener
{
  JTextField fld;
  JButton btn;
  JLabel  lbl;

  P2()
  {
    JFrame frm = new JFrame("Test");
           fld = new JTextField(10);
    JPanel pnl = new JPanel();
           btn = new JButton("Clear it out");
           lbl = new JLabel("This is a test");

    fld.addKeyListener(this);

    btn.addActionListener(this);

    frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frm.setSize(400,400);
    frm.setLayout(new FlowLayout() );
    pnl.add(fld);
    pnl.add(btn);
    pnl.add(lbl);
    frm.getContentPane().add(pnl);
    frm.setVisible(true);
  }
  public void keyPressed(KeyEvent ke) {}
  public void keyReleased(KeyEvent ke) {}
  public void keyTyped(KeyEvent ke)
  {
     System.out.println("got a pressed key");

//this is the setText method that ought to wipe clean the field comments:
    this.fld.setText("test");
    this.btn.setText("try again");
    this.lbl.setText("got a presseed key");


//toggle this comment to see the invocation of the action event:
//    this.actionPerformed(new ActionEvent( new Object(), 2, "test")  );

  }
  public void actionPerformed(ActionEvent ae)
  {
     fld.setText("");
     fld.selectAll();
  }
  public static void main(String[] args)
  {
    SwingUtilities.invokeLater
    (
      new Runnable()
      {
        public void run()
        {
          new P2();
        }
      }
    );
  }
}

解决方案

This behavior is due to the fact that the KeyEvent will be processed by the field after your KeyListener was fired. You can circumvent it by consuming the event via

ke.consume();

inside your method keyTyped.

Depending on your requirements another way would be to encapsulate the clearing calls inside a SwingUtilities.invokeLater which will be processed after your current event and thus clear the field after it was updated.

这篇关于JTextField的setText方法不能从KeyListener中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆