日期:2014-05-20 浏览次数:21317 次
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();
    }
}
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();
    }
}
------解决方案--------------------