JAVA实训-连连看(二)总结

总结分为以下几个部分:

目录

一、 项目简介

二、 项目演示

三、 项目的模块化介绍

1) 登陆模块

2) 游戏模块

3) 聊天模块

四、 收获



一、 项目简介:

    初学JAVA,连连看算是比较好入门的窗体程序,连连看项目主要实现了功能:登入连接数据库、数据库基本操作、连连看基本逻辑功能、重列、计时器、积分累加、通关、保存棋盘(目前处于保存阶段 读取还没完全实现)、采用UDP模拟网络聊天;


 

 

二、项目演示:

1.登入其中,连接数据库

 

 

 

2.聊天室+基本逻辑功能+读写


左为JAVA网络编程 聊天室 右为一开始的图形界面以及信息

左为无法消除情况,中为点击了重列然后消除剩下一个,右为下一关

(文件读取操作 存档,不清楚这个是否符合老师的要求,保存了每次下的数组,为后面读取做了准备)

三、 项目的模块化介绍

3.1登陆功能

3.1.1界面设计部分                              事件监听器部分:

3.2主窗体部分:

3.3存档功能

3.4 数据交互:


3.5 基本逻辑功能:RULE

后面一切的逻辑实现建立在connect0

3.6网络编程部分:

使用UDP而没有用TCP,利用端口号实现聊天程序

下面直接附上代码。。。

2333


对于一些经典的部分,直接上代码会清晰一点!!!

采用UDP设置的一个模拟网络聊天

package com.lyd.LinkGame.view;
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.net.*;
import javax.swing.*;
public class UDP2 extends JFrame{
	private Container cp;
	private JLabel outlb,inlb;
	private JTextField outPort;
	private JTextField SentText;	
    private JTextArea ReceiveArea;    
    private JScrollPane sp;
    private JPanel p0,p1,p2; 
    private DatagramSocket datagramSocket;    
    UDP2(){
    	super("UDP聊天程序测试");
    	cp = this.getContentPane(); 
    	inlb=new JLabel("接收端口:1010");
    	outlb=new JLabel("发送端口:");    	
    	outPort = new JTextField(5);
    	outPort.setText("1020");    	
		SentText = new JTextField(20);	
        ReceiveArea = new JTextArea(10,20);
        ReceiveArea.setEditable(false);
        p0 = new JPanel(new FlowLayout(FlowLayout.LEFT));
        p0.setBorder(BorderFactory.createTitledBorder("端口信息:"));
        p0.add(outlb);
        p0.add(outPort);
        p0.add(inlb);
        cp.add(p0,BorderLayout.NORTH); 	        
        p1 = new JPanel();
        p1.setBorder(BorderFactory.createTitledBorder("接收内容:"));		
		p1.add(ReceiveArea);
		sp = new JScrollPane(p1);
		cp.add(sp,BorderLayout.CENTER); 		
		p2 = new JPanel();
        p2.setBorder(BorderFactory.createTitledBorder("发送内容:"));				
		p2.add(SentText);
        cp.add(p2,BorderLayout.SOUTH);              
        SentText.addActionListener(new SentListener());         
        this.setLocation(10,10);
        this.pack();
        this.setResizable(false); // 窗口大小不可调整
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);         
        this.setVisible(true);
    }  
    class ReceiveThread implements Runnable {   	
		public void run() {
			DatagramPacket dp;
			int port=1020;
			try {
				port = Integer.parseInt(JOptionPane.showInputDialog(null,"请输入接收端口号:",JOptionPane.QUESTION_MESSAGE));
				if (port < 1 || port > 65535) {
					throw new RuntimeException("端口号超出范围");
				}
				inlb.setText("接收端口:"+port);
				datagramSocket = new DatagramSocket(port);				
			} catch (SocketException e1) {
				e1.printStackTrace();
			}
	    	byte[] buf = new byte[1024];
	    	dp = new DatagramPacket(buf, buf.length);	    	
			String tempInfo=null; 
            while(!datagramSocket.isClosed()){
            	try {
					datagramSocket.receive(dp); // 接收聊天消息
				} catch (IOException e) {
					e.printStackTrace();
				} 
            	tempInfo ="对方说: "+ new String(dp.getData(), 0,dp.getLength())+"\n";
            	ReceiveArea.append(tempInfo);
                ReceiveArea.setCaretPosition(ReceiveArea.getText().length());//使滚动条滚动到最底端
            }			
		}     	
    } 
    class SentListener implements ActionListener{
		public void actionPerformed(ActionEvent arg0) {
			int port=1;
			port = Integer.parseInt(outPort.getText().trim());
			if (port < 1 || port > 65535) {
				throw new RuntimeException("端口号超出范围");
			}
			String tempInfo = SentText.getText().trim();
			byte[] buf = tempInfo.getBytes();
			SentText.setText("");		
			try {
				DatagramPacket dp=new DatagramPacket(buf,buf.length,InetAddress.getByName("127.0.0.1"), port);
				datagramSocket.send(dp);  // 发送聊天消息
			} catch (UnknownHostException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
			ReceiveArea.append("我说: "+tempInfo+"\n");
	        ReceiveArea.setCaretPosition(ReceiveArea.getText().length());
		}
    	
    }  
}


