有8个按钮,用键盘上的方向键来移动这些按钮。
请问怎么编写,
请高手帮帮忙!
----------------解决方案--------------------------------------------------------
华容道?
----------------解决方案--------------------------------------------------------
也可以,
拼图的也可以,就是用键盘能控制移动就行。
----------------解决方案--------------------------------------------------------
这是我的源代码,运行不出来,有错误。高手帮忙!
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class MYWin extends JFrame implements KeyListener,FocusListener{
JButton button[]=new JButton[8];
public MYWin(String s){
super(s);
Container con=getContentPane();
con.setLayout(new FlowLayout());
for(int i=0;i<=8;i++){
button[i].addKeyListener(this);
con.add(button[i]);
}
button[i].requestFocusInWindow();
button[i].addFocusListener(this);
setBounds(100,100,200,200);
validate();
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void FocusGained(FocusEvent e){
JButton button=(JButton)e.getSource();
}
public void FocusLost(FocusEvent e){}
public void KeyTyped(KeyEvent ee){
JButton button=(JButton)ee.getSource();
if(ee.getKeyCode()==KeyEvent.vK_LEFT){
}
else if(ee.getKeyCode()==KeyEvent.vK_RIGHT){
}
else if(ee.getKeyCode()==KeyEvent.vK_up){
}
else if(ee.getKeyCode()==KeyEvent.vK_DOWN){
}
}
public void KeyPressed(KeyEvent ee){}
public void KeyReleased(KeyEvent ee){}
}
}
class Example4{
public static void main(String args[]){
MYWin win=new MYWin("窗体");
}
}
----------------解决方案--------------------------------------------------------
我从本论坛下的拼图原代码
----------------解决方案--------------------------------------------------------
谢谢5楼!
----------------解决方案--------------------------------------------------------