敲代码的第十一天:推箱子练习(设置窗口,添加围墙及图片)(java)

  • 时间:
  • 浏览:
  • 来源:互联网
package wangwang2;
import javax.swing.*;
import java.awt.*;
import java.awt.font.ImageGraphicAttribute;

public class TXZ2 {
    public static void main(String[] args){
        JFrame gameFrame = new JFrame();
        gameFrame.setTitle("推箱子");
        gameFrame.setSize(22*48+10,12*48+38);
        gameFrame.setBounds((1920-800)/2,(1080-600)/2,800,600);
        gameFrame.setLocation(null);
        gameFrame.setResizable(false);
        gameFrame.setDefaultCloseOperation(3);
        JPanel panel= new JPanel();
        panel.setBackground(Color.GRAY);
        panel.setLayout(null);
        gameFrame.setContentPane(panel);
        ImageIcon boxImg = new ImageIcon("");
        JLabel box = new JLabel(boxImg);
        panel.add(box);
        box.setBounds(3*48,3*48,48,48);
        ImageIcon workerImg = new ImageIcon("");
        JLabel worker = new JLabel(workerImg);
        worker.setBounds(17*48,6*48,48,48);
        ImageIcon goalImg = new ImageIcon("");
        JLabel goal = new JLabel(goalImg);
        panel.add(goal);
        goal.setBounds(8*48,9*48,48,48);
        ImageIcon wallImg  = new ImageIcon("");
        JLabel[]walls = new JLabel[22*2+(12-2)*2];
        for(int i=0,i<walls.length,i++){
            walls[i]=new JLabel(wallImg);
        }
        int index = 0;
        for(int i=0;i<22;i++) {
            panel.add(walls[index]);
            walls[index++].setBounds(i * 48, 0, 48, 48);
            panel.add(walls[index]);
            walls[index++].setBounds(i * 48, 11 * 48, 48, 48);
        }
            for(int i=1;i<=10;i++) {
            panel.add(walls[index]);
            walls[index++].setBounds(0,i*48,48,48);
            panel.add(walls[index]);
            walls[index++].setBounds(21*48,i*48,48,48);
            }             
        }
    }

本文链接http://metronic.net.cn/metronic/show-64462.html