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 变小,但是却没反应,哪里出错呢??
解决方案 »
- 怎样实现倒计时开始/暂停/结束功能
- synchronized的问题
- 初来CSDN,有问题!请指教!菜鸟!!
- 我想通过一次比赛来提高自已,但是这个题一些细节问题看不懂,请个位高手指点一下!!
- 求助用poi操作Excel时合并行的时候设置的植被覆盖
- 听说《core java》流行两个中文版本,第六版和第七版,我注意到译者不同,不知道哪本更好?
- 请问applet能不能对显示的图像进行编辑,例如改变某个区域的颜色,如何实现,谢谢!
- 请问有在windows2003server下开发JSP的同志一个问题!
- JComboBox加入图片,但一下拉图片就显示不出了
- "BorrowerInformation.java": Error #: 300 : class Persistent not found in class bo.BorrowerInformation
- 判断素数疑问
- 关于System.out.println()直接输出对象的引用
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();
}}
应该是
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());
add(c); //不要忘了在Frame里添加c,否则当然看不到你要的效果
setVisible(true); }