开发者社区> 问答> 正文

Swing事件的一些小问题? 400 报错

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();
  
  }
 }
}

展开
收起
爱吃鱼的程序员 2020-06-03 14:25:54 430 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB
    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();
        }
    }


    2020-06-03 16:42:59
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载