打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Java学习—124.2048代码

游戏的主界面的代码在类game_2048中。

本类主要包括两个方面:

一是布局。

二是动作,按钮的动作。

其中,动作又有菜单动作,按钮动作。而按钮动作又有多方面,在下一篇的代码中会有注释说明。

本篇帖的是布局。其初始化界面如下:

其布局代码如下:

import java.awt.*;

import java.awt.event.*;

import java.util.*;

import javax.swing.*;

public class game_2048 extends JFrame implements KeyListener,Runnable{

/**

*/

private static final long serialVersionUID = 1L;

private JButton[][] shuzi=new JButton[4][4];//游戏按钮

private JMenuBar menubar;//菜单栏

private JMenuItem item[];//菜单项

private JLabel label,labelcount,result;//计步,步数,结果标签

private int count=0,x,y,num,number;//步数,坐标,数字

private JButton start,cancel,up,down,left,right;//重新开始,退出游戏,上,下,左,右按钮

private String str,st;//取游戏按钮上的值

private boolean iswin;//判断是否胜利

Random ran = new Random();//随机值

public game_2048(){

super("2048小游戏—by张熹熹");

this.setSize(400, 600);

this.setLocationRelativeTo(this);

this.setResizable(false);//窗口最大化按钮

this.setVisible(true);

this.getContentPane().setForeground(SystemColor.inactiveCaption);

this.getContentPane().setBackground(Color.pink);

this.setLayout(null);

this.addmenu();//添加菜单

this.addanniu();//添加主按钮

this.addfunc();// 添加计分栏

this.addbu();//添加功能按钮

this.addresult();

}

public void addmenu(){

menuAct m=new menuAct();

menubar=new JMenuBar();

this.setJMenuBar(menubar);

String items[]={"开始游戏","游戏规则","关于我"};

item=new JMenuItem[items.length];

for(int i=0;i<items.length;i++){

item[i]=new JMenuItem(items[i]);

item[i].addActionListener(m);

menubar.add(item[i]);

}

}

public void addanniu(){

JPanel anniu=new JPanel();

anniu.setLayout(new GridLayout(4,4));

anniu.setLocation(40, 80);

anniu.setBorder(BorderFactory.createMatteBorder(2, 2, 2, 2, new Color(252, 250, 242)));

anniu.setSize(320, 320);

anniu.setBackground(SystemColor.textText);

for (int i = 0; i < 4; i++) { 

for (int j = 0; j < 4; j++) {

shuzi[i][j] = new JButton();

shuzi[i][j].setText(" ");

anniu.add(shuzi[i][j]); 

}

}

this.getContentPane().add(anniu);

}

public void addfunc(){

JPanel func=new JPanel();

func.setLocation(10, 10);

func.setSize(380, 50);

func.setBackground(SystemColor.activeCaptionBorder);

label=new JLabel("计步");

label.setHorizontalAlignment(SwingConstants.RIGHT);

label.setFont(new Font("仿宋", Font.BOLD, 17));

label.setBounds(10, 0, 59, 50);

func.add(label);

labelcount = new JLabel(String.valueOf(count));

labelcount.setFont(new Font("宋体", Font.PLAIN, 27));

labelcount.setBounds(79, -1, 114, 50);

func.add(labelcount);

this.getContentPane().add(func);

}

public void addbu(){

btAction act=new btAction();

start=new JButton("重新开始");

start.setBounds(40, 433, 93, 40);

cancel=new JButton("退出游戏");

cancel.setBounds(40, 483, 93, 40);

up=new JButton("up");

down=new JButton("down");

left=new JButton("left");

right=new JButton("right");

up.setBounds(249, 433, 60, 40);

down.setBounds(249, 483, 60, 40);

left.setBounds(178, 483, 60, 40);

right.setBounds(320, 483, 60, 40);

start.addActionListener(act);

cancel.addActionListener(act);

up.addActionListener(act);

down.addActionListener(act);

left.addActionListener(act);

right.addActionListener(act);

this.addKeyListener(this);

this.getContentPane().add(start);

this.getContentPane().add(cancel);

this.getContentPane().add(up);

this.getContentPane().add(down);

this.getContentPane().add(left);

this.getContentPane().add(right);

}

public void addresult(){

result=new JLabel("游戏结束,如需再玩,请点击重新开始!");

result.setForeground(Color.red);

result.setBounds(40, 393, 280, 40);

result.setBackground(Color.black);

result.setVisible(false);

this.getContentPane().add(result);

}

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
作业
【第84题】JAVA高级技术-网络编程3(网络资源的单线程下载)
Java添加事件监听的四种方法代码实例
Java事件处理机制
编程语言java常见事件响应方法实例汇总
水果蔬菜连连看游戏
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服