推箱子 | Java课程设计

推箱子 |  Java课程设计

忙了快20天了,终于把推箱子的游戏做完了。自己第一次做这种项目,收获还是比较多的。先把项目交给大家,和面还会 有几篇博客,是关于我在项目中遇到的问题以及解决办法,如果有想法的可以关注我。

文章目录

项目结构

推箱子 |  Java课程设计
这个项目我是用eclipse写的,我这个项目文件我会以压缩包的形式,放在下方,链接是百度网盘的链接,可能程序运行过程中,有些bug,我自己还未发现。欢迎大家指出

链接: https://pan.baidu.com/s/1sh9TZememG-L30RfL-VnmQ
提取码: 1p6m

视频展示:

演示视频入口

源码分享

PlayGameInterface.java

package JavaClassDesign;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Date;
import java.sql.Time;
import java.text.SimpleDateFormat;
import java.util.TimerTask;

import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;


public class PlayGameInterface extends JFrame implements KeyListener{
	//1墙  0空地  4箱子  12有箱子的目的地  8空目的地   2玩家
    //  定义静态变量来实现对关卡的计数
	static int k=0;
	static int step=0;
	Boolean playFlag  =true;
	int width = 20;
	int height = 20;
	ClipTestClass musicFlag,musicFlagtmp;
	JComboBox<String> comboBox ;
	Boolean stopflag = true;
	String musicFlagString;
	String musicFlagTmpString;
	JLabel jl8;
	
