java 华容道

wish198 2013-05-04 12:55:49
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class A extends JFrame implements FocusListener{

JButton[] jbutton;
public void init()
{
jbutton[0].setBounds(100,350,100,30);
jbutton[1].setBounds(100,50,100,100);
jbutton[2].setBounds(50,50,50,100);
jbutton[3].setBounds(200,50,50,100);
jbutton[4].setBounds(100,150,100,50);
jbutton[5].setBounds(50,150,50,100);
jbutton[6].setBounds(200,150,50,100);
jbutton[7].setBounds(100,200,50,50);
jbutton[8].setBounds(150,200,50,50);
jbutton[9].setBounds(50,250,50,50);
jbutton[10].setBounds(200,250,50,50);

jbutton[11].setBounds(50,45,200,5);
jbutton[12].setBounds(45,50,5,250);
jbutton[13].setBounds(250,50,5,250);
jbutton[14].setBounds(50,300,200,5);
}
public A(){

Container cn=getContentPane();
cn.setLayout(null);setSize(400,500);

jbutton=new JButton[15];
jbutton[0]=new JButton("重新开始");

jbutton[1]=new JButton("曹操");
jbutton[2]=new JButton("周");
jbutton[3]=new JButton("黄");
jbutton[4]=new JButton("关羽");
jbutton[5]=new JButton("张");
jbutton[6]=new JButton("刘");
jbutton[7]=new JButton("兵");
jbutton[8]=new JButton("兵");
jbutton[9]=new JButton("兵");
jbutton[10]=new JButton("兵");
jbutton[11]=new JButton();
jbutton[12]=new JButton();
jbutton[13]=new JButton();
jbutton[14]=new JButton();
add(jbutton[0]);
add(jbutton[11]);
add(jbutton[12]);
add(jbutton[13]);
add(jbutton[14]);
init();
jbutton[0].addActionListener(new ActionListener (){
public void actionPerformed(ActionEvent e){
init();
}
});
for(int i=1;i<11;++i)
{
add(jbutton[i]);
jbutton[i].setBackground(new Color(255,245,170));
jbutton[i].addFocusListener(this);
}

for(int i=1;i<11;++i){
jbutton[i].addKeyListener(new KeyAdapter()
{

public void keyPressed(KeyEvent e){
int z=0;
JButton jbntemp=(JButton)e.getSource();//选中按钮副本
Rectangle r=jbntemp.getBounds();
int x=r.x;
int y=r.y;
if (e.getKeyCode()==KeyEvent.VK_UP)
{
for(int j=1;j<15;++j){
Rectangle c=jbutton[j].getBounds();
if(c!=r)
{
r.setLocation(x,y-50);
if (r.intersects(c))
{
z=1;
break;
}

}
}

if(z==0)
jbntemp.setLocation(x,y-50);
}
if (e.getKeyCode()==KeyEvent.VK_DOWN)
{
for(int j=1;j<15;++j){
Rectangle c=jbutton[j].getBounds();
if(c!=r)
{
r.setLocation(x,y+50);
if (r.intersects(c))
{
z=1;
break;
}

}
}

if(z==0)
jbntemp.setLocation(x,y+50);

}
if (e.getKeyCode()==KeyEvent.VK_LEFT)
{
for(int j=1;j<15;++j){
Rectangle c=jbutton[j].getBounds();
if(c!=r)
{
r.setLocation(x-50,y);
if (r.intersects(c))
{
z=1;
break;
}

}
}

if(z==0)
jbntemp.setLocation(x-50,y);
}
if (e.getKeyCode()==KeyEvent.VK_RIGHT)
{
for(int j=1;j<15;++j){
Rectangle c=jbutton[j].getBounds();
if(c!=r)
{
r.setLocation(x+50,y);
if (r.intersects(c))
{
z=1;
break;
}

}
}

if(z==0)
jbntemp.setLocation(x+50,y);
}
}
});
}
setVisible(true);
}
public void focusGained(FocusEvent e)
{

((JButton)e.getSource()).setBackground(Color.red);
}
public void focusLost(FocusEvent e)
{
((JButton)e.getSource()).setBackground(new Color(255,245,170));
}
public static void main(String args[]){
A a=new A();
}

}求大神指导 错哪了
...全文
86 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

50,523

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