import java.awt.*;
importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;publicclassyidong2impleme...
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class yidong2 implements KeyListener{
private Frame jf;
private Panel p;
Button button1;
Button button2;
Button button3;
Button button4;
public yidong2(){
jf=new Frame("I'm moving");
p=new Panel();
jf.add(p);
jf.setSize(300,300);
jf.setVisible(true);
jf.setBackground(Color.red);
jf.addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent e){System.exit(0);}});
init();
}
public void init(){
p.add(button1);
p.add(button2);
p.add(button3);
p.add(button4);
button1.setLabel("a");
button1.setLabel("b");
button1.setLabel("c");
button1.setLabel("d");
button1.addKeyListener(this);
button2.addKeyListener(this);
button3.addKeyListener(this);
button4.addKeyListener(this);
}
public void KeyListener(KeyEvent e){
int x,y;
Button button=(Button)e.getSource();
x=button.getBounds().x;
y=button.getBounds().y;
if(e.getKeyCode()==KeyEvent.VK_LEFT)
x=x-2;
if(x<=0)
x=0;
}
public void keyReleased(KeyEvent e){}
public void keyPressed(KeyEvent e){}
public void keyTyped(KeyEvent e){}
public static void main(String args[]){
try{
yidong2 ni=new yidong2();
}
catch(Exception e){}
}
} 展开
import java.awt.event.*;
import javax.swing.*;
public class yidong2 implements KeyListener{
private Frame jf;
private Panel p;
Button button1;
Button button2;
Button button3;
Button button4;
public yidong2(){
jf=new Frame("I'm moving");
p=new Panel();
jf.add(p);
jf.setSize(300,300);
jf.setVisible(true);
jf.setBackground(Color.red);
jf.addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent e){System.exit(0);}});
init();
}
public void init(){
p.add(button1);
p.add(button2);
p.add(button3);
p.add(button4);
button1.setLabel("a");
button1.setLabel("b");
button1.setLabel("c");
button1.setLabel("d");
button1.addKeyListener(this);
button2.addKeyListener(this);
button3.addKeyListener(this);
button4.addKeyListener(this);
}
public void KeyListener(KeyEvent e){
int x,y;
Button button=(Button)e.getSource();
x=button.getBounds().x;
y=button.getBounds().y;
if(e.getKeyCode()==KeyEvent.VK_LEFT)
x=x-2;
if(x<=0)
x=0;
}
public void keyReleased(KeyEvent e){}
public void keyPressed(KeyEvent e){}
public void keyTyped(KeyEvent e){}
public static void main(String args[]){
try{
yidong2 ni=new yidong2();
}
catch(Exception e){}
}
} 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询