当前位置: 代码迷 >> J2SE >> 这个监听器如何没效果?
  详细解决方案

这个监听器如何没效果?

热度:63   发布时间:2016-04-24 00:43:29.0
这个监听器怎么没效果??
Java code
import java.awt.*;import java.awt.event.KeyEvent;import java.awt.event.*;import javax.swing.*;import javax.swing.event.*;public class App_0622 extends JFrame{    /**     * @param args     */private JLabel lbl;     Container c=getContentPane();         public App_0622()     {         super("Image");         c.setLayout(new FlowLayout());         lbl=new JLabel(new ImageIcon("2.png"));         c.add(lbl);         //setSize(460,650);         c.addKeyListener(new hand());         setVisible(true);     }    class hand extends KeyAdapter     {         public void keypressed(KeyEvent e)         {             if(e.getKeyChar()=='r')             {                 for(int i=0;i<30;i++)                 {                     try                     {                     Thread.sleep(70);                     c.setSize(16*i,22*i);                     }                     catch(InterruptedException q)                     {                                           }                 }             }             if(e.getKeyChar()=='p')             {                 for( int i=29;i>0;i--)                 {                     try                     {                     Thread.sleep(70);                     c.setSize(16*i,22*i);                     }                     catch(InterruptedException q)                     {                                           }                 }             }         }     }    public static void main(String[] args)     {        // TODO Auto-generated method stub        App_0622 App=new App_0622();    }}


想实现程序运行后,按下r 窗口变大,按下p 变小,但是却没反应,哪里出错呢??

------解决方案--------------------
Java code
import java.awt.*;import java.awt.event.KeyEvent;import java.awt.event.*;import javax.swing.*;import javax.swing.event.*;public class App_0622 extends JFrame{    /**     * @param args     */private JLabel lbl;     Container c=getContentPane();         public App_0622()     {         super("Image");         c.setLayout(new FlowLayout());         lbl=new JLabel(new ImageIcon("2.png"));         c.add(lbl);         //setSize(460,650);         addKeyListener(new hand());    //这里应该是给JFrame加监听         setVisible(true);     }    class hand extends KeyAdapter     {         public void keyPressed(KeyEvent e)    //这里写错了,你写成keypressed了         {             if(e.getKeyChar()=='r')             {                 System.out.println("r");                 for(int i=0;i<30;i++)                 {                     try                     {                     Thread.sleep(70);                     setSize(16*i,22*i);    //这里应该是设置JFrame的大小                     }                     catch(InterruptedException q)                     {                                           }                 }             }             if(e.getKeyChar()=='p')             {                 System.out.println("p");                 for( int i=29;i>0;i--)                 {                     try                     {                     Thread.sleep(70);                     setSize(16*i,22*i);    //这里应该是设置JFrame的大小                     }                     catch(InterruptedException q)                     {                                           }                 }             }         }     }    public static void main(String[] args)     {        // TODO Auto-generated method stub        App_0622 App=new App_0622();    }}
------解决方案--------------------
探讨
Java code


import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.*;
import javax.swing.*;
  相关解决方案