Swing事件的一些小问题

kong_resty 发布于 2012/03/19 16:01
阅读 223
收藏 0

问题描述:

     我原来的目标:把这个程序运行起来, 然后按键盘上的方向键   打印 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();
  
  }
 }
}

加载中
0
Jeky
Jeky
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();
    }
}


返回顶部
顶部