前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >java五子棋小游戏含免费源码

java五子棋小游戏含免费源码

作者头像
海拥
发布2021-08-23 17:48:50
1.5K0
发布2021-08-23 17:48:50
举报
文章被收录于专栏:全栈技术全栈技术

游戏截图:

在这里插入图片描述
在这里插入图片描述

看一下运行效果

在这里插入图片描述
在这里插入图片描述

这里我使用的开发工具是Eclipse

主要代码Main.java:

代码语言:javascript
复制
public class Main extends JFrame{
	/*
	 * 用户登录
	 */
	private static final long servialVersionUID = 1L;
	
	final JLabel logoLabel = new JLabel("开心五子棋");
	final JLabel logo = new JLabel();
	final JButton loginButton = new JButton("登陆");
	final JLabel registerLabel = new JLabel("立即注册");
	final JLabel userLabel = new JLabel("账号:");
	final JLabel passwordLabel = new JLabel("密码:");
	final static JTextField userjt = new JTextField(11);
	final JPasswordField passwordjt = new JPasswordField(11);
	final JCheckBox rememberPasswordjcb = new JCheckBox();
	final JLabel rememberPasswordjl = new JLabel("记住密码");
	final JCheckBox automaticLoginjcb = new JCheckBox();
	final JLabel automaticLoginjl = new JLabel("自动登录");
	final JLabel promptPasswordFalse = new JLabel("密码错误!");
	final JLabel promptRegister = new JLabel("该账号还未注册!");
	final JLabel promptUserNameEmpty = new JLabel("请输入账号!");
	final JLabel prompPasswordEmpty = new JLabel("请输入密码!");
	final Color color = new Color(255, 218, 185);
	final FileOperation read = new FileOperation();//创建文件对象
	final FileOperation f = new FileOperation();
	public Main() {
		setTitle("开心五子棋");
		setBounds(200, 200, 500, 500);
		setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		setVisible(true);
		
		//基本布局设置
		SpringLayout springLayout = new SpringLayout();//使用弹簧布局管理器
		Container c = getContentPane();//创建容器
		c.setBackground(new Color(255, 218, 185));
		c.setLayout(springLayout);
		
		userjt.setFont(new Font("微软雅黑", 0, 18 ));
		userjt.setText(Register.userName);
		passwordjt.setFont(new Font("微软雅黑", 0, 18));
		passwordjt.setText(Register.password);
		logoLabel.setFont(new Font("微软雅黑", 1, 48));
		logoLabel.setForeground(Color.pink);
		ImageIcon logoimage = new ImageIcon(Main.class.getResource("/image/logo5.jpg"));
		logoimage.setImage(logoimage.getImage().getScaledInstance(260, 130, Image.SCALE_DEFAULT));
		logo.setIcon(logoimage);
		userLabel.setFont(new Font("微软雅黑", 1, 20));
		passwordLabel.setFont(new Font("微软雅黑", 1, 20));
		rememberPasswordjl.setFont(new Font("微软雅黑", 0, 14));
		rememberPasswordjl.setForeground(Color.gray);
		automaticLoginjl.setFont(new Font("微软雅黑", 0, 14));
		automaticLoginjl.setForeground(Color.gray);
		loginButton.setFont(new Font("微软雅黑", 1, 16));
		registerLabel.setFont(new Font("微软雅黑", 1, 13));
		registerLabel.setForeground(Color.gray);
		promptPasswordFalse.setFont(new Font("微软雅黑", 0, 13));
		promptPasswordFalse.setForeground(Color.red);
		promptUserNameEmpty.setFont(new Font("微软雅黑", 0, 13));
		promptUserNameEmpty.setForeground(Color.red);
		prompPasswordEmpty.setFont(new Font("微软雅黑", 0, 13));
		prompPasswordEmpty.setForeground(Color.red);
		promptRegister.setFont(new Font("微软雅黑", 0, 13));
	    promptRegister.setForeground(Color.red);
	    rememberPasswordjcb.setBackground(new Color(255, 218, 185));
	    automaticLoginjcb.setBackground(new Color(255, 218, 185));
	    
	    c.add(logo);//首页图标
	    springLayout.putConstraint(springLayout.NORTH, logo, 40, springLayout.NORTH, c);
	    springLayout.putConstraint(springLayout.WEST, logo, 115, springLayout.WEST, c);
		c.add(logoLabel);//标题“开心五子棋”
		springLayout.putConstraint(springLayout.NORTH, logoLabel, 100, springLayout.NORTH, c);
		springLayout.putConstraint(springLayout.WEST, logoLabel, 120, springLayout.WEST, c);
		logoLabel.setVisible(false);
		
		c.add(userLabel);//用户名
		springLayout.putConstraint(springLayout.NORTH, userLabel, 35, springLayout.SOUTH, logoLabel);
		springLayout.putConstraint(springLayout.WEST, userLabel, 110, springLayout.WEST, c);
		c.add(userjt);
		springLayout.putConstraint(springLayout.NORTH, userjt, 35, springLayout.SOUTH, logoLabel);
		springLayout.putConstraint(springLayout.WEST, userjt, 10, springLayout.EAST, userLabel);
		
		c.add(passwordLabel);//密码
		springLayout.putConstraint(springLayout.NORTH, passwordLabel, 10, springLayout.SOUTH, userLabel);
		springLayout.putConstraint(springLayout.WEST, passwordLabel, 110, springLayout.WEST, c);
		c.add(passwordjt);
		springLayout.putConstraint(springLayout.NORTH, passwordjt, 10, springLayout.SOUTH, userjt);
		springLayout.putConstraint(springLayout.WEST, passwordjt, 10, springLayout.EAST, passwordLabel);
		
		c.add(rememberPasswordjcb);//复选框
		springLayout.putConstraint(springLayout.NORTH, rememberPasswordjcb, 10, springLayout.SOUTH, passwordLabel);
		springLayout.putConstraint(springLayout.WEST, rememberPasswordjcb, 175, springLayout.WEST, c);
		c.add(rememberPasswordjl);
		springLayout.putConstraint(springLayout.NORTH, rememberPasswordjl, 10, springLayout.SOUTH, passwordjt);
		springLayout.putConstraint(springLayout.WEST, rememberPasswordjl, 5, springLayout.EAST, rememberPasswordjcb);
		c.add(automaticLoginjcb);
		springLayout.putConstraint(springLayout.NORTH, automaticLoginjcb, 10, springLayout.SOUTH, passwordjt);
		springLayout.putConstraint(springLayout.WEST, automaticLoginjcb, 30, springLayout.EAST, rememberPasswordjl);
		c.add(automaticLoginjl);
		springLayout.putConstraint(springLayout.NORTH, automaticLoginjl, 10, springLayout.SOUTH, passwordjt);
		springLayout.putConstraint(springLayout.WEST, automaticLoginjl, 5, springLayout.EAST, automaticLoginjcb);
		
		c.add(loginButton);//登陆按钮
		springLayout.putConstraint(springLayout.NORTH, loginButton, 20, springLayout.SOUTH, rememberPasswordjl);
		springLayout.putConstraint(springLayout.WEST, loginButton, 110, springLayout.WEST, c);
		c.add(registerLabel);//注册按钮
		springLayout.putConstraint(springLayout.NORTH, registerLabel, 5, springLayout.SOUTH, loginButton);
		springLayout.putConstraint(springLayout.WEST, registerLabel, 320, springLayout.WEST, c);
		
		c.add(promptRegister);//账号未注册提示
		promptRegister.setVisible(false);
	    springLayout.putConstraint(springLayout.NORTH, promptRegister, 41, springLayout.SOUTH, logoLabel);
		springLayout.putConstraint(springLayout.WEST, promptRegister, 5, springLayout.EAST, userjt);
		c.add(promptUserNameEmpty);//请输入账号
		promptUserNameEmpty.setVisible(false);
	    springLayout.putConstraint(springLayout.NORTH, promptUserNameEmpty, 41, springLayout.SOUTH, logoLabel);
		springLayout.putConstraint(springLayout.WEST, promptUserNameEmpty, 5, springLayout.EAST, userjt);
		
		c.add(promptPasswordFalse);//密码错误提示
		promptPasswordFalse.setVisible(false);
		springLayout.putConstraint(springLayout.NORTH, promptPasswordFalse, 20, springLayout.SOUTH, promptRegister);
		springLayout.putConstraint(springLayout.WEST, promptPasswordFalse, 5, springLayout.EAST, passwordjt);
		c.add(prompPasswordEmpty);//密码为空提示
		prompPasswordEmpty.setVisible(false);
		springLayout.putConstraint(springLayout.NORTH, prompPasswordEmpty, 20, springLayout.SOUTH, promptRegister);
		springLayout.putConstraint(springLayout.WEST, prompPasswordEmpty, 5, springLayout.EAST, passwordjt);
		
		//设置文本框鼠标点击事件
		userjt.addMouseListener(new MouseAdapter() {//文本框
			public void mouseClicked(MouseEvent e) {
				userjt.setText("");
			}
		});
		passwordjt.addMouseListener(new MouseAdapter() {//密码框
			public void mouseClicked(MouseEvent e) {
				passwordjt.setText("");
			}
		});
		
		//设置登陆按钮单击事件
		loginButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String userName = userjt.getText().trim();//获取用户输入的账号和密码
				String Password = new String(passwordjt.getPassword()).trim();
				//判断账号和密码
			    if(userName.length() != 0) {//用户名不为空
			    	promptUserNameEmpty.setVisible(false);//关闭账号为空显示
			    	if(Password.length() != 0) {//密码不为空
			    		if(f.readData("user.xls", userName) && Password.equals(f.backData("user.xls", userName, "password"))) {//用户输入的账号和密码正确
							promptRegister.setVisible(false);//隐藏提示信息
							promptPasswordFalse.setVisible(false);
							prompPasswordEmpty.setVisible(false);
							loginButton.setText("                登 陆 中...               ");
							new Chessboard();//跳转到五子棋棋盘页面
							dispose();//销毁当前页面
						}
			    		else if( f.readData("user.xls", userName) && !Password.equals(f.backData("user.xls", userName, "password"))) {//用户输入密码错误
							promptPasswordFalse.setVisible(true);//显示密码错误提示
							promptRegister.setVisible(false);
							prompPasswordEmpty.setVisible(false);
							passwordjt.setText("");//密码框清空
							passwordjt.requestFocus();//光标定位到密码框
						}else {//账号还未注册
							promptRegister.setVisible(true);
					    	promptPasswordFalse.setVisible(false);
							prompPasswordEmpty.setVisible(false);
						}
			        }
			        else {//密码为空
			        	if(userName.equals("admin")) {//用户名已经注册, 提示输入密码
			        		prompPasswordEmpty.setVisible(true);
				        	promptUserNameEmpty.setVisible(false);
				        	promptRegister.setVisible(false);
					    	promptPasswordFalse.setVisible(false);
			        	}else {//用户名未注册
			        		prompPasswordEmpty.setVisible(false);
				        	promptUserNameEmpty.setVisible(false);
				        	promptRegister.setVisible(true);
					    	promptPasswordFalse.setVisible(false);
			        	}
			        	
			        }
			    }else {//用户名为空
			    	promptUserNameEmpty.setVisible(true);//提示输入账号
			    	promptRegister.setVisible(false);
			    	promptPasswordFalse.setVisible(false);
			    	prompPasswordEmpty.setVisible(false);
			    	passwordjt.setText("");//将密码框置为空
			    	if(Password.length() == 0) {//密码为空
			    		prompPasswordEmpty.setVisible(true);
			    		promptRegister.setVisible(false);
				    	promptPasswordFalse.setVisible(false);
			    	}
			    }
			}
		});
		
		//注册标签监听器
		registerLabel.addMouseListener(new MouseListener() {
			public void mouseClicked(MouseEvent e) {
                dispose();
				new Register();
			}
			public void mouseEntered(MouseEvent e) {
				registerLabel.setForeground(Color.red);;
			}
			public void mouseExited(MouseEvent e) {
			    registerLabel.setForeground(Color.black);
			}
			public void mousePressed(MouseEvent e) {}
			public void mouseReleased(MouseEvent e) {}
		});
	}
	public static void main(String[] args) {
		// TODO 自动生成的方法存根
        new Main();
	}

}

选择Main.java点击运行,第一次登陆之前需要注册,注册后会在目录中生成user.xls

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

user.xls用于记录用户名;密码;积分:用户当前积分,初级,中级和高级对应获得100,200和300分,如果已经出现一方获胜,则悔棋不能改变积分;胜场;总场,是否记住密码,是否自动登录和注册日期,充当的是简易版的数据库。

源码获取

1.CSDN积分下载地址:

https://download.csdn.net/download/qq_44273429/15210290

2.关注作者公众号海拥回复java五子棋小游戏免费获取

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2021-01-21 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 源码获取
    • 1.CSDN积分下载地址:
      • 2.关注作者公众号海拥回复java五子棋小游戏免费获取
      相关产品与服务
      容器服务
      腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档