数据库连接

package com.lyd.LinkGame.view;

import javax.swing.JFrame;
import javax.swing.JButton;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JSplitPane;
import javax.swing.JTextField;

import com.lyd.LinkGame.data.GameData;
import com.lyd.LinkGame.view.UDP2.ReceiveThread;

public class LoginUser extends JFrame {
	// 链接数据库的驱动以及服务器
	static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
	static final String DB_URL = "jdbc:mysql://localhost:3306/RUNOOB";

	// 数据库的用户名与密码,需要根据自己的设置
	static final String USER = "root";
	static final String PASS = "root";

	JTextField name;
	JPasswordField pass;

	public LoginUser() {
		// 登陆界面的设计
		this.setTitle("登陆界面");
		Container cp = this.getContentPane();
		this.setLayout(null);
		JLabel nameLab = new JLabel("用户名称");
		name = new JTextField();
		JLabel passLab = new JLabel("密码");
		pass = new JPasswordField();
		JButton login = new JButton("登陆");
		JButton reset = new JButton("重设");
		nameLab.setBounds(45, 38, 67, 34);
		name.setBounds(115, 39, 210, 33);
		passLab.setBounds(43, 86, 66, 26);
		pass.setBounds(115, 84, 210, 33);
		login.setBounds(78, 150, 86, 30);
		reset.setBounds(193, 150, 86, 30);
		cp.add(nameLab);
		cp.add(name);
		cp.add(passLab);
		cp.add(pass);
		cp.add(login);
		cp.add(reset);
		String zhanghao = name.getText().toString();
		// 使用匿名内部类进行事件的监听
		login.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (MySQLDemo()) {
					JOptionPane.showMessageDialog(null, "登陆成功,欢迎来到我的连连看");
					MainFrame mf = new MainFrame();// 生成连连看主窗体
					mf.namejl.setText(name.getText());// 获得用户名
					dispose();// 释放当前窗体的资源
					mf.setVisible(true);
					UDP2 user1 = new UDP2();
					ReceiveThread Receive1 = user1.new ReceiveThread();
					new Thread(Receive1).start();
					UDP2 user2 = new UDP2();
					ReceiveThread Receive2 = user2.new ReceiveThread();
					new Thread(Receive2).start();
				} else {
					JOptionPane.showMessageDialog(null, "密码错误");
				}
			}
		});
		reset.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				name.setText(null);
				pass.setText(null);

			}
		});
		this.setSize(390, 250);
		this.setLocation(400, 200);
		this.setDefaultCloseOperation(3);
	}

	public boolean MySQLDemo() {
		// JDBC 驱动名及数据库 URL
		Connection conn = null;
		Statement stmt = null;
		try {
			// 注册 JDBC 驱动
			Class.forName("com.mysql.jdbc.Driver");

			// 打开链接
			System.out.println("连接数据库...");
			conn = DriverManager.getConnection(DB_URL, USER, PASS);

			// 执行查询
			System.out.println(" 实例化Statement对象...");
			stmt = conn.createStatement();
			String sql;
			sql = "SELECT id, username, password FROM UserInfo";
			ResultSet rs = stmt.executeQuery(sql);

			// 展开结果集数据库
			while (rs.next()) {
				// 通过字段检索
				int id = rs.getInt("id");
				String namesql = rs.getString("username");
				String passsql = rs.getString("password");

				
				  if( name.getText().equals(namesql)&& pass.getText().equals(passsql))
				  {
					  return true;
				  }
				 
				// return true;
				// 输出数据
				System.out.print("ID: " + id);
				System.out.print(", 名称: " + namesql);
				System.out.print(", 密码: " + passsql);
				System.out.print("\n");

			}
			// 完成后关闭
			rs.close();
			stmt.close();
			conn.close();
		} catch (SQLException se) {
			// 处理 JDBC 错误
			se.printStackTrace();
		} catch (Exception e) {
			// 处理 Class.forName 错误
			e.printStackTrace();
		} finally {
			// 关闭资源
			try {
				if (stmt != null)
					stmt.close();
			} catch (SQLException se2) {
			} // 什么都不做
			try {
				if (conn != null)
					conn.close();
			} catch (SQLException se) {
				se.printStackTrace();
			}
		}
		System.out.println("Goodbye!");
		return false;

	}

}

整个项目这样下来,主要是各科还在备考阶段,窗体程序本次实训涉及到的内容较多还是第一次用一维数组完成了二维数组的事情,也体会到了各自的优缺点,其实整个项目下来,如果不查资料一维一般是很难写出来的 很容易乱 二维接地气 但是代码太长了。。。 整个下来学会了查资料以及排错 总体感觉还是不错的 也连接了数据库做了JAVA网络编程,主要还是 项目需求分析不明确 常常做着做着不知道做什么了。。

不过好在整个项目下来 有做笔记的习惯!!!

以下附上自己刚开始学习做的笔记

https://blog.csdn.net/qq_37457202/article/details/80659017

迭代。。。 是这个意思吧。。。


猜你喜欢

转载自blog.csdn.net/qq_37457202/article/details/80719022