Swing事件的一些小问题? 400 报错
问题描述:
我原来的目标:把这个程序运行起来, 然后按键盘上的方向键 打印 wocao 字符串
运行结果:按方向键无任何反映啊
package Ztest2;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Demo2 extends JFrame implements KeyListener,ActionListener{
private String direction;
private JButton jb = new JButton("按钮1");
public Demo2(String direction){
this.direction = direction;
this.setLayout(new BorderLayout());
this.setSize(400,200);
jb.addActionListener(this);
this.add(jb,BorderLayout.NORTH);
this.setVisible(true);
this.addKeyListener(this);
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyPressed(KeyEvent e) {
int k = e.getKeyCode();
System.out.println(k);
if(k==KeyEvent.VK_RIGHT){
System.out.println(direction);
}else if(k==KeyEvent.VK_LEFT){
System.out.println(direction);
}else if(k==KeyEvent.VK_DOWN){
System.out.println(direction);
}else if(k==KeyEvent.VK_UP){
System.out.println(direction);
}
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
public static void main(String[] args) {
Demo2 d2 = new Demo2("wocao");
}
@Override
public void actionPerformed(ActionEvent e) {
Object o1 =e.getSource();
//System.out.println(o1);
if(o1==jb){
requestFocusInWindow();
Thread t = new Thread(){
public void run(){
while(true){
System.out.println("haha");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
};
t.start();
}
}
}
package Ztest2; import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import javax.swing.JButton; import javax.swing.JFrame; public class Demo2 extends JFrame implements KeyListener, ActionListener { private String direction; private JButton jb = new JButton("按钮1"); public Demo2(String direction) { this.direction = direction; this.setLayout(new BorderLayout()); this.setSize(400, 200); jb.addActionListener(this); this.add(jb, BorderLayout.NORTH); this.setVisible(true); this.addKeyListener(this); this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.requestFocusInWindow(); } @Override public void keyTyped(KeyEvent e) { } @Override public void keyPressed(KeyEvent e) { int k = e.getKeyCode(); System.out.println(k); if (k == KeyEvent.VK_RIGHT) { System.out.println(direction); } else if (k == KeyEvent.VK_LEFT) { System.out.println(direction); } else if (k == KeyEvent.VK_DOWN) { System.out.println(direction); } else if (k == KeyEvent.VK_UP) { System.out.println(direction); } } @Override public void keyReleased(KeyEvent e) { } public static void main(String[] args) { Demo2 d2 = new Demo2("wocao"); } @Override public void actionPerformed(ActionEvent e) { Object o1 = e.getSource(); System.out.println(o1); if (o1 == jb) { Thread t = new Thread() { public void run() { while (true) { System.out.println("haha"); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } }; t.start(); } this.requestFocusInWindow(); } }
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。