Java求助:keypressed无法被引用

里咿恩夜石 里咿恩夜石 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class jkll {

public static void createAndShowGUI() {
JFrame frame=new JFrame("test");
frame.setLayout(new FlowLayout());
frame.setLocation(300, 200);//确定窗口位置
JTextField tf=new JTextField(40);//创建文本框
frame.add(tf);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,300);
frame.setVisible(true);
tf.addKeyListener(new KeyAdapter() {
public void keypressed(KeyEvent e) {
char ch=e.getKeyChar();
System.out.println(ch);
}
});
}
public static void main(String[] args) {
SwingUtilities.invokeLater(jkll::createAndShowGUI);
}
}

代码如上,第十五行的keypressed一直报错“The method keypressed(KeyEvent e) from the new KeyAdapter() is not used locally”,该怎么解决?求助各位大佬
...全文
评论 3
收藏 已收藏
分享
返回顶部
里咿恩夜石
Java SE 62,620 成员 307,251 内容