GUI(图形用户接口),是指采用图形化的方式显示操作界面。
Java中包含两套完整体系:AWT包,Swing包
一.代码
package com.abc.ui;
import javax.swing.*;
import javax.swing.border.BevelBorder;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;
public class GameJFrame extends JFrame implements KeyListener, ActionListener {
//创建一个二维数组,用来管理数据
int[][]arrTow=new int[3][3];
//定义一个变量记录图片路径
String path="E:\\java学习\\day2\\src\\com\\abc\\ui\\ran\\";
//定义一个正确的二维数组
int[][]win={
{1,2,3},
{4,5,6},
{7,8,0}
};
//定义一个变量,存储步数
int step=0;
//条目
JMenu changePicture=new JMenu("更换图片");
JMenuItem grilPicture=new JMenuItem("美女");
JMenuItem animalPicture=new JMenuItem("动物");
JMenuItem morenPicture=new JMenuItem("默认");
JMenuItem rePlayGame=new JMenuItem("重新游戏");
JMenuItem reLogin=new JMenuItem("重新登录");
JMenuItem closeGame=new JMenuItem("关闭游戏");
JMenuItem checkMa=new JMenuItem("公众号");
int x=0;
int y=0;
public GameJFrame(){
//初始化界面
initJFrame();
//初始化菜单
initJMenuBar();
//初始化数据(打乱图片)
initData();
//初始化图片,就是将图片加载到界面中
initImage();
//显示界面,建议写在后面
this.setVisible(true);
}
//打乱数据,并将数据存到二维数组
private void initData() {
int[]arr={0,1,2,3,4,5,6,7,8};
Random r=new Random();
//打乱数字排列
for (int i = 0; i < arr.length; i++) {
int randomIndex=r.nextInt(9);
int temp=arr[i];
arr[i]=arr[randomIndex];
arr[randomIndex]=temp;
}
//将打乱后的一维数组数据,存储到二维数组中
int index=0;
for (int i = 0; i < arrTow.length; i++) {
for (int i1 = 0; i1 < arrTow.length; i1++) {
if (arr[index]==0){
x=i;
y=i1;
System.out.println(x+","+y);
}
arrTow[i][i1] = arr[index];
index++;
}
}
}
//初始化图片
private void initImage() {
//清除原始数据
this.getContentPane().removeAll();
//判断胜利
if (victory()){
this.getContentPane().removeAll();
JLabel winJlabel=new JLabel(new ImageIcon("E:\\java学习\\day2\\src\\com\\abc\\ui\\image\\4.jpg"));
winJlabel.setBounds(6,27,600,600);
this.getContentPane().add(winJlabel);
}
//载入图片
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
//创建一个JLabel的对象(管理容器),设置图片的长宽和坐标位置(左上角坐标)
JLabel jlabel=new JLabel(new ImageIcon(path+arrTow[i][j]+".jpg"));
jlabel.setBounds(150*j+80,150*i+100,150,150);
//把管理容器添加到界面中
// this.add(jlabel);
jlabel.setBorder(new BevelBorder(0));
this.getContentPane().add(jlabel);
}
}
//设置背景
JLabel jLabel=new JLabel(new ImageIcon("E:\\java学习\\day2\\src\\com\\abc\\ui\\ran\\1.jpg"));
jLabel.setBounds(6,27,600,600);
this.getContentPane().add(jLabel);
this.getContentPane().repaint();
//设置步数显示
JLabel stepJLabel=new JLabel("步数: "+step);
stepJLabel.setBounds(50,30,100,20);
this.getContentPane().add(stepJLabel);
}
//菜单目录
public void initJMenuBar() {
JMenuBar jmenueBar=new JMenuBar();
JMenu functionJMenu=new JMenu("功能");
JMenu aboutJMenu=new JMenu("关于我们");
functionJMenu.add(rePlayGame);
functionJMenu.add(reLogin);
functionJMenu.add(closeGame);
functionJMenu.add(changePicture);
aboutJMenu.add(checkMa);
//添加菜单
jmenueBar.add(functionJMenu);
jmenueBar.add(aboutJMenu);
//给整个界面设置菜单
this.setJMenuBar(jmenueBar);
//添加子菜单
changePicture.add(grilPicture);
changePicture.add(animalPicture);
changePicture.add(morenPicture);
//给条目绑定事件
rePlayGame.addActionListener(this);
reLogin.addActionListener(this);
closeGame.addActionListener(this);
checkMa.addActionListener(this);
grilPicture.addActionListener(this);
animalPicture.addActionListener(this);
morenPicture.addActionListener(this);
}
//框架
public void initJFrame() {
this.setSize(603,680);
//设置界面标题
this.setTitle("拼图 单机版v1.0");
//设置界面置顶
this.setAlwaysOnTop(true);
//设置界面居中
this.setLocationRelativeTo(null);
//设置关闭模式
this.setDefaultCloseOperation(3);
//取消隐藏容器的默认居中设置,图片才可以按照xy轴的方式排列
this.setLayout(null);
//添加键盘监听
this.addKeyListener(this);
}
//判断胜利
public boolean victory(){
for (int i = 0; i < arrTow.length; i++) {
for (int j = 0; j < arrTow.length; j++) {
if (arrTow[i][j]!=win[i][j]){
return false;
}
}
}
return true;
}
//键盘监听
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
int code=e.getKeyCode();
//A的ascii码为65
if (code==65){
//删除界面中的所有图片
this.getContentPane().removeAll();
JLabel jbAll=new JLabel(new ImageIcon(path+"2.jpg"));
jbAll.setBounds(40,40,500,500);
this.getContentPane().add(jbAll);
//背景
JLabel jLabel=new JLabel(new ImageIcon(path+"1.jpg"));
jLabel.setBounds(40,40,500,500);
this.getContentPane().add(jLabel);
this.getContentPane().repaint();
}
}
@Override
public void keyReleased(KeyEvent e) {
//如果胜利则不运行以下代码
if (victory()){
//直接结束方法
return;
}
//对上下左右做判断并移动
//左37,上38,右39,下40
int code=e.getKeyCode();
if (code==37){
//左
System.out.println("左");
//解决数组范围的bug
if (y==2){
return;
}else {
arrTow[x][y]=arrTow[x][y+1];
arrTow[x][y+1]=0;
//0的x坐标要变
y++;
step++;
//调用方法,以最新排列方式加载
initImage();
}
}
else if (code==38){
//上
System.out.println("上");
if (x==2){
return;
}
else{
arrTow[x][y]=arrTow[x+1][y];
arrTow[x+1][y]=0;
//0的x坐标要变
x++;
step++;
//调用方法,以最新排列方式加载
initImage();
}
}
else if (code==39){
//右
System.out.println("右");
if (y==0){
return;
}
else {
arrTow[x][y]=arrTow[x][y-1];
arrTow[x][y-1]=0;
//0的x坐标要变
y--;
step++;
//调用方法,以最新排列方式加载
initImage();
}
}
else if (code==40) {
//下
System.out.println("下");
if (x == 0) {
return;
}
else{
arrTow[x][y] = arrTow[x - 1][y];
arrTow[x - 1][y] = 0;
//0的x坐标要变
x--;
step++;
//调用方法,以最新排列方式加载
initImage();
}
}
else if (code==65){
initImage();
}
else if (code==87){
arrTow=new int[][]{
{1,2,3},
{4,5,6},
{7,8,0}
};
initImage();
}
}
//动作监听
@Override
public void actionPerformed(ActionEvent e) {
Object obj=e.getSource();
if (obj==rePlayGame){
System.out.println("重新游戏");
//清除步数
step=0;
//再打乱数组数据
initData();
//重新加载图片
initImage();
}else if (obj==reLogin){
System.out.println("重新登陆");
//关闭当前游戏界面
this.setVisible(false);
//打开登录界面
new LoginJFrame();
}else if (obj==closeGame){
System.out.println("关闭游戏");
//直接关闭虚拟机
System.exit(0);
}else if (obj==checkMa){
System.out.println("公众号");
//创建一个弹框对象
JDialog jDialog=new JDialog();
//创建一个管理图片的容器对象
JLabel jlable=new JLabel(new ImageIcon("E:\\java学习\\day2\\src\\com\\abc\\ui\\image\\5.jpg"));
jlable.setBounds(0,0,258,258);
jDialog.getContentPane().add(jlable);
//设置弹框大小
jDialog.setSize(344,344);
//弹框置顶
jDialog.setAlwaysOnTop(true);
//弹框居中
jDialog.setLocationRelativeTo(null);
//弹框不关闭,无法进行其他操作
jDialog.setModal(true);
//显示弹框
jDialog.setVisible(true);
}else if (obj==grilPicture){
step=0;
path="E:\\java学习\\day2\\src\\com\\abc\\ui\\gril\\";
initData();
initImage();
}
else if (obj==animalPicture){
step=0;
path="E:\\java学习\\day2\\src\\com\\abc\\ui\\animal\\";
initData();
initImage();
}
else if (obj==morenPicture){
step=0;
path="E:\\java学习\\day2\\src\\com\\abc\\ui\\ran\\";
initData();
initImage();
}
}
}
二.过程分析
1.创建主界面
.练习:创建主界面1 (使用JFrame)
到idea中创建一个宽603像素,高680像素的游戏主界面
到idea中创建一个宽488像素,高480像素的登陆界面
到idea中创建一个宽488像素,高500像素的注册界面
2.菜单制作
3.添加图片
将图片放到JLable中 ,JLable可以管理图片和文字。
创建一个ImageIcon对象,并指定图片在电脑中的位置。
图片,文字,进度条都称为组件。
4.打乱顺序
将一维数组打乱再添加进二维数组中
5.事件
事件是可以被组件识别的操作。
当你对组件干了某件事情之后,就会执行相应的代码。
动作监听:只有点击和空格
鼠标监听:划入动作,按下动作,松开动作,划出动作
键盘监听:按下不放,释放,键入键
动作监听
简化代码,使用了,匿名内部类(JButton按钮)
this的意思是,执行本类里对应的代码
鼠标监听
键盘监听
6.美化界面
添加游戏背景
先加载的图片在上方,背景图片要在最后添加
7.移动图片
8.查看完整图片、作弊码、判断胜利
9.计步和菜单业务实现
10.练习 一
11.练习二