這個我試了的沒有任務(wù)問題,稀望對你有點幫助,記得類名要改為Hua_Rong_Road ,因為只有Hua_Rong_Road 這個類是公開的.另外包名也改下package xxxx(你自己建的包名),玩游戲時移動人物,用鍵盤(上下左右 ,--,--,上,下)操作,鼠標(biāo)是不能移動 人物的,照著我說的做,應(yīng)該是沒什么問題的:
成都創(chuàng)新互聯(lián)"三網(wǎng)合一"的企業(yè)建站思路。企業(yè)可建設(shè)擁有電腦版、微信版、手機版的企業(yè)網(wǎng)站。實現(xiàn)跨屏營銷,產(chǎn)品發(fā)布一步更新,電腦網(wǎng)絡(luò)+移動網(wǎng)絡(luò)一網(wǎng)打盡,滿足企業(yè)的營銷需求!成都創(chuàng)新互聯(lián)具備承接各種類型的網(wǎng)站設(shè)計、成都做網(wǎng)站項目的能力。經(jīng)過十余年的努力的開拓,為不同行業(yè)的企事業(yè)單位提供了優(yōu)質(zhì)的服務(wù),并獲得了客戶的一致好評。
package baidu.testfive;
import java.applet.Applet;
import java.awt.Button;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
class People extends Button implements FocusListener // 代表華容道人物的類。
{
Rectangle rect = null;
int left_x, left_y;// 按扭的左上角坐標(biāo).
int width, height; // 按扭的寬和高.
String name;
int number;
People(int number, String s, int x, int y, int w, int h, Hua_Rong_Road road)// 構(gòu)造函數(shù)
{
super(s);
name = s;
this.number = number;
left_x = x;
left_y = y;
width = w;
height = h;
setBackground(Color.orange);
road.add(this);
addKeyListener(road);
setBounds(x, y, w, h);
addFocusListener(this);
rect = new Rectangle(x, y, w, h);
}
public void focusGained(FocusEvent e) {
setBackground(Color.red);
}
public void focusLost(FocusEvent e) {
setBackground(Color.orange);
}
}
public class Hua_Rong_Road extends Applet implements KeyListener,
ActionListener {
People people[] = new People[10];
Rectangle left, right, above, below;// 華容道的邊界 .
Button restart = new Button("重新開始");
public void init() {
setLayout(null);
add(restart);
restart.setBounds(5, 5, 80, 25);
restart.addActionListener(this);
people[0] = new People(0, "曹操", 104, 54, 100, 100, this);// 構(gòu)造曹操
people[1] = new People(1, "關(guān)羽", 104, 154, 100, 50, this);// 構(gòu)造關(guān)羽
people[2] = new People(2, "張飛", 54, 154, 50, 100, this);
people[3] = new People(3, "劉備", 204, 154, 50, 100, this);
people[4] = new People(4, "張遼", 54, 54, 50, 100, this);
people[5] = new People(5, "曹仁", 204, 54, 50, 100, this);
people[6] = new People(6, "兵 ", 54, 254, 50, 50, this);
people[7] = new People(7, "兵 ", 204, 254, 50, 50, this);
people[8] = new People(8, "兵 ", 104, 204, 50, 50, this);
people[9] = new People(9, "兵 ", 154, 204, 50, 50, this);
people[9].requestFocus();
left = new Rectangle(49, 49, 5, 260);
people[0].setForeground(Color.white);
right = new Rectangle(254, 49, 5, 260);
above = new Rectangle(49, 49, 210, 5);
below = new Rectangle(49, 304, 210, 5);
}
public void paint(Graphics g) {// 畫出華容道的邊界:
g.setColor(Color.cyan);
g.fillRect(49, 49, 5, 260);// left.
g.fillRect(254, 49, 5, 260);// right.
g.fillRect(49, 49, 210, 5); // above.
g.fillRect(49, 304, 210, 5);// below.
// 提示曹操逃出位置和按鍵規(guī)則:
g.drawString("點擊相應(yīng)的人物,然后按鍵盤上的上下左右箭頭移動", 100, 20);
g.setColor(Color.red);
g.drawString("曹操到達該位置", 110, 300);
}
public void keyPressed(KeyEvent e) {
People man = (People) e.getSource();// 獲取事件源.
man.rect.setLocation(man.getBounds().x, man.getBounds().y);
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
man.left_y = man.left_y + 50; // 向下前進50個單位。
man.setLocation(man.left_x, man.left_y);
man.rect.setLocation(man.left_x, man.left_y);
// 判斷是否和其它人物或下邊界出現(xiàn)重疊,如果出現(xiàn)重疊就退回50個單位距離。
for (int i = 0; i 10; i++) {
if ((man.rect.intersects(people[i].rect)) (man.number != i)) {
man.left_y = man.left_y - 50;
man.setLocation(man.left_x, man.left_y);
man.rect.setLocation(man.left_x, man.left_y);
}
}
if (man.rect.intersects(below)) {
man.left_y = man.left_y - 50;
man.setLocation(man.left_x, man.left_y);
man.rect.setLocation(man.left_x, man.left_y);
}
}
if (e.getKeyCode() == KeyEvent.VK_UP) {
man.left_y = man.left_y - 50; // 向上前進50個單位。
man.setLocation(man.left_x, man.left_y);
man.rect.setLocation(man.left_x, man.left_y);
// 判斷是否和其它人物或上邊界出現(xiàn)重疊,如果出現(xiàn)重疊就退回50個單位距離。
for (int i = 0; i 10; i++) {
if ((man.rect.intersects(people[i].rect)) (man.number != i)) {
man.left_y = man.left_y + 50;
man.setLocation(man.left_x, man.left_y);
man.rect.setLocation(man.left_x, man.left_y);
}
}
if (man.rect.intersects(above)) {
man.left_y = man.left_y + 50;
man.setLocation(man.left_x, man.left_y);
man.rect.setLocation(man.left_x, man.left_y);
}
}
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
man.left_x = man.left_x - 50; // 向左前進50個單位。
man.setLocation(man.left_x, man.left_y);
man.rect.setLocation(man.left_x, man.left_y);
// 判斷是否和其它人物或左邊界出現(xiàn)重疊,如果出現(xiàn)重疊就退回50個單位距離。
for (int i = 0; i 10; i++) {
if ((man.rect.intersects(people[i].rect)) (man.number != i)) {
man.left_x = man.left_x + 50;
man.setLocation(man.left_x, man.left_y);
man.rect.setLocation(man.left_x, man.left_y);
}
}
if (man.rect.intersects(left)) {
man.left_x = man.left_x + 50;
man.setLocation(man.left_x, man.left_y);
man.rect.setLocation(man.left_x, man.left_y);
}
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
man.left_x = man.left_x + 50; // 向右前進50個單位。
man.setLocation(man.left_x, man.left_y);
man.rect.setLocation(man.left_x, man.left_y);
// 判斷是否和其它人物或右邊界出現(xiàn)重疊,如果出現(xiàn)重疊就退回50個單位距離。
for (int i = 0; i 10; i++) {
if ((man.rect.intersects(people[i].rect)) (man.number != i)) {
man.left_x = man.left_x - 50;
man.setLocation(man.left_x, man.left_y);
man.rect.setLocation(man.left_x, man.left_y);
}
}
if (man.rect.intersects(right)) {
man.left_x = man.left_x - 50;
man.setLocation(man.left_x, man.left_y);
man.rect.setLocation(man.left_x, man.left_y);
}
}
}
public void keyTyped(KeyEvent e) {
}
public void keyReleased(KeyEvent e) {
}
public void actionPerformed(ActionEvent e) {
this.removeAll();
this.init();
}
}
1:
import java.io.IOException;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
// 華容道原理的拼圖游戲。 利用輕組建的套用。
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Choice;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class MyMainFrame extends JFrame implements ActionListener {
/**
*
*/
private static final long serialVersionUID = 1L;
MyCanvas myCanvas;
JPanel panelNorth,panelPreview;
Button start,preview,set;
Container container;
public MyMainFrame() {//初使化
container=this.getContentPane();
start=new Button("開始");
start.addActionListener(this);
preview=new Button("預(yù)覽");
preview.addActionListener(this);
set = new Button("設(shè)置");
set.addActionListener(this);
panelPreview=new JPanel();
panelPreview.setLayout(null);
Icon icon=new ImageIcon ("images/pic_"+MyCanvas.pictureID+".jpg");
JLabel label=new JLabel(icon);
label.setBounds(0,0,400,400);
panelPreview.add(label);
panelNorth=new JPanel();
panelNorth.setBackground(Color.yellow);
panelNorth.add(start);
panelNorth.add(preview);
panelNorth.add(set);
myCanvas=new MyCanvas();
container.add(myCanvas,BorderLayout.CENTER);
container.add(panelNorth,BorderLayout.NORTH);
this.setTitle("成型拼圖小游戲-1212");
this.setLocation(300,200);
this.setSize(408,465);
this.setResizable(false);
this.setVisible(true);
this.setDefaultCloseOperation(3);
} //end of 初始化 構(gòu)造函數(shù)
public void actionPerformed(ActionEvent e) {
Button button=(Button)e.getSource();
if(button==start){
myCanvas.Start();
}else if(button==preview){
if(button.getLabel()=="預(yù)覽"){
container.remove(myCanvas);
container.add(panelPreview);
panelPreview.updateUI();
container.repaint();
button.setLabel("返回");
}else{
container.remove(panelPreview);
container.add(myCanvas);
container.repaint();
button.setLabel("預(yù)覽");
}
}else if(button==set){
Choice pic = new Choice();
//pic.add("QQ");
pic.add("美女");
int i=JOptionPane.showConfirmDialog(this,pic,"選擇圖片", JOptionPane.OK_CANCEL_OPTION);
//使用選擇對話框來進行選擇圖片。
if(i==JOptionPane.YES_OPTION){
MyCanvas.pictureID=pic.getSelectedIndex()+5;
myCanvas.reLoadPictrue();
Icon icon=new ImageIcon("images/pic_"+MyCanvas.pictureID+".jpg");
JLabel label=new JLabel(icon);
label.setBounds(0,0,400,400);
panelPreview.removeAll();
panelPreview.add(label);
panelPreview.repaint();
}
}
}
public static void main(String[] args) throws UnsupportedAudioFileException, LineUnavailableException, IOException
{
new MyMainFrame();
} //end of main
}
2:
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class MyCanvas extends JPanel implements MouseListener
{
/**
*
*/
private static final long serialVersionUID = 1L;
boolean hasAddActionListener=false;//設(shè)置方格的動作監(jiān)聽器的標(biāo)志位,TRUE為已經(jīng)添加上動作事件
Cell cell[];//定義方格
Rectangle cellNull;//定義空方格區(qū)域 是一個矩形類
public static int pictureID=4;// 當(dāng)前選擇的圖片代號
public MyCanvas() {
this.setLayout(null);
this.setSize(400,400);
cellNull=new Rectangle(300,300,100,100);//空方格區(qū)域在第三行每三列
cell=new Cell[16];
Icon icon;
for (int i = 0; i 4; i++) {
for(int j=0;j4;j++){
icon=new ImageIcon("images/pic_"+pictureID+"_"+(i*4+j+1)+".jpg");
cell[i*4+j]=new Cell(icon);
cell[i*4+j].setLocation(j*100,i*100);
this.add(cell[i*4+j]);
}
}
this.remove(cell[15]);//移除最后一個多余的方格
} //放置9張小圖片并且移調(diào)最后一張
public void reLoadPictrue(){//當(dāng)選擇其它圖形進行拼圖時,需重新加載新圖片
Icon icon;
for (int i = 0; i 4; i++) {
for(int j=0;j4;j++){
icon=new ImageIcon("images/pic_"+pictureID+"_"+(i*4+j+1)+".jpg");
cell[i*4+j].setIcon(icon);
}
}
}
public boolean isFinish(){//判斷是否拼合成功
for(int i=0;i15;i++)
{ int x=cell[i].getBounds().x;
int y=cell[i].getBounds().y;
if(y/100*4+x/100!=i)
return false;
} //end of for
return true;
}
public void Start(){//對方格進行重新排列,打亂順序
while(cell[0].getBounds().x=100cell[0].getBounds().y=100){//當(dāng)?shù)谝粋€方格距左上角較近時
int x=cellNull.getBounds().x;
int y=cellNull.getBounds().y;
int direction=(int)(Math.random()*4);//產(chǎn)生0-4,對應(yīng)空方格的上下左右移動
if(direction==0){//空方格左移動,與左側(cè)方格互換位置,左側(cè)方格右移動
x-=100;
if(test(x,y)){
for(int j=0;j15;j++){
if((cell[j].getBounds().x==x)(cell[j].getBounds().y==y)){//依次尋找左側(cè)的按鈕
cell[j].move("RIGHT",100);
cellNull.setLocation(x,y);
break;//找到后跳出for循環(huán)
}
}
}
}else if(direction==1){//RIGHT
x+=100;
if(test(x,y)){
for(int j=0;j15;j++){
if((cell[j].getBounds().x==x)(cell[j].getBounds().y==y)){
cell[j].move("LEFT",100);
cellNull.setLocation(x,y);
break;
}
}
}
}else if(direction==2){//UP
y-=100;
if(test(x,y)){
for(int j=0;j15;j++){
if((cell[j].getBounds().x==x)(cell[j].getBounds().y==y)){
cell[j].move("DOWN",100);
cellNull.setLocation(x,y);
break;
}
}
}
}else{//DOWN
y+=100;
if(test(x,y)){
for(int j=0;j15;j++){
if((cell[j].getBounds().x==x)(cell[j].getBounds().y==y)){
cell[j].move("UP",100);
cellNull.setLocation(x,y);
break;
}
}
}
}
}
if(!hasAddActionListener)//如果尚未添加動作事件,則添加
for(int i=0;i15;i++)//為第個方格添加動作事件,這樣單擊按鈕就能移動了
cell[i].addMouseListener(this);
hasAddActionListener=true;
}
private boolean test(int x,int y){
if((x=0x=200)||(y=0y=200))
return true;
else
return false;
}
public void mouseClicked(MouseEvent e) { }
public void mouseEntered(MouseEvent e) { }
public void mouseExited(MouseEvent e) { }
public void mouseReleased(MouseEvent e) { }
public void mousePressed(MouseEvent e) {
//方格的鼠標(biāo)事件,因為用到了MyCanvas中的一些方法,因此沒有在Cell類中處理鼠標(biāo)事件
Cell button=(Cell)e.getSource();
int x1=button.getBounds().x;//得到所單擊方格的坐標(biāo)
int y1=button.getBounds().y;
int x2=cellNull.getBounds().x;//得到空方格的坐標(biāo)
int y2=cellNull.getBounds().y;
if(x1==x2y1-y2==100)//進行比較,如果滿足條件則進行交換
button.move("UP",100);
else if(x1==x2y1-y2==-100)
button.move("DOWN",100);
else if(x1-x2==100y1==y2)
button.move("LEFT",100);
else if(x1-x2==-100y1==y2)
button.move("RIGHT",100);
else
return;//不滿足就不進行任何處理
cellNull.setLocation(x1,y1);
this.repaint();
if(this.isFinish()){//進行是否完成的判斷
JOptionPane.showMessageDialog(this,"景鋒恭喜你完成拼圖,加油!想繼續(xù)下一關(guān)么?");
for(int i=0;i15;i++)
cell[i].removeMouseListener(this);//如果已完成,撤消鼠標(biāo)事件,鼠標(biāo)單擊方格不在起作用
hasAddActionListener=false;
}
}
}
3:
import javax.swing.Icon;
import javax.swing.JButton;
public class Cell extends JButton {
/**
*
*/
private static final long serialVersionUID = 1L;
Cell(Icon icon){//實際為ICON
super(icon);
this.setSize(100,100);
}
public void move(String direction,int sleep){//方格的移動
if(direction=="UP"){
this.setLocation(this.getBounds().x,this.getBounds().y-100);
}else if(direction=="DOWN"){
this.setLocation(this.getBounds().x,this.getBounds().y+100);
}else if(direction=="LEFT"){
this.setLocation(this.getBounds().x-100,this.getBounds().y);
}else{
this.setLocation(this.getBounds().x+100,this.getBounds().y);
}
}
}
import java.awt.*;
import java.awt.event.*;
public class MoveExample
{
public static void main(String args[])
{
new Hua_Rong_Road();
}
}
class Person extends Button implements FocusListener
{
int number;
Color c = new Color(255,245,170);
Person(int number,String s)
{
super(s);
setBackground(c);
this.number = number;
c = getBackground();
addFocusListener(this);
}
public void focusGained(FocusEvent e)
{
setBackground(Color.red);
}
public void focusLost(FocusEvent e)
{
setBackground(c);
}
}
class Hua_Rong_Road extends Frame implements MouseListener,KeyListener,ActionListener
{
Person person[] = new Person[10];
Button left,right,above,below;
Button restart = new Button("重新開始");
public Hua_Rong_Road()
{
init();
setBounds(100,100,320,360);
setVisible(true);
validate();
addWindowListener( new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
}
public void init()
{
setLayout(null);
add(restart);
restart.setBounds(100,320,120,25);
restart.addActionListener(this);
String name[] = {"曹操","關(guān)羽","張飛","劉備","趙云","黃忠","兵","兵","兵","兵"};
for(int k = 0;kname.length;k++)
{
person[k] = new Person(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 = new Button();
right = new Button();
above = new Button();
below = new Button();
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();
}
public void keyTyped(KeyEvent e){}
public void keyReleased(KeyEvent e){}
public void keyPressed(KeyEvent e)
{
Person man = (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);
}
}
public void mousePressed(MouseEvent e)
{
Person man = (Person)e.getSource();
int x = -1,y = -1;
x = e.getX();
y = e.getY();
int w = man.getBounds().width;
int h = man.getBounds().height;
if(yh/2)
{
go(man,below);
}
if(yh/2)
{
go(man,above);
}
if(xw/2)
{
go(man,left);
}
if(xw/2)
{
go(man,right);
}
}
public void mouseReleased(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseClicked(MouseEvent e){}
public void go(Person man,Button direction)
{
boolean move = true;
Rectangle manRect = man.getBounds(); //什么意思??
int x = man.getBounds().x; //又不懂了
int y = man.getBounds().y;
if(direction==below)
{
y = y+50;
}
else if(direction==above)
{
y = y-50;
}
else if(direction==left)
{
x = x-50;
}
else if(direction==right)
{
x = x+50;
}
manRect.setLocation(x,y);
Rectangle directionRect = direction.getBounds();
for(int k = 0;k10;k++)
{
Rectangle personRect = 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);
}
}
public void actionPerformed(ActionEvent e)
{
dispose();
new Hua_Rong_Road();
}
}
這是我們課本上的,顏色不一樣,其他的都差不多,不過是用awt組件寫的,你應(yīng)該是要用swing寫的吧,照這個改改吧...
java課程設(shè)計題目及代碼分別是:
1、題目:計算器。設(shè)計內(nèi)容是設(shè)計一個圖形界面(GUI)的計算器應(yīng)用程序,完成簡單的算術(shù)運算。
設(shè)計要求是設(shè)計的計算器應(yīng)用程序可以完成家法、減法、乘法、除法和取余運算。且有小數(shù)點、正負號、求倒數(shù)、退格和清零功能。
2、代碼:
數(shù)字按鈕NumberButton類如下:
import java.awt.
import java.awt.event.
import javax.swing.
public class NumberButton extends Button.
{
int number.
public NumberButton(int number).
{
super(""+number).
this.number=number.
setForeground(Color.blue).
}
public int getNumber().
{
return number;
}
}
其它java課程設(shè)計題目及代碼是:
題目:華容道。編寫一個按鈕的子類,使用該子類創(chuàng)建的對象代表華容道中的人物。通過焦點事件控制人物顏色,當(dāng)人物獲得焦點時顏色為藍色,當(dāng)失去焦點時顏色為灰色。
通過鍵盤事件和鼠標(biāo)事件來實現(xiàn)曹操、關(guān)羽等人物的移動。當(dāng)人物上發(fā)生鼠標(biāo)事件或鍵盤事件時,如果鼠標(biāo)指針的位置是在人物的下方(也就是組件的下半部分)或按下鍵盤的“↓“鍵,該人物向下移動。向左、向右和向上的移動原理類似。
代碼是:
String name[]={"曹操","關(guān)羽","張","劉","馬","許","兵","兵","兵","兵"}.
for(int i=0;iname.length;i++).
{
person[i]=new Person(i,name[i]).
person[i].addKeyListener(this).
person[i].addMouseListener(this).
//? ? ?person[i].addFocusListener(new Person).
add(person[i]).
}
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);
package 華容道;
import java.awt.*;
import java.awt.event.*;
//主函數(shù)
public class Main {
public static void main(String[] args) {
new Hua_Rong_Road();
}
}
//人物按鈕顏色
class Person extends Button implements FocusListener{
int number;
Color c=new Color(255,245,170);
Person(int number,String s)
{
super(s);
setBackground(c);//人物的顏色背景是黃色
this.number=number;
c=getBackground();
addFocusListener(this);//好像是焦點監(jiān)聽器
}
public void focusGained(FocusEvent e)
{
setBackground(Color.red);//只要單擊該按鈕則按鈕變顏色
}
public void focusLost(FocusEvent e) {
setBackground(c);//上一個按鈕回復(fù)原先的顏色
}
}
//華容道總類
class Hua_Rong_Road extends Frame implements MouseListener,KeyListener,ActionListener{
Person person[] = new Person[10];
Button left,right,above,below;
Button restart = new Button("Start");//重新開始按鈕
public Hua_Rong_Road()
{
init();
setBounds(100,100,320,360);
setVisible(true);//設(shè)置Frame為可見,默認(rèn)為不可見
validate();
addWindowListener(
new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
}
public void init()
{
setLayout(null);
add(restart);
restart.setBounds(100, 320, 120, 25);
restart.addActionListener(this);
String name[]={"我","陸遜","姜維","陳宮","許攸","鄧艾","周瑜","龐統(tǒng)","諸葛亮","賈詡"};
for(int k=0;kname.length;k++)
{
person[k]=new Person(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=new Button();
right=new Button();
above=new Button();
below=new Button();
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();
}
public void keyTyped(KeyEvent e){}
public void keyReleased(KeyEvent e){}
public void keyPressed(KeyEvent e)
{
Person man=(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);
}
}
public void mousePressed(MouseEvent e)
{
Person man =(Person)e.getSource();
int x=-1,y=-1;
x=e.getX();
y=e.getY();
int w=man.getBounds().width;
int h=man.getBounds().height;
if(yh/2)
{
go(man,below);
}
if(yh/2)
{
go(man,above);
}
if(xw/2)
{
go(man,left);
}
if(xw/2)
{
go(man,right);
}
}
public void mouseReleased(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseClicked(MouseEvent e){}
public void go(Person man,Button direction)
{
boolean move=true;
Rectangle manRect=man.getBounds();
int x=man.getBounds().x;
int y=man.getBounds().y;
if(direction==below)
y=y+50;
else if(direction==above)
y=y-50;
else if(direction==left)
x=x-50;
else if(direction==right)
x=x+50;
manRect.setLocation(x,y);
Rectangle directionRect=direction.getBounds();
for(int k=0;k10;k++)
{
Rectangle personRect=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);
}
}
public void actionPerformed(ActionEvent e)
{
dispose();
new Hua_Rong_Road();
}
}
標(biāo)題名稱:華容道代碼解釋java 數(shù)字華容道代碼
標(biāo)題路徑:http://aaarwkj.com/article24/dooohce.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站排名、網(wǎng)站營銷、ChatGPT、服務(wù)器托管、企業(yè)網(wǎng)站制作、手機網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)