    public PlayGameInterface() throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException{
    	k++;
    	step=0;
		ReadMap(k);
		UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		setIconImage(new ImageIcon("src/Img/电脑.png").getImage());
		setLayout(null);
		setTitle("这是第 "+k+"关"+"\t"+"祝你好运!");
    	Setplay(); //人物
    	Setbox();  //箱子
    	Setdestination(); //目的地
    	Setwall();
    	getContentPane().setBackground(Color.white);
    	
    	ImageIcon image1 = new ImageIcon("src/Img/音乐.png");
        image1.setImage(image1.getImage().getScaledInstance(width, height, Image.SCALE_DEFAULT));
        JLabel jl1 = new JLabel(image1);
        JLabel jl2 = new JLabel();
        jl2.setText("当前播放的音乐:");
        jl2.setFont(new Font("楷体",Font.BOLD, 13));
        jl2.setForeground(Color.red);
        JPanel jp1 = new JPanel();
        jp1.add(jl1);
        jp1.add(jl2);
        jp1.setBounds(500+10, 10, 150, 20);
        add(jp1);
        
        String[] listData = new String[]{"5 Seconds Of Summer - Teeth.wav", "冰轩 - 另类 (DJ版伴奏).wav",
        		"华语群星 - 爸爸去哪儿.wav", "宋厦 - 早晚.wav","Hawk Nelson - Sold Out.wav",
        		"SHAUN (숀) - Way Back Home (Mosimann Remix).wav","Vicetone _ Meron Ryan - Walk Thru Fire.wav"};

        // 创建一个下拉列表框
        comboBox = new JComboBox<String>(listData);
        
        comboBox.setFocusable(false);
        // 添加条目选中状态改变的监听器
        comboBox.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                // 只处理选中的状态
                if (e.getStateChange() == ItemEvent.SELECTED) {
                    try {
						if(playFlag) {
							if(musicFlagtmp!=null)
							musicFlagtmp.stop();
							musicFlag = new ClipTestClass("src/Music/"+comboBox.getSelectedItem());
							musicFlagString = (String) comboBox.getSelectedItem();
							musicFlag.player();
							playFlag=false;
							
						}else if(!playFlag) {
							if(musicFlag!=null)
							musicFlag.stop();
							musicFlagtmp = new ClipTestClass("src/Music/"+comboBox.getSelectedItem());
							musicFlagTmpString=(String) comboBox.getSelectedItem();
							musicFlagtmp.player();
							playFlag=true;
						}
					} catch (UnsupportedAudioFileException | IOException | LineUnavailableException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
                    
                }
            }
        });
        // 设置默认选中的条目
        comboBox.setSelectedIndex(k);
        comboBox.setBounds(500+10,40,150,20);
        add(comboBox);
        
        ImageIcon image3 = new ImageIcon("src/Img/上一关.png");
        image3.setImage(image3.getImage().getScaledInstance(width, height, Image.SCALE_DEFAULT));
        JLabel jl3 = new JLabel(image3);
        jl3.addMouseListener(new MouseAdapter() {

			@Override
			public void mouseClicked(MouseEvent e) {
				if(comboBox.getSelectedIndex()>0);{
					if(comboBox.getSelectedItem()==musicFlagString) {
						musicFlag.stop();
					}else
						musicFlagtmp.stop();
					comboBox.setSelectedIndex(comboBox.getSelectedIndex()-1);
				}
			}
        	
		});
        jl3.setBounds(500+10, 70, 30, 30);
        add(jl3);
        
        ImageIcon image4 = new ImageIcon("src/Img/暂停.png");
        ImageIcon image44 = new ImageIcon("src/Img/开始.png");
        image4.setImage(image4.getImage().getScaledInstance(width, height, Image.SCALE_DEFAULT));
        image44.setImage(image44.getImage().getScaledInstance(width, height, Image.SCALE_DEFAULT));
        JLabel jl4 = new JLabel(image4);
        jl4.addMouseListener(new MouseAdapter() {

			@Override
			public void mouseClicked(MouseEvent e) {
				if(comboBox.getSelectedItem()==musicFlagString) {
					musicFlag.stop();
				}else
					musicFlagtmp.stop();
				// TODO Auto-generated method stub
				if(stopflag) {
					jl4.setIcon(image44);
					stopflag=false;
					if(comboBox.getSelectedItem()==musicFlagString) {
						musicFlag.stop();
					}else
						musicFlagtmp.stop();
				}else if(!stopflag) {
					jl4.setIcon(image4);
					stopflag=true;
					if(comboBox.getSelectedItem()==musicFlagString) {
						try {
							musicFlag.player();
						} catch (UnsupportedAudioFileException | IOException | LineUnavailableException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
					} else
						try {
							musicFlagtmp.player();
						} catch (UnsupportedAudioFileException | IOException | LineUnavailableException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
				}
			}
        	
		});
        jl4.setBounds(500+65, 70, 30, 30);
        add(jl4);
        
        
        ImageIcon image5 = new ImageIcon("src/Img/下一关.png");
        image5.setImage(image5.getImage().getScaledInstance(width, height, Image.SCALE_DEFAULT));
        JLabel jl5 = new JLabel(image5);
        jl5.addMouseListener(new MouseAdapter() {

			@Override
			public void mouseClicked(MouseEvent e) {
				if(comboBox.getSelectedIndex()<6);{
					if(comboBox.getSelectedItem()==musicFlagString) {
						musicFlag.stop();
					}else
						musicFlagtmp.stop();
					comboBox.setSelectedIndex(comboBox.getSelectedIndex()+1);
				}
			}
        	
		});
        jl5.setBounds(500+120, 70, 30, 30);
        add(jl5);
        
        AddButton();
        
        Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize();// 获取屏幕大小
		int screenWidth = (int) screensize.getWidth();
		int screenHeight = (int) screensize.getHeight();
		setLocation(screenWidth / 2 -340, screenHeight / 2 -275);// 设置窗口居中显示
		setSize(500+180, 550);
		setVisible(true);
        this.addKeyListener(this);
    }
	
    private void AddButton() {
    	JButton jb1 = new JButton();
        jb1.setBounds(500+10,110,150,30);
        jb1.setText("上一关");
        jb1.addMouseListener(new MouseAdapter() {

			@Override
			public void mouseClicked(MouseEvent e) {
				if(k>1) {
					try {
						k-=2;
						if(musicFlag!=null)
							 musicFlag.stop();
						 if(musicFlagtmp!=null)
							 musicFlagtmp.stop();
						 CloseThis();
						new PlayGameInterface();
					} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | IOException
							| UnsupportedLookAndFeelException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}
			}
        	
		});
        jb1.setFont(new Font("楷体",Font.BOLD,13));
        jb1.setFocusable(false);
        add(jb1);
		
        JButton jb2 = new JButton();
        jb2.setBounds(500+10,150,150,30);
        jb2.setText("下一关");
        jb2.addMouseListener(new MouseAdapter() {

			@Override
			public void mouseClicked(MouseEvent e) {
				if(k<4) {
					try {
						if(musicFlag!=null)
							 musicFlag.stop();
						 if(musicFlagtmp!=null)
							 musicFlagtmp.stop();
						 CloseThis();
						new PlayGameInterface();
					} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | IOException
							| UnsupportedLookAndFeelException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}
			}
        	
		});
        jb2.setFont(new Font("楷体",Font.BOLD,13));
        jb2.setFocusable(false);
        add(jb2);
        
        JLabel jl1 = new JLabel("选择你要运行的关卡:");
        jl1.setBounds(510,190,150,30);
        jl1.setFont(new Font("楷体",Font.BOLD,13));
        add(jl1);
        
        String[] level = new String[]{"1.txt","2.txt","3.txt","4.txt"};
        JComboBox<String> mapLevel = new JComboBox<>(level);
        mapLevel.setFocusable(false);
        mapLevel.setBounds(510, 230, 150, 20);
        mapLevel.addItemListener(new ItemListener() {
			
			@Override
			public void itemStateChanged(ItemEvent e) {
				if(k!=mapLevel.getSelectedIndex()+1) {
					k=mapLevel.getSelectedIndex();
					try {
						if(musicFlag!=null)
							 musicFlag.stop();
						 if(musicFlagtmp!=null)
							 musicFlagtmp.stop();
						 CloseThis();
						new PlayGameInterface();
					} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | IOException
							| UnsupportedLookAndFeelException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}
			}
		});
        mapLevel.setSelectedIndex(k-1);
        add(mapLevel);
        
        JButton jb3 = new JButton();
        jb3.setBounds(500+10,260,150,30);
        jb3.setText("重玩");
        jb3.addMouseListener(new MouseAdapter() {

			@Override
			public void mouseClicked(MouseEvent e) {
				try {
					k--;
					if(musicFlag!=null)
						 musicFlag.stop();
					 if(musicFlagtmp!=null)
						 musicFlagtmp.stop();
					 CloseThis();
					new PlayGameInterface();
				} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | IOException
						| UnsupportedLookAndFeelException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
        	
		});
        jb3.setFont(new Font("楷体",Font.BOLD,13));
        jb3.setFocusable(false);
        add(jb3);
        
        JLabel jl2 = new JLabel("当前时间:");
        jl2.setBounds(510, 300, 150, 30);
        jl2.setFont(new Font("楷体",Font.BOLD,13));
        jl2.setFocusable(false);
        add(jl2);
        
        long currentTime1 = System.currentTimeMillis();
        SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
        Date date = new Date(currentTime1);
        JLabel jl33 = new JLabel(formatter.format(date));
        jl33.setBounds(510, 330, 150, 30);
        jl33.setFont(new Font("楷体",Font.BOLD,13));
        jl33.setFocusable(false);
        add(jl33);
        
        JLabel jl5 = new JLabel("你已经玩了:");
        jl5.setBounds(510, 370, 100, 30);
        jl5.setFont(new Font("楷体",Font.BOLD,13));
        jl5.setFocusable(false);
        add(jl5);
        
        JLabel jl6 = new JLabel();
        jl6.setBounds(610, 370, 50, 30);
        jl6.setFont(new Font("楷体",Font.BOLD,13));
        jl6.setFocusable(false);
        add(jl6);
       
        JLabel jl7 = new JLabel("你已经走了:");
        jl7.setBounds(510, 400, 100, 30);
        jl7.setFont(new Font("楷体",Font.BOLD,13));
        jl7.setFocusable(false);
        add(jl7);
        
        jl8 = new JLabel(step+"步");
        jl8.setBounds(610, 400, 50, 30);
        jl8.setFont(new Font("楷体",Font.BOLD,13));
        jl8.setFocusable(false);
        add(jl8);
        
        Timer time = new Timer(1000,new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				long currentTime = System.currentTimeMillis();
		        SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
		        Date date = new Date(currentTime);
				jl33.setText(formatter.format(date));
				jl6.setText(String.valueOf((currentTime-currentTime1)/1000)+"秒");
			}
		});
        time.start();
    }
    
	protected void CloseThis() {
		this.dispose();
	}

	public PlayGameInterface(int[][] map ,JLabel[][] box) throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException{
    	this.boxs=box;
    	this.wall=map;
    	UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    	setIconImage(new ImageIcon("src/Img/电脑.png").getImage());
		setLayout(null);
		setTitle("这是自定义地图祝你好运!");
    	Setplay(); //人物
    	Setbox();  //箱子
    	Setdestination(); //目的地
    	Setwall();
    	getContentPane().setBackground(Color.white);
    	
    	ImageIcon image1 = new ImageIcon("src/Img/音乐.png");
        image1.setImage(image1.getImage().getScaledInstance(width, height, Image.SCALE_DEFAULT));
        JLabel jl1 = new JLabel(image1);
        JLabel jl2 = new JLabel();
        jl2.setText("当前播放的音乐:");
        jl2.setFont(new Font("楷体",Font.BOLD, 13));
        jl2.setForeground(Color.red);
        JPanel jp1 = new JPanel();
        jp1.add(jl1);
        jp1.add(jl2);
        jp1.setBounds(500+10, 10, 150, 20);
        add(jp1);
        
        String[] listData = new String[]{"5 Seconds Of Summer - Teeth.wav", "冰轩 - 另类 (DJ版伴奏).wav",
        		"华语群星 - 爸爸去哪儿.wav", "宋厦 - 早晚.wav","Hawk Nelson - Sold Out.wav",
        		"SHAUN (숀) - Way Back Home (Mosimann Remix).wav","Vicetone _ Meron Ryan - Walk Thru Fire.wav"};

        // 创建一个下拉列表框
        comboBox = new JComboBox<String>(listData);
        comboBox.setFocusable(false);
        
        // 添加条目选中状态改变的监听器
        comboBox.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                // 只处理选中的状态
                if (e.getStateChange() == ItemEvent.SELECTED) {
                    try {
						if(playFlag) {
							if(musicFlagtmp!=null)
							musicFlagtmp.stop();
							musicFlag = new ClipTestClass("src/Music/"+comboBox.getSelectedItem());
							musicFlagString = (String) comboBox.getSelectedItem();
							musicFlag.player();
							playFlag=false;
							
						}else if(!playFlag) {
							if(musicFlag!=null)
							musicFlag.stop();
							musicFlagtmp = new ClipTestClass("src/Music/"+comboBox.getSelectedItem());
							musicFlagTmpString=(String) comboBox.getSelectedItem();
							musicFlagtmp.player();
							playFlag=true;
						}
					} catch (UnsupportedAudioFileException | IOException | LineUnavailableException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
                    
                }
            }
        });
        // 设置默认选中的条目
        comboBox.setSelectedIndex(2);
        comboBox.setBounds(500+10,40,140,20);
        add(comboBox);
        
        ImageIcon image3 = new ImageIcon("src/Img/上一关.png");
        image3.setImage(image3.getImage().getScaledInstance(width, height, Image.SCALE_DEFAULT));
        JLabel jl3 = new JLabel(image3);
        jl3.addMouseListener(new MouseAdapter() {

			@Override
			public void mouseClicked(MouseEvent e) {
				if(comboBox.getSelectedIndex()>0);{
					if(comboBox.getSelectedItem()==musicFlagString) {
						musicFlag.stop();
					}else
						musicFlagtmp.stop();
					comboBox.setSelectedIndex(comboBox.getSelectedIndex()-1);
				}
			}
        	
		});
        jl3.setBounds(500+10, 70, 30, 30);
        add(jl3);
        
        ImageIcon image4 = new ImageIcon("src/Img/暂停.png");
        ImageIcon image44 = new ImageIcon("src/Img/开始.png");
        image4.setImage(image4.getImage().getScaledInstance(width, height, Image.SCALE_DEFAULT));
        image44.setImage(image44.getImage().getScaledInstance(width, height, Image.SCALE_DEFAULT));
        JLabel jl4 = new JLabel(image4);
        jl4.addMouseListener(new MouseAdapter() {

			@Override
			public void mouseClicked(MouseEvent e) {
				if(comboBox.getSelectedItem()==musicFlagString) {
					musicFlag.stop();
				}else
					musicFlagtmp.stop();
				// TODO Auto-generated method stub
				if(stopflag) {
					jl4.setIcon(image44);
					stopflag=false;
					if(comboBox.getSelectedItem()==musicFlagString) {
						musicFlag.stop();
					}else
						musicFlagtmp.stop();
				}else if(!stopflag) {
					jl4.setIcon(image4);
					stopflag=true;
					if(comboBox.getSelectedItem()==musicFlagString) {
						try {
							musicFlag.player();
						} catch (UnsupportedAudioFileException | IOException | LineUnavailableException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
					} else
						try {
							musicFlagtmp.player();
						} catch (UnsupportedAudioFileException | IOException | LineUnavailableException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
				}
			}
        	
		});
        jl4.setBounds(500+65, 70, 30, 30);
        add(jl4);
        
        
        ImageIcon image5 = new ImageIcon("src/Img/下一关.png");
        image5.setImage(image5.getImage().getScaledInstance(width, height, Image.SCALE_DEFAULT));
        JLabel jl5 = new JLabel(image5);
        jl5.addMouseListener(new MouseAdapter() {

			@Override
			public void mouseClicked(MouseEvent e) {
				if(comboBox.getSelectedIndex()<6);{
					if(comboBox.getSelectedItem()==musicFlagString) {
						musicFlag.stop();
					}else
						musicFlagtmp.stop();
					comboBox.setSelectedIndex(comboBox.getSelectedIndex()+1);
				}
			}
        	
		});
        jl5.setBounds(500+120, 70, 30, 30);
        add(jl5);
        
        AddButton();
        
        Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize();// 获取屏幕大小
		int screenWidth = (int) screensize.getWidth();
		int screenHeight = (int) screensize.getHeight();
        setLocation(screenWidth / 2 -340, screenHeight / 2 -275);// 设置窗口居中显示
		setSize(680, 550);
		setVisible(true);
        this.addKeyListener(this);
    }
	private void ReadMap(int k) throws IOException {
		DataInputStream cin = new DataInputStream(new FileInputStream("src/JavaClassDesign/"+k+".txt"));
		String b = "";
		int j=0;
		while((b=cin.readLine())!=null) {
			char[] a=b.toCharArray();
			for(int i=0;i<a.length;i++) {
				wall[j][i]=a[i]-'0';
			}
			j++;
		}
		cin.close();
	}

	JLabel[][] boxs=new JLabel[14][14];
	int[][] wall = new int[14][14];
	
	int px;//横向
	int py;//纵向
	int num=0;
	int boxnum=0;
	
	private void win() throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException{
		if (num==boxnum){
			Object[] options={"下一关","返回"};
			 int response=JOptionPane.showOptionDialog ( null, "你的选择","接下来",JOptionPane.YES_OPTION ,JOptionPane.PLAIN_MESSAGE, null, options, options[0] ) ;
			 if(response==0) {
				 if(musicFlag!=null)
					 musicFlag.stop();
				 if(musicFlagtmp!=null)
					 musicFlagtmp.stop();
				 this.dispose();
				 if(k<4)
				 new PlayGameInterface();
			 }
			 if(response==1) {
				 k=0;
				 if(musicFlag!=null)
					 musicFlag.stop();
				 if(musicFlagtmp!=null)
					 musicFlagtmp.stop();
				 this.dispose();
				 if(k<4)
				 new PlayGameInterface();
			 }
		}
	}
	//墙
    private void Setwall() {                        
		// TODO Auto-generated method stub
    	Icon w=new ImageIcon("src/Img/wall.png");
    	for(int i=0;i<wall.length;i++)
    		for(int j=0;j<wall.length;j++){
    			if(wall[i][j]==1){
    			JLabel wall=new JLabel(w);
    			wall.setBounds(30*j, 30*i, 30, 30);
    			this.add(wall);
    			}
    		}
	}
    //目的地
	private void Setdestination() {                  
		// TODO Auto-generated method stub
    	Icon i=new ImageIcon("src/Img/destination.png");
    	for(int i1=0;i1<wall.length;i1++)
    		for(int j=0;j<wall.length;j++){
    			if(wall[i1][j]==8) {
    				JLabel ds1=new JLabel(i);
        			ds1.setBounds(j*30, i1*30, 30, 30);
        			this.add(ds1);
    			}
    		};
	}
   //箱子
	private void Setbox() {                        
		// TODO Auto-generated method stub
    	Icon i=new ImageIcon("src/Img/box1.png");
    	for(int i1=0;i1<wall.length;i1++)
    		for(int j=0;j<wall.length;j++){
    			if(wall[i1][j]==4) {
    				boxnum++;
    				JLabel box1=new JLabel(i);
        			box1.setBounds(j*30, i1*30, 30, 30);
        			this.add(box1);
        			boxs[i1][j]=box1;
    			}
    		};
	}
	
	//玩家
	private void Setplay() {                       
		// TODO Auto-generated method stub
		Icon i=new ImageIcon("src/Img/player.png");
		for(int i1=0;i1<wall.length;i1++)
    		for(int j=0;j<wall.length;j++){
    			if(wall[i1][j]==2) {
    				px=j;
    				py=i1;
    				play=new JLabel(i);
    				play.setBounds(30*px, 30*py, 30, 30);
    				this.add(play);
    				wall[i1][j]=0;
    			}
    		}
	}
    JLabel play;
    JLabel b;

@Override
public void keyPressed(KeyEvent e) {
	step++;
	jl8.setText(step+"步");
	// TODO Auto-generated method stub
	int key=e.getKeyCode();
//***********************************************************//
	if(key==39){ //右
		if(wall[py][px+1]==1)return;                      //人物 墙
		if(wall[py][px+1]==4&&wall[py][px+2]==4)return;   //人物  箱子 箱子
	    if(wall[py][px+1]==4&&wall[py][px+2]==1) return;  //人物 箱子 墙
	    if(wall[py][px+1]==12&&wall[py][px+2]==1)return;        //人物   有箱子的目的地  墙
	    if(wall[py][px+1]==12&&wall[py][px+2]==12)return;        //人物   有箱子的目的地  有箱子的目的地
	    if(wall[py][px+1]==0||wall[py][px+1]==8){         //人物  空地  或  人物  空目的地
			px++;
		   int x=(int)play.getLocation().getX();
		   int y=(int)play.getLocation().getY();
		   play.setLocation(x+30,y);
		   return;
		}
/*****************************************************************/
	    if(wall[py][px+1]==4&&wall[py][px+2]==0){        // 人物  箱子 空地 。
			wall[py][px+1]=0;                         
			wall[py][px+2]=4;
			}
/*****************************************************************/
		if(wall[py][px+1]==4&&wall[py][px+2]==8){ 
			wall[py][px+1]=0;                         //推动: 人物  箱子  空目的地 
			wall[py][px+2]=12;
		    boxs[py][px+1].setLocation(px*30+60, py*30);
		    Icon i=new ImageIcon("src/Img/box2.png");
		    boxs[py][px+1].setIcon(i);
			boxs[py][px+2]=boxs[py][px+1];
			boxs[py][px+1]=null;
			px++;
			int x=(int)play.getLocation().getX();
			int y=(int)play.getLocation().getY();
			play.setLocation(x+30,y);
			num++;
			try {
				win();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (ClassNotFoundException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (InstantiationException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (IllegalAccessException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (UnsupportedLookAndFeelException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		    return;
		}
/*****************************************************************/
	   if(wall[py][px+1]==12&&wall[py][px+2]==0){ 
		          wall[py][px+1]=8;                   //人物  目标箱子  空地
		          wall[py][px+2]=4;
		          boxs[py][px+1].setLocation(px*30+60, py*30);
				    Icon i=new ImageIcon("src/Img/box1.png");
				    boxs[py][px+1].setIcon(i);
					boxs[py][px+2]=boxs[py][px+1];
					boxs[py][px+1]=null;
					px++;
					int x=(int)play.getLocation().getX();
					int y=(int)play.getLocation().getY();
					play.setLocation(x+30,y);
					num--;
					try {
						win();
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (ClassNotFoundException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (InstantiationException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (IllegalAccessException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (UnsupportedLookAndFeelException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				    return;
		}
/*****************************************************************/
	   if(wall[py][px+1]==12&&wall[py][px+2]==8){ 
		     wall[py][px+1]=8;                         // 人物  有箱子的目的地  空目的地 
		     wall[py][px+2]=12;
	   }
/*****************************************************************/
	    boxs[py][px+1].setLocation(px*30+60, py*30);
		boxs[py][px+2]=boxs[py][px+1];
		boxs[py][px+1]=null;
		px++;
		int x=(int)play.getLocation().getX();
		int y=(int)play.getLocation().getY();
		play.setLocation(x+30,y);
		return;
	}
//*************************************************************//
	if(key==37){ //左
		if(wall[py][px-1]==1)return;                      //人物 墙
		if(wall[py][px-1]==4&&wall[py][px-2]==4)return;   //人物  箱子 箱子
	    if(wall[py][px-1]==4&&wall[py][px-2]==1) return;  //人物 箱子 墙
	    if(wall[py][px-1]==12&&wall[py][px-2]==1)return;        //人物   有箱子的目的地  墙
	    if(wall[py][px-1]==12&&wall[py][px-2]==12)return;        //人物   有箱子的目的地  有箱子的目的地
	    if(wall[py][px-1]==0||wall[py][px-1]==8){         //人物  空地  或  人物  空目的地
			px--;
		   int x=(int)play.getLocation().getX();
		   int y=(int)play.getLocation().getY();
		   play.setLocation(x-30,y);
		   return;
		}
/*****************************************************************/
	    if(wall[py][px-1]==4&&wall[py][px-2]==0){        // 人物  箱子 空地 。
			wall[py][px-1]=0;                         
			wall[py][px-2]=4;
			}
/*****************************************************************/
		if(wall[py][px-1]==4&&wall[py][px-2]==8){ 
			wall[py][px-1]=0;                         //推动: 人物  箱子  空目的地 
			wall[py][px-2]=12;
			boxs[py][px-1].setLocation(px*30-60, py*30);
			 Icon i=new ImageIcon("src/Img/box2.png");
			    boxs[py][px-1].setIcon(i);
			boxs[py][px-2]=boxs[py][px-1];
			boxs[py][px-1]=null;
			px--;
			int x=(int)play.getLocation().getX();
			int y=(int)play.getLocation().getY();
			play.setLocation(x-30,y);
			num++;
			try {
				win();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (ClassNotFoundException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (InstantiationException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (IllegalAccessException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (UnsupportedLookAndFeelException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			return;
			
		}
/*****************************************************************/
	   if(wall[py][px-1]==12&&wall[py][px-2]==0){ 
		          wall[py][px-1]=8;                   //人物  目标箱子  空地
		          wall[py][px-2]=4;
	              num--;
		}
/*****************************************************************/
	   if(wall[py][px-1]==12&&wall[py][px-2]==8){ 
		     wall[py][px-1]=8;                         // 人物  有箱子的目的地  空目的地 
		     wall[py][px-2]=12;
	   }
/*****************************************************************/
	    boxs[py][px-1].setLocation(px*30-60, py*30);
		boxs[py][px-2]=boxs[py][px-1];
		boxs[py][px-1]=null;
		px--;
		int x=(int)play.getLocation().getX();
		int y=(int)play.getLocation().getY();
		play.setLocation(x-30,y);
		return;
	}
//************************************************************//
	if(key==38){  //上
		if(wall[py-1][px]==1)return;                      //人物 墙
		if(wall[py-1][px]==4&&wall[py-2][px]==4)return;   //人物  箱子 箱子
	    if(wall[py-1][px]==4&&wall[py-2][px]==1) return;  //人物 箱子 墙
	    if(wall[py-1][px]==12&&wall[py-2][px]==1)return;        //人物   有箱子的目的地  墙
	    if(wall[py-1][px]==12&&wall[py-2][px]==12)return;        //人物   有箱子的目的地  有箱子的目的地
/*****************************************************************/
	    if(wall[py-1][px]==0||wall[py-1][px]==8){         //人物  空地  或  人物  空目的地
			py--;
		   int x=(int)play.getLocation().getX();
		   int y=(int)play.getLocation().getY();
		   play.setLocation(x,y-30);
		   return;
		}
/*****************************************************************/
	    if(wall[py-1][px]==4&&wall[py-2][px]==0){        // 人物  箱子 空地 。
			wall[py-1][px]=0;                         
			wall[py-2][px]=4;
			}
/*****************************************************************/
		if(wall[py-1][px]==4&&wall[py-2][px]==8){ 
			wall[py-1][px]=0;                         //推动: 人物  箱子  空目的地 
			wall[py-2][px]=12;
			boxs[py-1][px].setLocation(px*30, py*30-60);
			 Icon i=new ImageIcon("src/Img/box2.png");
			    boxs[py-1][px].setIcon(i);
			boxs[py-2][px]=boxs[py-1][px];
			boxs[py-1][px]=null;
			py--;
			int x=(int)play.getLocation().getX();
			int y=(int)play.getLocation().getY();
			play.setLocation(x,y-30);
			num++;
			try {
				win();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (ClassNotFoundException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (InstantiationException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (IllegalAccessException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (UnsupportedLookAndFeelException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			return;
		}
/*****************************************************************/
	   if(wall[py-1][px]==12&&wall[py-2][px]==0){ 
		          wall[py-1][px]=8;                   //人物  目标箱子  空地
		          wall[py-2][px]=4;
	              num--;
		}
/*****************************************************************/
	   if(wall[py-1][px]==12&&wall[py-2][px]==8){ 
		     wall[py-1][px]=8;                         // 人物  有箱子的目的地  空目的地 
		     wall[py-2][px]=12;
	   }
/*****************************************************************/
	    boxs[py-1][px].setLocation(px*30, py*30-60);
		boxs[py-2][px]=boxs[py-1][px];
		boxs[py-1][px]=null;
		py--;
		int x=(int)play.getLocation().getX();
		int y=(int)play.getLocation().getY();
		play.setLocation(x,y-30);
		try {
			win();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (ClassNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (InstantiationException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (IllegalAccessException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (UnsupportedLookAndFeelException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		return;
	}
//************************************************************//
	if(key==40){   //下
		if(wall[py+1][px]==1)return;                      //人物 墙
		if(wall[py+1][px]==4&&wall[py+2][px]==4)return;   //人物  箱子 箱子
	    if(wall[py+1][px]==4&&wall[py+2][px]==1) return;  //人物 箱子 墙
	    if(wall[py+1][px]==12&&wall[py+2][px]==1)return;        //人物   有箱子的目的地  墙
	    if(wall[py+1][px]==12&&wall[py+2][px]==12)return;        //人物   有箱子的目的地  有箱子的目的地
	    if(wall[py+1][px]==0||wall[py+1][px]==8){         //人物  空地  或  人物  空目的地
			py++;
		   int x=(int)play.getLocation().getX();
		   int y=(int)play.getLocation().getY();
		   play.setLocation(x,y+30);
		   return;
		}
/*****************************************************************/
	    if(wall[py+1][px]==4&&wall[py+2][px]==0){        // 人物  箱子 空地 。
			wall[py+1][px]=0;                         
			wall[py+2][px]=4;
			}
/*****************************************************************/
		if(wall[py+1][px]==4&&wall[py+2][px]==8){ 
			wall[py+1][px]=0;                         //推动: 人物  箱子  空目的地 
			wall[py+2][px]=12;
			 boxs[py+1][px].setLocation(px*30, py*30+60);
			 Icon i=new ImageIcon("src/Img/box2.png");
			    boxs[py+1][px].setIcon(i);
				boxs[py+2][px]=boxs[py+1][px];
				boxs[py+1][px]=null;
				py++;
				int x=(int)play.getLocation().getX();
				int y=(int)play.getLocation().getY();
				play.setLocation(x,y+30);
			    num++;
			    try {
					win();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (ClassNotFoundException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (InstantiationException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (IllegalAccessException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (UnsupportedLookAndFeelException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			    return;
		}
/*****************************************************************/
	   if(wall[py+1][px]==12&&wall[py+2][px]==0){ 
		          wall[py+1][px]=8;                   //人物  目标箱子  空地
		          wall[py+2][px]=4;
	              num--;
		}
/*****************************************************************/
	   if(wall[py+1][px]==12&&wall[py+2][px]==8){ 
		     wall[py+1][px]=8;                         // 人物  有箱子的目的地  空目的地 
		     wall[py+2][px]=12;
	   }
/*****************************************************************/
	    boxs[py+1][px].setLocation(px*30, py*30+60);
		boxs[py+2][px]=boxs[py+1][px];
		boxs[py+1][px]=null;
		py++;
		int x=(int)play.getLocation().getX();
		int y=(int)play.getLocation().getY();
		play.setLocation(x,y+30);
		try {
			win();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (ClassNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (InstantiationException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (IllegalAccessException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (UnsupportedLookAndFeelException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		return;
	}
}

  @Override
   public void keyTyped(KeyEvent e) {
	  // TODO Auto-generated method stub
	
   }

    @Override
   public void keyReleased(KeyEvent e) {
	   // TODO Auto-generated method stub
	
    }
    
}


ClipTestClass.java

package JavaClassDesign;

import java.io.File;
import java.io.IOException;

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;

public class ClipTestClass {
	String file;
	Clip clip;
	public ClipTestClass(String file) {
		this.file=file;
	}
	public void player() throws UnsupportedAudioFileException, IOException, LineUnavailableException {
		File filePath = new File(file);
		if(filePath.exists()) {
			AudioInputStream audioInput = AudioSystem.getAudioInputStream(filePath);
			clip = AudioSystem.getClip();
			clip.open(audioInput);
			clip.start();
			clip.loop(Clip.LOOP_CONTINUOUSLY);
			//Thread.sleep(Integer.MAX_VALUE);
		}
	}
	public void stop() {
		if(clip!=null)
		clip.stop();
	}
}

DefineMapBySelf.java

package JavaClassDesign;

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.*;
import java.io.IOException;

import javax.swing.*;

public class DefineMapBySelf extends JFrame{
	//  定义下面的按键数组
	JButton[] jb;
	//  定义全局变量flag监听要填充的物体
	int flag=0;
	//  定义一个bool变量用来监听玩家的唯一性
	boolean player = true;
	public DefineMapBySelf() throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException {
		setLayout(null);
		setIconImage(new ImageIcon("src/Img/电脑.png").getImage());
		UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		setTitle("这里是地图编辑器");
		//  设置背景颜色是白色,JFrame必须先获得其面板,在通过调用setBackground()来设置其颜色
		setSize(600, 550);
		//  对jp面板注册鼠标监听
		addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent e) {
				int x,y;
				//  获取拖拽的坐标
				x = e.getX()/30;
				y = e.getY()/30;
				//  将对应map的地图更改为对应的标志
				if(map[y][x]==0){
				if(flag == 1){
					map[y][x]=flag;
					InitWall(x*30,y*30);
				}
				if(flag ==2 && player){
					map[y][x]=flag;
					InitPlayer(x*30,y*30);
					player=false;
				}
				if(flag ==8){
					map[y][x]=flag;
					InitDestination(x*30,y*30);
				}
				if(flag==4){
					map[y][x]=flag;
					InitBox(x*30,y*30,y,x);
				}
			    }
			}
		});
		setVisible(true);
		setKey();
		this.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				// TODO Auto-generated method stub
				// 弹出一个确认对话框来确定其操作
				int tmp = JOptionPane.showConfirmDialog(null, "你确定要退出游戏?","提示",JOptionPane.YES_NO_CANCEL_OPTION,JOptionPane.INFORMATION_MESSAGE);
				if(tmp == JOptionPane.YES_OPTION)
					System.exit(0);
			}
		});
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		setLocationRelativeTo(null);
	}
	//  设置地图,0代表可走,1代表墙体,2代表玩家,8代表目的地,4代表箱子
	int[][] map = new int[14][14];
	//  建立Jlabel来实现箱子数组的移动
	JLabel[][] box = new JLabel[14][14];
	//  设置下面按键
	private void setKey() {
		JLabel jl = new JLabel();
		jl.setBounds(20, 460, 550, 30);
		jb = new JButton[7];
		jb[0] = new JButton("墙块");
		jb[0].addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				int tmp = JOptionPane.showConfirmDialog(null, "你确定要增加"+jb[0].getText()+"吗?","提示",JOptionPane.YES_NO_CANCEL_OPTION,JOptionPane.INFORMATION_MESSAGE);
				if(tmp == JOptionPane.YES_OPTION){
					flag = 1;
					//System.out.println(flag);
				}
			}
		});
		jb[1] = new JButton("玩家");
        jb[1].addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				int tmp = JOptionPane.showConfirmDialog(null, "你确定要增加"+jb[1].getText()+"吗?","提示",JOptionPane.YES_NO_CANCEL_OPTION,JOptionPane.INFORMATION_MESSAGE);
				if(tmp == JOptionPane.YES_OPTION){
					flag = 2;
				}
			}
		});
		jb[2] = new JButton("箱子");
        jb[2].addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				int tmp = JOptionPane.showConfirmDialog(null, "你确定要增加"+jb[2].getText()+"吗?","提示",JOptionPane.YES_NO_CANCEL_OPTION,JOptionPane.INFORMATION_MESSAGE);
				if(tmp == JOptionPane.YES_OPTION){
					flag = 4;
				}
			}
		});
		jb[3] = new JButton("目的地");
        jb[3].addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				int tmp = JOptionPane.showConfirmDialog(null, "你确定要增加"+jb[3].getText()+"吗?","提示",JOptionPane.YES_NO_CANCEL_OPTION,JOptionPane.INFORMATION_MESSAGE);
				if(tmp == JOptionPane.YES_OPTION){
					flag = 8;
				}
			}
		});
		jb[4] = new JButton("清空");
        jb[4].addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				int tmp = JOptionPane.showConfirmDialog(null, "你确定要"+jb[4].getText()+"吗?","提示",JOptionPane.YES_NO_CANCEL_OPTION,JOptionPane.INFORMATION_MESSAGE);
				if(tmp == JOptionPane.YES_OPTION){
					closeThis();
					try {
						new DefineMapBySelf();
					} catch (ClassNotFoundException | InstantiationException | IllegalAccessException
							| UnsupportedLookAndFeelException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}
			}
		});
		jb[5] = new JButton("保存");
        jb[5].addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				int tmp = JOptionPane.showConfirmDialog(null, "你确定要开始游戏吗?","提示",JOptionPane.YES_NO_CANCEL_OPTION,JOptionPane.INFORMATION_MESSAGE);
				closeThis();
				try {
					new PlayGameInterface(map,box);
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (ClassNotFoundException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (InstantiationException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (IllegalAccessException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (UnsupportedLookAndFeelException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		jb[6] = new JButton("返回");
        jb[6].addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				int tmp = JOptionPane.showConfirmDialog(null, "你确定要"+jb[6].getText()+"吗?","提示",JOptionPane.YES_NO_CANCEL_OPTION,JOptionPane.INFORMATION_MESSAGE);
			}
		});
		jl.setLayout(new GridLayout(1,7));
		for(int i=0;i<jb.length;i++) {
			jl.add(jb[i]);
		}
		this.add(jl);
	}
	//  设置箱子初始化
	private void InitBox (int x,int y,int i,int j) {
		ImageIcon box1 = new ImageIcon("src/Img/box1.png");
		box[i][j] = new JLabel(box1);
		box[i][j].setBounds(x, y, 30, 30);
		this.getLayeredPane().add(box[i][j],new Integer(Integer.MIN_VALUE));
		//  取JPanel的内容面板,这里需要强制转换
		JPanel jp= (JPanel)this.getContentPane();
		//  将内容面板设置为透明
		jp.setOpaque(false);
	}
	//  设置目的地初始化
	private void InitDestination (int x,int y) {
		ImageIcon box1 = new ImageIcon("src/Img/destination.png");
		JLabel jl = new JLabel(box1);
		jl.setBounds(x, y, 30, 30);
		this.getLayeredPane().add(jl,new Integer(Integer.MIN_VALUE));
		//  取JPanel的内容面板,这里需要强制转换
		JPanel jp= (JPanel)this.getContentPane();
		//  将内容面板设置为透明
		jp.setOpaque(false);
	}
	//  设置玩家初始化
	JLabel jlplay;
	private void InitPlayer (int x,int y) {
		ImageIcon box1 = new ImageIcon("src/Img/player.png");
		jlplay = new JLabel(box1);
		jlplay.setBounds(x, y, 30, 30);
		this.getLayeredPane().add(jlplay,new Integer(Integer.MIN_VALUE));
		//  取JPanel的内容面板,这里需要强制转换
		JPanel jp= (JPanel)this.getContentPane();
		//  将内容面板设置为透明
		jp.setOpaque(false);
	}
	//  设置墙体初始化
	private void InitWall (int x,int y) {
		ImageIcon box1 = new ImageIcon("src/Img/wall.png");
		JLabel jl = new JLabel(box1);
		jl.setBounds(x, y, 30, 30);
		this.getLayeredPane().add(jl,new Integer(Integer.MIN_VALUE));
		//  取JPanel的内容面板,这里需要强制转换
		JPanel jp= (JPanel)this.getContentPane();
		//  将内容面板设置为透明
		jp.setOpaque(false);
		//System.out.println("InitWall");
	}
	private void closeThis() {
		this.dispose();
	}
}

MainJFrameWindow.java

package JavaClassDesign;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.ImageObserver;
import java.awt.image.ImageProducer;
import java.io.IOException;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class MainJFrameWindow extends JFrame{
	public MainJFrameWindow() throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException {
		setIconImage(new ImageIcon("src/Img/电脑.png").getImage());
		UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		//  初始化背景
		backgroundInit();
		//  设置开始游戏按钮
		setStartGameButton();
		//  设置编辑地图按钮
		setMapButton();
		//  设置主窗口属性
		setMainWindow();
		//  设置窗口关闭监听器
		this.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				int tmp = JOptionPane.showConfirmDialog(null, "你确认要离开游戏吗?", "提示", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE);
				if(tmp == JOptionPane.YES_OPTION)
					System.exit(0);
			}
		});
	}
	
	//  背景初始化
	private void backgroundInit() {
		//  使用JLabel做 背景
		//  创建一个图片对象
		ImageIcon backg = new ImageIcon("src/Img/background.png");
		JLabel jl = new JLabel(backg);
		jl.setBounds(0,0, backg.getIconWidth(), backg.getIconHeight());
		//  getLayeredPane()获取的是分层面板,将背景图片放置最底层
		this.getLayeredPane().add(jl,new Integer(Integer.MIN_VALUE));
        //  取JPanel的内容面板,这里需要强制转换
        JPanel jp= (JPanel)this.getContentPane();
        //  将内容面板设置为透明
        jp.setOpaque(false);
	}
	
	//  设置主窗体的属性
	private void setMainWindow() {
	    //  覆盖其默认布局,我们自己控制布局
		setLayout(null);    
		setTitle("推箱子");
		setVisible(true);
		setSize(600, 600);
		//  设置窗口居中  
		setLocationRelativeTo(null);
		//  设置当关闭消息确认框时,主窗口不会改变
		setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
	}
	
	//  设置开始游戏按钮
	private void setStartGameButton() {
		JButton jb = new JButton("开始游戏 ");
		jb.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//  关闭当前的主窗口
				closeThis();
				//  进入游戏的窗口
				try {
					new PlayGameInterface();
				} catch (IOException ioException) {
					ioException.printStackTrace();
				} catch (ClassNotFoundException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (InstantiationException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (IllegalAccessException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (UnsupportedLookAndFeelException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		jb.setBounds(250, 250, 90, 30);
		this.add(jb);
	}
	
	//  设置编辑地图按钮
	private void setMapButton() {
		JButton jb = new JButton("编辑地图 ");
		jb.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				closeThis();
				try {
					new DefineMapBySelf();
				} catch (ClassNotFoundException | InstantiationException | IllegalAccessException
						| UnsupportedLookAndFeelException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		jb.setBounds(250, 300, 90, 30);
		this.add(jb);
	}
	
	//  关闭当前界面的函数
	private void closeThis() {
		this.dispose();
	}
}

UserInter.java

package JavaClassDesign;

import javax.swing.UnsupportedLookAndFeelException;

public class UserInter {
	public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException {
		new MainJFrameWindow();
	}
}

想知道我是如解决编码过程中遇到的问题和困难,可以关注我哦。后面的博客会更新相关问题。

上一篇:gcc的-g,-o,-c,-D,-w,-W,-Wall,-O3等参数的意义


下一篇:在Linux命令行里与其他用户通信