问题描述
package华容道;importjava.awt.*;importjava.awt.event.*;//主函数publicclassMain{publicstaticvoidmain(String[]args){newHua_Rong_Road();}}//人物按钮颜色classPersonextendsButtonimplementsFocusListener{intnumber;Colorc=newColor(255,245,170);Person(intnumber,Strings){super(s);setBackground(c);//人物的颜色背景是黄色this.number=number;c=getBackground();addFocusListener(this);//好像是焦点监听器}publicvoidfocusGained(FocusEvente){setBackground(Color.red);//只要单击该按钮则按钮变颜色}publicvoidfocusLost(FocusEvente){setBackground(c);//上一个按钮回复原先的颜色}}//华容道总类classHua_Rong_RoadextendsFrameimplementsMouseListener,KeyListener,ActionListener{Personperson[]=newPerson[10];Buttonleft,right,above,below;Buttonrestart=newButton("Start");//重新开始按钮publicHua_Rong_Road(){init();setBounds(100,100,320,360);setVisible(true);//设置Frame为可见,默认为不可见validate();addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEvente){System.exit(0);}});}publicvoidinit(){setLayout(null);add(restart);restart.setBounds(100,320,120,25);restart.addActionListener(this);Stringname[]={"我","陆逊","姜维","陈宫","许攸","邓艾","周瑜","庞统","诸葛亮","贾诩"};for(intk=0;k<name.length;k++){person[k]=newPerson(k,name[k]);person[k].addMouseListener(this);person[k].addKeyListener(this);add(person[k]);}//为所有的按钮注册所需的东西person[0].setBounds(104,54,100,100);person[1].setBounds(104,154,100,50);person[2].setBounds(54,154,50,100);person[3].setBounds(204,154,50,100);person[4].setBounds(54,54,50,100);person[5].setBounds(204,54,50,100);person[6].setBounds(54,254,50,50);person[7].setBounds(204,254,50,50);person[8].setBounds(104,204,50,50);person[9].setBounds(154,204,50,50);//初始化按钮的位子person[0].requestFocus();left=newButton();right=newButton();above=newButton();below=newButton();left.setBounds(49,49,5,260);right.setBounds(254,49,5,260);above.setBounds(49,49,210,5);below.setBounds(49,304,210,5);validate();}publicvoidkeyTyped(KeyEvente){}publicvoidkeyReleased(KeyEvente){}publicvoidkeyPressed(KeyEvente){Personman=(Person)e.getSource();if(e.getKeyCode()==KeyEvent.VK_DOWN){go(man,below);}if(e.getKeyCode()==KeyEvent.VK_UP){go(man,above);}if(e.getKeyCode()==KeyEvent.VK_LEFT){go(man,left);}if(e.getKeyCode()==KeyEvent.VK_RIGHT){go(man,right);}}publicvoidmousePressed(MouseEvente){Personman=(Person)e.getSource();intx=-1,y=-1;x=e.getX();y=e.getY();intw=man.getBounds().width;inth=man.getBounds().height;if(y>h/2){go(man,below);}if(y<h/2){go(man,above);}if(x<w/2){go(man,left);}if(x>w/2){go(man,right);}}publicvoidmouseReleased(MouseEvente){}publicvoidmouseEntered(MouseEvente){}publicvoidmouseExited(MouseEvente){}publicvoidmouseClicked(MouseEvente){}publicvoidgo(Personman,Buttondirection){booleanmove=true;RectanglemanRect=man.getBounds();intx=man.getBounds().x;inty=man.getBounds().y;if(direction==below)y=y+50;elseif(direction==above)y=y-50;elseif(direction==left)x=x-50;elseif(direction==right)x=x+50;manRect.setLocation(x,y);RectangledirectionRect=direction.getBounds();for(intk=0;k<10;k++){RectanglepersonRect=person[k].getBounds();if((manRect.intersects(personRect))&&(man.number!=k)){move=false;}}if(manRect.intersects(directionRect)){move=false;}if(move==true){man.setLocation(x,y);}}publicvoidactionPerformed(ActionEvente){dispose();newHua_Rong_Road();}}
解决方案
解决方案二:
游戏?华容道~
解决方案三:
你写的?
解决方案四:
恩是啊我就只能写些么多了
解决方案五:
你看看这个:importjava.awt.*;importjava.awt.event.*;publicclassMoveExample{publicstaticvoidmain(Stringargs[]){newHua_Rong_Road();}}classPersonextendsButtonimplementsFocusListener{intnumber;Colorc=newColor(255,245,170);Person(intnumber,Strings){super(s);setBackground(c);this.number=number;c=getBackground();addFocusListener(this);}publicvoidfocusGained(FocusEvente){setBackground(Color.red);}publicvoidfocusLost(FocusEvente){setBackground(c);}}classHua_Rong_RoadextendsFrameimplementsMouseListener,KeyListener,ActionListener{Personperson[]=newPerson[10];Buttonleft,right,above,below;Buttonrestart=newButton("重新开始");publicHua_Rong_Road(){init();setBounds(100,100,320,360);setVisible(true);validate();addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEvente){System.exit(0);}});}publicvoidinit(){setLayout(null);add(restart);restart.setBounds(100,320,120,25);restart.addActionListener(this);Stringname[]={"曹操","关羽","张飞","刘备","周瑜","黄忠","兵","兵","兵","兵"};for(intk=0;k<name.length;k++){person[k]=newPerson(k,name[k]);person[k].addMouseListener(this);person[k].addKeyListener(this);add(person[k]);}person[0].setBounds(104,54,100,100);person[1].setBounds(104,154,100,50);person[2].setBounds(54,154,50,100);person[3].setBounds(204,154,50,100);person[4].setBounds(54,54,50,100);person[5].setBounds(204,54,50,100);person[6].setBounds(54,254,50,50);person[7].setBounds(204,254,50,50);person[8].setBounds(104,204,50,50);person[9].setBounds(154,204,50,50);person[9].requestFocus();left=newButton();right=newButton();above=newButton();below=newButton();add(left);add(right);add(above);add(below);left.setBounds(49,49,5,260);right.setBounds(254,49,5,260);above.setBounds(49,49,210,5);below.setBounds(49,304,210,5);validate();}publicvoidkeyTyped(KeyEvente){}publicvoidkeyReleased(KeyEvente){}publicvoidkeyPressed(KeyEvente){Personman=(Person)e.getSource();if(e.getKeyCode()==KeyEvent.VK_DOWN){go(man,below);}if(e.getKeyCode()==KeyEvent.VK_UP){go(man,above);}if(e.getKeyCode()==KeyEvent.VK_LEFT){go(man,left);}if(e.getKeyCode()==KeyEvent.VK_RIGHT){go(man,right);}}publicvoidmousePressed(MouseEvente){Personman=(Person)e.getSource();intx=-1,y=-1;x=e.getX();y=e.getY();intw=man.getBounds().width;inth=man.getBounds().height;if(y>h/2){go(man,below);}if(y<h/2){go(man,above);}if(x<w/2){go(man,left);}if(x>w/2){go(man,right);}}publicvoidmouseReleased(MouseEvente){}publicvoidmouseEntered(MouseEvente){}publicvoidmouseExited(MouseEvente){}publicvoidmouseClicked(MouseEvente){}publicvoidgo(Personman,Buttondirection){booleanmove=true;RectanglemanRect=man.getBounds();intx=man.getBounds().x;inty=man.getBounds().y;if(direction==below)y=y+50;elseif(direction==above)y=y-50;elseif(direction==left)x=x-50;elseif(direction==right)x=x+50;manRect.setLocation(x,y);RectangledirectionRect=direction.getBounds();for(intk=0;k<10;k++){RectanglepersonRect=person[k].getBounds();if((manRect.intersects(personRect))&&(man.number!=k)){move=false;}}if(manRect.intersects(directionRect)){move=false;}if(move==true){man.setLocation(x,y);}}publicvoidactionPerformed(ActionEvente){dispose();newHua_Rong_Road();}}
解决方案六:
不知不知
解决方案七:
华容道貌似不错正在想做点什么呢
解决方案八:
解决方案九:
要求首先编写一个按钮的子类,该子类创建的对象代表华容道中的人物。通过焦点事件控制人物的颜色,当人物获得焦点时颜色为蓝色,当失去焦点时颜色为灰色。我们通过键盘事件和鼠标事件来实现曹操,关羽等人物的移动,当人物上发生鼠标事件或键盘事件时,如果鼠标指针的位置是在人物的下方(也就是组件的下半部分)或按键盘上的“↓”键,该人物向下移动。这种事件触发的方法你不认为很不对吗!!!!
解决方案十:
写的太神奇了。。。
解决方案十一:
额我照书上看的写的要不我哪能编这么好啊
解决方案十二:
跟我们书里的例题一模一样