“KeyPressed”事件被多次调用。

huangapple go评论74阅读模式
英文:

KeyPressed event is called multiple times

问题

我有一个jTextField,并向其添加了一个键盘监听器。因此,如果我输入一些内容并按下Enter键,将调用keypressed事件,然后我将保存输入的任何内容。

但是,如果我按下Enter键,它会被调用3次。

tPatientName.addKeyListener(new java.awt.event.KeyAdapter() {
    @Override
    public void keyPressed(java.awt.event.KeyEvent e) {
        if(e.getKeyCode() == KeyEvent.VK_ENTER) {
            System.out.println("保存成功");
        }
    }
});	

控制台输出:

保存成功
保存成功
保存成功
我不知道我的代码有什么问题。
提前致谢。
英文:

I have a jTextField and added a key listener to it. So, if I type something and press Enter key, the keypressed event will be called and then I will save whatever is typed.

But, if I press Enter key, it is called 3 times.

tPatientName.addKeyListener(new java.awt.event.KeyAdapter() {
		@Override
		public void keyPressed(java.awt.event.KeyEvent e) {
			  if(e.getKeyCode() == KeyEvent.VK_ENTER) {
				  System.out.println("Saved Successfully");
			  }
		}
	});	

Console output:

Saved Successfully
Saved Successfully
Saved Successfully

I don't know what is wrong in my code.
Thanks in advance.

答案1

得分: 1

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class EnterKeyTest
{
    JFrame frame;
    JPanel panel;
    JTextField tPatientName;
    
    public EnterKeyTest()
    {
        initGUI();
    }
    
    public static void main(String[] args)
    {
        EnterKeyTest e = new EnterKeyTest();
    }
    
    private void initGUI()
    {
        frame = new JFrame();
        panel = new JPanel();
        
        tPatientName = new JTextField(15);
        tPatientName.addKeyListener(new KeyAdapter()
        {
            @Override
            public void keyPressed(KeyEvent e)
            {
                if(e.getKeyCode() == KeyEvent.VK_ENTER)
                {
                    System.out.println("Saved Successfully");
                }
            }
        });
        
        panel.add(tPatientName);
        frame.add(panel);
        
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }
}
英文:

I created a MRE based on what you're telling us and it works without any problems whatsoever. Feel free to compare whatever mysterious code that you can't share with us with my little example.

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class EnterKeyTest
{
    JFrame frame;
    JPanel panel;
    JTextField tPatientName;
    
    public EnterKeyTest()
    {
        initGUI();
    }
    
    public static void main(String[] args)
    {
        EnterKeyTest e = new EnterKeyTest();
    }
    
    private void initGUI()
    {
        frame = new JFrame();
        panel = new JPanel();
        
        tPatientName = new JTextField(15);
        tPatientName.addKeyListener(new KeyAdapter()
        {
            @Override
            public void keyPressed(KeyEvent e)
            {
                if(e.getKeyCode() == KeyEvent.VK_ENTER)
                {
                    System.out.println("Saved Successfully");
                }
            }
        });
        
        panel.add(tPatientName);
        frame.add(panel);
        
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }
}

答案2

得分: 1

我找到了原因。我已将jTextField声明为静态。

我之前是这样写的:

public static JTextField tPatientName = new JTextField();

static关键字移除后,它就正常工作了。

但是,为什么static关键字会造成这种情况呢?

英文:

I found out the reason for it. I have declared the jTextField as static.

I had it like this:

public static JTextField tPatientName= new JTextField();

After removing the static keyword it worked.

But, why static keyword do this?

答案3

得分: 0

改将 keyPressed() 改为 keyReleased() 有帮助吗?

英文:

Does changing keyPressed() to keyReleased() help?

huangapple
  • 本文由 发表于 2020年9月10日 16:05:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/63825390.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定