java swing篇之推箱子小游戏实现及见解
swing是java里的一个包,是程序员在图像方面编程的接口,包含了GUI(graphical user interface)图形用户界面组件(文本框,按钮,表)等等
说到swing不得不提一下AWT(abstruct window toolkit)抽象窗口工具包,他是早期的GUI编程包,
AWT的图像函数于操作系统提供的图形函数有着一一对应的关系,所以AWT控件被称为 重量级控件,(现在程序代码中的事件,及事件监听依然会用到AWT),但更多的还是swing,因为swing是为了解决AWT存在的问题而新开发的图形界面包,是对AWT的改良和扩展
1:awt是基于北地方法的c/c++程序 速度相对快
2:swing是基于awt的java程序 速度慢
(由以上可推断swing更能符合java一次编译,到处运行的特点)
3:导swing包,javax.swing.* . 导awt包,java.awt.*
这么说只是想说明我理解的JLabel是非常重要的,因为对于GUI程序来讲,最重要的就是图像再用户界面的呈现了,JLabel小到可以作为一颗棋子呈现,大到也可以作为程序背景图来呈现,就初学者来讲称他为最基本元素也不为过(我也是初学者,望大佬们勿喷,多鼓励,哈哈哈哈)。以下几个以JLabel呈现图像的步骤。
1:Icon i = new ImageIcon("*.jpg");
2:JLabel t = new JLabel(i);
3:t.setBounds(x,y,width,height);
这里讲一下setBounds(),JFrame默认是边界布局(borderLayout),JPanel的默认布局是流式布局,流失布局的特点就是:一行占满才到下一行,所以要用setBounds(),先设置布局管理器,容器.setLayout(null),然后用setBounds()自定义控件的位置,四个参数,分别是x,y,width,height,宽度高度好理解,x,y,就理解为控件与容器左上角的距离,有的小程序(包括我下面的推箱子)是通过数组来定义布局的,数组的两个参数分别是行,列,对应的是y,x,(望自行理解一番),所以代码中需要把x,y,带进数组的y在x前面,datas[y][x]
再加载布局的时候总有个先后顺序吧,那你觉想背景这样的布局应该多久加载呢?
正确的做法应该是这样的,先加载小布局,小JLabel这样的,再加载进大布局,像背景JLabel这样的,(原因是避免大布局把小布局覆盖点),最后在设置顶层容器,jFrame.setVisible(true);设置顶层容器可见(默认是不可见的),就像是加载最大的布局一样,这样方便记。
public class App extends JFrame {
public static void main(String[] args) {
int[][] datas = {
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 4, 0, 0, 1, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 4, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 8, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1},
{1, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
};
new Member(datas);
}
构造函数传参方便后边多个关卡的编写
package cc.cque.cjf;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class Member extends JFrame implements KeyListener, ActionListener {
/**
* 0:空地
* 1:树,障碍
* 3:笼子
* 4:羊
*
*/
int[][] datas;
JLabel [][] sheep = new JLabel[12][16];
int wx,wy;
int num=0; //判断羊的数量
final static int TOTAL = 3;
JLabel wolfLable;
JMenuBar jMenuBar;
JMenu choose,about;
JMenuItem restar,author;
public Member(int[][] datas){
this.datas = datas;
BarInit();
treeInit();
wolfInit();
sheepInit();
cageInit();
backgroundInit();
setFrom();
this.addKeyListener(this);
}
public void setFrom(){
this.setTitle("推箱子");
this.setSize(850,660);
this.setResizable(false); //禁止用户改变窗口大小
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null); //设置在屏幕中间显示
this.setVisible(true);
}
public void BarInit(){
jMenuBar = new JMenuBar();
choose = new JMenu("选择");
about = new JMenu("关于作者");
restar = new JMenuItem("重来");
author = new JMenuItem("关于作者");
restar.addActionListener(this);
author.addActionListener(this);
choose.add(restar);
about.add(author);
jMenuBar.add(choose);
jMenuBar.add(about);
this.add(jMenuBar);
this.setJMenuBar(jMenuBar);
}
public void treeInit(){
JLabel t;
Icon icon = new ImageIcon("pic/tree.jpg");
for(int i =0 ;i
下面是各个关卡地图数据的类,地图由二维数组构造
package cc.cque.cjf;
public class DataOptins {
public DataOptins() {
}
public int[][] data0(){
int[][] no0 = {
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 4, 0, 0, 1, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 4, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 8, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1},
{1, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
};
return no0;
}
public int[][] data1(){
int[][] no1 = {
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,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