| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2775 人关注过本帖
标题:推箱子问题/新手
只看楼主 加入收藏
fp2952
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2016-8-11
结帖率:0
收藏
已结贴  问题点数:20 回复次数:4 
推箱子问题/新手
推箱子,大神们求助,自己跟着别人的代码写的。运行出现开始界面和第一关的界面,别人正常的的game(1)是在startgame()里运行的,我的感觉初始化创建对象就出来了。
图片附件: 游客没有浏览图片的权限,请 登录注册
程序如下

package com.fp;

import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class GameFram extends JFrame {
    int level=1;
    private Game game=new Game(level);
    JPanel jp;
    public GameFram(){
        this.setTitle("推箱子");                                //标题
        this.setBounds(200,100,800,600);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
        jp=new JPanel(){
            public void paintComponent(Graphics g){
                super.paintComponent(g);
                ImageIcon img=new ImageIcon(GameFram.class.getClassLoader().getResource("imgs/backgroundImg.png"));
                g.drawImage(img.getImage(), 0, 0, null);
            }
        };
        this.add(jp);
        jp.setLayout(null);
        JButton jb=new JButton("开始游戏");                    //开始按键
        jp.add(jb);
        jb.setBounds(630,400,100,30);
        jb.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                gamestart();
                requestFocus();
            }
        });
        JButton jb1=new JButton("操作说明");                    //操作按键
        jp.add(jb1);
        jb1.setBounds(630,450,100,30);
        jb1.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                JOptionPane.showMessageDialog(null,"上下左右控制方向"+"\r\n空格键退后一步","操作说明", JOptionPane.INFORMATION_MESSAGE);
                requestFocus();
            }
        });
        JButton jb3=new JButton("退出游戏");                    //退出
        jp.add(jb3);
        jb3.setBounds(630,500,100,30);
        jb3.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                System.exit(0);
                requestFocus();
            }
        });
        this.requestFocus();
        this.addKeyListener(new MyKeyListener());
    }
   
        
        protected void gamestart() {                                //开始
        // TODO Auto-generated method stub
        repaint();
        this.remove(jp);
        this.setLayout(null);
        this.add(game);
        JPanel jp1=new JPanel(){
            public void paintComponent(Graphics g){
                super.paintComponent(g);
                ImageIcon img=new ImageIcon(GameFram.class.getClassLoader().getResource("imgs/toolImg.png"));
                g.drawImage(img.getImage(), 0, 0, null);
            }
        };
        this.add(jp1);
        jp1.setBounds(600, 0, 200, 600);
        jp1.setLayout(null);
        JButton jb1=new JButton("后退一步");
        jp1.add(jb1);
        jb1.setBounds(50,250,100,30);
        jb1.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                /////////////
                requestFocus();
            }
        });
        JButton jb2=new JButton("上一关");
        jp1.add(jb2);
        jb2.setBounds(50,300,100,30);
        jb2.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                /////////////
                requestFocus();
            }
        });
        JButton jb3=new JButton("选关");
        jp1.add(jb3);
        jb3.setBounds(50,350,100,30);
        jb3.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                /////////////
                requestFocus();
            }
        });
        JButton jb4=new JButton("下一关");
        jp1.add(jb4);
        jb4.setBounds(50,400,100,30);
        jb4.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                /////////////
                requestFocus();
            }
        });
        JButton jb5=new JButton("重新开始");
        jp1.add(jb5);
        jb5.setBounds(50,450,100,30);
        jb5.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                /////////////
                requestFocus();
            }
        });
        JButton jb6=new JButton("退出游戏");
        jp1.add(jb6);
        jb6.setBounds(50,500,100,30);
        jb6.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                System.exit(0);
                setVisible(false);
                requestFocus();
            }
        });
        
        
    }
        public static void main(String[] args) {

            new GameFram();
        }
   }


//////////////////////////////////////////////////
package com.fp;

import javax.swing.JFrame;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;

import com.


public class Game extends JFrame {
    private LoadMap lm;
    private int level;
    private int mapx,mapy;
    private int [][]map;
    private static Image []imgs=null;
    private static Toolkit tk=Toolkit.getDefaultToolkit();
    static {
        imgs=new Image[]{
            tk.getImage(GameFram.class.getClassLoader().getResource("imgs/0.gif")),
            tk.getImage(GameFram.class.getClassLoader().getResource("imgs/1.gif")),
            tk.getImage(GameFram.class.getClassLoader().getResource("imgs/2.GIF")),
            tk.getImage(GameFram.class.getClassLoader().getResource("imgs/3.GIF")),
            tk.getImage(GameFram.class.getClassLoader().getResource("imgs/4.gif")),
            tk.getImage(GameFram.class.getClassLoader().getResource("imgs/5.GIF")),
            tk.getImage(GameFram.class.getClassLoader().getResource("imgs/6.GIF")),
            tk.getImage(GameFram.class.getClassLoader().getResource("imgs/7.GIF")),
            tk.getImage(GameFram.class.getClassLoader().getResource("imgs/8.GIF")),
            tk.getImage(GameFram.class.getClassLoader().getResource("imgs/9.GIF")),
        };
    }
  public Game(int level){
      this.setBounds(0,0,600,600);   
        this.setVisible(true);
        lm = new LoadMap(level);
        map = lm.getmap();
        this.mapx = lm.getmapx();
        this.mapy = lm.getmapy();
        this.level = level;
  }
  public void paint(Graphics g) {
      for(int i=0;i<20;i++){
          for(int j=0;j<20;j++){
              g.drawImage(imgs[map[i][j]], 30*i, 30*j, this);
          }
      }
    g.setColor(Color.BLUE);
    g.setFont(new Font("仿宋", Font.BOLD, 18));
    g.drawString("第"+level+"关", 50, 50);
   
}

   

    public void back() {
        // TODO Auto-generated method stub
        
    }

    public void goback() {
        // TODO Auto-generated method stub
        
    }

    public void gonext() {
        // TODO Auto-generated method stub
        
    }

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

}
搜索更多相关主题的帖子: package import 推箱子 
2016-08-11 22:41
砖家的谎言
Rank: 12Rank: 12Rank: 12
等 级:禁止访问
威 望:30
帖 子:693
专家分:3898
注 册:2013-12-6
收藏
得分:10 
你的问题是啥呢

我不是砖家,要努力成为砖家。
2016-08-12 09:34
fp2952
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2016-8-11
收藏
得分:0 
别人写的不会弹出后面"第一关"窗口,程序几乎一样,不晓得怎么回事。
2016-08-12 19:22
fp2952
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2016-8-11
收藏
得分:0 
PushBox.rar (748.38 KB)
2016-08-12 19:25
la6692003
Rank: 2
等 级:论坛游民
帖 子:3
专家分:10
注 册:2016-3-15
收藏
得分:10 
撸主的背景跟我之前看的一本书的实例很像,你是说你这个打开时会产生两个窗口?那你看一下你后面那个窗口是不是默认显示的,或者说是有两个窗口实例
2016-08-16 18:39
快速回复:推箱子问题/新手
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.086686 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved