java按钮随机移动_关于java按钮移动的问题

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;public classA extends JFrame implements FocusListener{

JButton jbn[]=new JButton[14];

JButton jbn14=new JButton("重新开始");publicA(){

jbn[0]=new JButton("周");

jbn[1]=new JButton("曹操");

jbn[2]=new JButton("黄");

jbn[3]=new JButton("张");

jbn[4]=new JButton("关羽");

jbn[5]=new JButton("刘");

jbn[6]=new JButton("兵");

jbn[7]=new JButton("兵");

jbn[8]=new JButton("兵");

jbn[9]=new JButton("兵");

jbn[10]=newJButton();

jbn[11]=newJButton();

jbn[12]=newJButton();

jbn[13]=newJButton();

Container cn=getContentPane();

cn.setLayout(null);

jbn[10].setBounds(50,290,200,5);

cn.add(jbn[10]);

jbn[11].setBounds(45,35,210,5);

cn.add(jbn[11]);

jbn[12].setBounds(45,40,5,255);

cn.add(jbn[12]);

jbn[13].setBounds(250,40,5,255);

cn.add(jbn[13]);

jbn[0].setBounds(50,40,50,100);

cn.add(jbn[0]);

jbn[1].setBounds(100,40,100,100);

cn.add(jbn[1]);

jbn[2].setBounds(200,40,50,100);

cn.add(jbn[2]);

jbn[3].setBounds(50,140,50,100);

cn.add(jbn[3]);

jbn[4].setBounds(100,140,100,50);

cn.add(jbn[4]);

jbn[5].setBounds(200,140,50,100);

cn.add(jbn[5]);

jbn[6].setBounds(100,190,50,50);

cn.add(jbn[6]);

jbn[7].setBounds(150,190,50,50);

cn.add(jbn[7]);

jbn[8].setBounds(50,240,50,50);

cn.add(jbn[8]);

jbn[9].setBounds(200,240,50,50);

cn.add(jbn[9]);

jbn14.setBounds(100,320,100,20);

cn.add(jbn14);for(int i=0;i<10;i++){

jbn[i].setBackground(new Color(255,245,170));

jbn[i].addFocusListener(this);

}for(int i=0;i

jbn[i].addKeyListener(newKeyAdapter(){public voidkeyPressed(KeyEvent e){

JButton jbntemp=(JButton)e.getSource();

Rectangle r=jbntemp.getBounds();int x=r.x;int y=r.y;int z=0;if (e.getKeyCode()==KeyEvent.VK_UP){for(int j=0;j

Rectangle c=jbn[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=0;j

Rectangle c=jbn[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=0;j

Rectangle c=jbn[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=0;j

Rectangle c=jbn[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);

}

}

});

}

jbn14.addActionListener(newActionListener(){public voidactionPerformed(ActionEvent e){

jbn[10].setBounds(50,290,200,5);

cn.add(jbn[10]);

jbn[11].setBounds(45,35,210,5);

cn.add(jbn[11]);

jbn[12].setBounds(45,40,5,255);

cn.add(jbn[12]);

jbn[13].setBounds(250,40,5,255);

cn.add(jbn[13]);

jbn[0].setBounds(50,40,50,100);

cn.add(jbn[0]);

jbn[1].setBounds(100,40,100,100);

cn.add(jbn[1]);

jbn[2].setBounds(200,40,50,100);

cn.add(jbn[2]);

jbn[3].setBounds(50,140,50,100);

cn.add(jbn[3]);

jbn[4].setBounds(100,140,100,50);

cn.add(jbn[4]);

jbn[5].setBounds(200,140,50,100);

cn.add(jbn[5]);

jbn[6].setBounds(100,190,50,50);

cn.add(jbn[6]);

jbn[7].setBounds(150,190,50,50);

cn.add(jbn[7]);

jbn[8].setBounds(50,240,50,50);

cn.add(jbn[8]);

jbn[9].setBounds(200,240,50,50);

cn.add(jbn[9]);

}

});

setSize(300,400);

setVisible(true);

validate();

}public voidfocusGained(FocusEvent e)

{

((JButton)e.getSource()).setBackground(Color.red);

}public voidfocusLost(FocusEvent e)

{

((JButton)e.getSource()).setBackground(new Color(255,245,170));

}public static voidmain(String args[]){

A a=newA();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值