import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class tss extends JFrame
{
ts p=new ts();
public tss()
{
getContentPane().add(p);
setSize(300,200);
setVisible(true);
}
}
class ts extends JPanel
{
public ts()
{
addKeyListener(new myevents());
}
private class myevents extends KeyAdapter
{
public void KeyPressed(KeyEvent e)
{
switch(e.getKeyCode())
{
case 37:Drawfff();break;
case 38:Drawfff();break;
case 39:Drawfff();break;
case 40:Drawfff();break;
}
}
}
public void Drawfff()
{
Graphics g=getGraphics();
g.drawRect((int)Math.random()*100,(int)Math.random()*100,20,20);
}
}
class ddd
{
public static void main(String args[])
{
new tss();
}
}
为何我按上下左右键窗体没任何反应?
import javax.swing.*;
import java.awt.event.*;
class tss extends JFrame
{
ts p=new ts();
public tss()
{
getContentPane().add(p);
setSize(300,200);
setVisible(true);
}
}
class ts extends JPanel
{
public ts()
{
addKeyListener(new myevents());
}
private class myevents extends KeyAdapter
{
public void KeyPressed(KeyEvent e)
{
switch(e.getKeyCode())
{
case 37:Drawfff();break;
case 38:Drawfff();break;
case 39:Drawfff();break;
case 40:Drawfff();break;
}
}
}
public void Drawfff()
{
Graphics g=getGraphics();
g.drawRect((int)Math.random()*100,(int)Math.random()*100,20,20);
}
}
class ddd
{
public static void main(String args[])
{
new tss();
}
}
为何我按上下左右键窗体没任何反应?
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货