请看这断代码运行后不能响应键盘事件,为什么? 因为你的jPanel1没有焦点。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 窗口上什么也没有,你希望哪里能响应你的事件呢?放个文本框试试会不会响应;粘过去执行看看package test;import java.awt.event.*;import javax.swing.*;import java.awt.*;public class Application1 { boolean packFrame = false; //Construct the application public Application1() { Frame1 frame = new Frame1(); frame.show(); } //Main method public static void main(String[] args) { new Application1(); }}class Frame1 extends JFrame { JPanel contentPane; BorderLayout borderLayout1 = new BorderLayout(); JPanel jPanel1 = new JPanel(); JTextField textField=new JTextField(); //Construct the frame public Frame1() { enableEvents(AWTEvent.WINDOW_EVENT_MASK); try { jbInit(); } catch(Exception e) { e.printStackTrace(); } } //Component initialization private void jbInit() throws Exception { contentPane = (JPanel) this.getContentPane(); contentPane.setLayout(borderLayout1); this.setSize(new Dimension(400, 300)); this.setTitle("Frame Title"); textField.setColumns(10); jPanel1.add(textField); jPanel1.addKeyListener(new KeyAdapter() { public void keyPressed(KeyEvent e) { jPanel1_keyPressed(e); } }); contentPane.add(jPanel1, BorderLayout.NORTH); } //Overridden so we can exit when window is closed protected void processWindowEvent(WindowEvent e) { super.processWindowEvent(e); if (e.getID() == WindowEvent.WINDOW_CLOSING) { System.exit(0); } } void jPanel1_keyPressed(KeyEvent e) { System.out.println("key pressed"); }} to zhouwj(阿杰):那你说该怎么改?是不是加requestFoucos(boolean)?我试过的,没有用。 要先设置jPanel1.setRequestFocusEnabled(true);然后jPanel1.requestFoucos();jPanel1就可以接收事件了 这是 1.4 的一个 Bug在 1.3 下就没有问题了 请教A udioFileFormat 能指定MP3 类型的吗? 怎么指定? JBuilder 字符串分析效率,求助! 请大家给我一点JAVA练习题,我是初学者 can not resolve symbol 错误(初学) 请教如何使JTree的每个节点对应到JTable的每一行~ 求助。使用spring的@service无法创建bean。代码配置如下 help!!! 数组问题一个???, 请问如果我想调试如下程序,在JB中应该如何操作?(立结) 怎样把string转成double(不是Double类)?(在线等待) 有关java编译的问题,急在线等待
粘过去执行看看
package test;import java.awt.event.*;
import javax.swing.*;
import java.awt.*;public class Application1 {
boolean packFrame = false; //Construct the application
public Application1() {
Frame1 frame = new Frame1();
frame.show();
}
//Main method
public static void main(String[] args) {
new Application1();
}
}class Frame1 extends JFrame {
JPanel contentPane;
BorderLayout borderLayout1 = new BorderLayout();
JPanel jPanel1 = new JPanel();
JTextField textField=new JTextField();
//Construct the frame
public Frame1() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(borderLayout1);
this.setSize(new Dimension(400, 300));
this.setTitle("Frame Title");
textField.setColumns(10);
jPanel1.add(textField);
jPanel1.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
jPanel1_keyPressed(e);
}
});
contentPane.add(jPanel1, BorderLayout.NORTH);
}
//Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
} void jPanel1_keyPressed(KeyEvent e) {
System.out.println("key pressed");
}
}
那你说该怎么改?是不是加requestFoucos(boolean)?我试过的,没有用。
然后jPanel1.requestFoucos();
jPanel1就可以接收事件了
在 1.3 下就没有问题了