?? example18_13.java
字號(hào):
import java.applet.*;import java.awt.*;import java.awt.event.*;public class Example18_13 extends Applet implements KeyListener,ActionListener{Label label=new Label("點(diǎn)擊一個(gè)積木,然后移動(dòng)它"); Button b[]=new Button[40]; int x[]=new int[40]; int y[]=new int[40]; String s=null; public void init() { setLayout(null); add(label); label.setBounds(0,0,150,30); int k1=70,k2=70,k3=70,k4=70; for(int i=0;i<40;i++) {b[i]=new Button(String.valueOf(i)); if(i%3==0) b[i].setBackground(Color.red); if(i%3==1) b[i].setBackground(Color.blue); if(i%3==2) b[i].setBackground(Color.yellow); b[i].addKeyListener(this);//按鈕獲得鍵盤事件監(jiān)視器. b[i].addActionListener(this); add(b[i]); if(i%4==0) {b[i].setBounds(k1,40,30,30);k1=k1+31;} else if(i%4==1) {b[i].setBounds(k2,71,30,30);k2=k2+31;} else if(i%4==2) {b[i].setBounds(k3,102,30,30);k3=k3+31;} else if(i%4==3) {b[i].setBounds(k4,133,30,30);k4=k4+31;} } for(int i=0;i<40;i++) { x[i]=b[i].getBounds().x;y[i]=b[i].getBounds().y;//獲取按鈕左上角的x,y坐標(biāo)。 } } public void keyTyped(KeyEvent e) { } public void keyPressed(KeyEvent e) {if(e.getKeyCode()==KeyEvent.VK_UP) {for(int i=0;i<=39;i++) { if(s.equals(String.valueOf(i))) {y[i]=y[i]-2; if(y[i]<=0) y[i]=0; b[i].setLocation(x[i],y[i]); } } } else if(e.getKeyCode()==KeyEvent.VK_DOWN) {for(int i=0;i<=39;i++) {if(s.equals(String.valueOf(i))) {y[i]=y[i]+2; if(y[i]>=300) y[i]=300; b[i].setLocation(x[i],y[i]); } } } else if(e.getKeyCode()==KeyEvent.VK_LEFT) {for(int i=0;i<=39;i++) {if(s.equals(String.valueOf(i))) {x[i]=x[i]-2; if(x[i]<=0) x[i]=0; b[i].setLocation(x[i],y[i]); } } } else if(e.getKeyCode()==KeyEvent.VK_RIGHT) {for(int i=0;i<=39;i++) {if(s.equals(String.valueOf(i))) {x[i]=x[i]+2; if(x[i]>=300) x[i]=300; b[i].setLocation(x[i],y[i]); } } } } public void keyReleased(KeyEvent e) {} public void actionPerformed(ActionEvent e) { for(int i=0;i<40;i++) {if(e.getSource()==b[i]) s=b[i].getLabel(); } }}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -