首页 新闻 赞助 找找看

关于java按钮移动的问题

0
悬赏园豆:50 [已关闭问题] 关闭于 2017-05-04 22:09

这是本人写的代码,在运行时,若按钮移动距离小于按钮本身运动方向大小,则不能运动。

这是否是因为按钮运动时遇到自身重叠导致停止运动,还是代码本身存在错误?请求帮助

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class A extends JFrame implements FocusListener{
    JButton jbn[]=new JButton[14];
    
    JButton jbn14=new JButton("重新开始");
    
    public A(){
        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]=new JButton();
        jbn[11]=new JButton();
        jbn[12]=new JButton();
        jbn[13]=new JButton();
        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.length;i++){
            jbn[i].addKeyListener(new KeyAdapter(){
                public void keyPressed(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<jbn.length;++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<jbn.length;++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<jbn.length;++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<jbn.length;++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(new ActionListener(){
            public void actionPerformed(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 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();
    }
}
木易哒哒哒的主页 木易哒哒哒 | 菜鸟二级 | 园豆:202
提问于:2017-04-15 18:58
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册