当前位置:文档之家› java课程设计实践

java课程设计实践

课程设计说明书(论文)课程名称 Java程序设计实践题目文字聊天软件设计专业软件工程班级软件1201 学号 **********姓名赵阳起止日期 2012.06.24----2012.06.28指导教师王洪亮成绩辽宁石油化工大学计算机与通信工程学院摘要计算机网络的迅速发展,对人类社会诸多领域产生了巨大的影响。

尤其是,随着信息时代的来临,人们之间愈来愈趋向于通过网络来交流和传递信息。

而聊天系统正是当前人们使用最多的中介。

在这个平台上,许多认识或不认识的人们彼此间能够随意的相互交流。

因此,开发和构架这种平台符合时代发展的需要。

本系统在开发中用到了局域网通信机制的原理,通过直接继承Thread类来建立多线程。

开发中利用了计算机网络编程的基本理论知识,如TCP/IP协议、客户端/服务器端模式(Client/Server模式)网络编程的设计方法等。

在网络编程中对信息的读取、发送,是利用流来实现信息的交换,其中介绍了对实现一个系统的信息流分析,包含了一些基本的软件工程方法。

AbstractThe rapid development of computer networks, many areas of human society had a huge impact. In particular, with the advent of the information age, more and more people tend to pass between the networks to communicate and convey information. The chat system is currently the most people to use intermediaries. On this platform, a lot of people know or do not know each other can freely mutual exchange. Therefore, the development and architecture of this platform meets the needs of the times.The system used in the development of a local area network communication mechanism principle, through the direct successor to the Thread class to create multiple threads. Developed a computer network using basic theoretical knowledge of programming, such as TCP / IP protocol, the client / server mode (Client / Server mode) network programming design method. In network programming to read the information, sending, is the use of the exchange of information flow to achieve, which describes the realization of a systematic information flow analysis, contains some basic software engineering methods.目录1设计要求 (7)2设计原理和技术 (7)2.1设计原理与开发工具 (7)2.2设计技术 (7)3总体设计 (8)4详细设计及实现 (8)4.1详细设计 (8)4.1.1设计方法 (8)4.1.2流程图 (9)4.2实现过程 (9)4.2.1源代码 (9)4.2.2运行截图 (24)5结束语 (26)参考文献 (27)课程设计任务书正文1设计要求设计并实现一个基于TCP协议或者UDP协议的文字聊天软件,是点对点的,通信双方都可以主动发起连接。

要求:采用Windows应用程序风格,功能完整,显示直观,色彩协调,界面友好,操作简单。

2设计原理和技术2.1设计原理与开发工具文字聊天软件设计所用的原理主要是网络编程原理,其中使用到的多线程原理和图形界面原理,所用的开发工具为MyEclipse。

2.2设计技术此次使用了基于TCP协议的Socket通讯技术。

Socket通信程序的功能基本结包括以下5个基本步骤:①服务器端创建ServerSocket,并调用accept()方法监听。

②客户端创建Socket连接服务器。

③连接上之后,客户端和服务器端都打开连接到Socket的输入/输出流。

④客户端和服务器端利用输入/输出流,按照一定的协议进行读/写操作。

⑤客户端和服务器端关闭输入/输出流和Socket。

图2-1 Socket通信的步骤3总体设计通过了解课程设计要求,实现文字聊天功能,并且要把客户端和用户端整合在一个程序里,为保证要求与实现功能方便,将监听和通信功能代码设计在界面类里,简单明了,容易实现。

4详细设计及实现4.1详细设计实现课程设计要求,设计时采用了一个模块,将链接、监听、通信功能放在一个类里。

4.1.1设计方法类里包括server()函数、client()函数,server()函数创建了一个监听线程,用于监听对方的链接。

client()函数也创建了一个线程,链接对方IP地址,server()函数和client()函数中都含有通信功能,以便通信。

程序采用图形界面结构,这样使界面更加友好,在这里我使用了MyEclipse8.6,选择使用Swing组件,进行界面的排版,在里面有有许多按钮,对其中一个Button按钮进行监听,输出文本信息,达到双方通信的要求。

4.1.2流程图4.2实现过程4.2.1源代码import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import .ServerSocket; import .Socket;import java.util.Calendar;server()函数线程client()函数线程@SuppressWarnings("serial")public class ConversationWindow extends javax.swing.JFrame {public ConversationWindow() {initComponents();}//GEN-BEGIN:initComponents// <editor-fold defaultstate="collapsed" desc="Generated Code">private void initComponents() {jTextField1 = new javax.swing.JTextField();jTextField2 = new javax.swing.JTextField();jTextField3 = new javax.swing.JTextField();jToggleButton1 = new javax.swing.JToggleButton();jToggleButton2 = new javax.swing.JToggleButton();jToggleButton3 = new javax.swing.JToggleButton();jScrollPane1 = new javax.swing.JScrollPane();jScrollPane2 = new javax.swing.JScrollPane();jTextArea1 = new javax.swing.JTextArea();jTextArea2 = new javax.swing.JTextArea();jButton1 = new javax.swing.JButton();jLabel1 = new javax.swing.JLabel();setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);setTitle("聊天页面");//设置标题为“聊天页面”setResizable(false);//设置框架不可变jTextField1.setText("\u7528\u62371");jTextField1.setToolTipText("\u8bbe\u7f6e\u4f60\u7684\u6635\u79f0");jToggleButton1.setText("\u9501\u5b9a");jToggleButton1.addActionListener(new java.awt.event.ActionListener() {public void actionPerformed(java.awt.event.ActionEvent evt) {jToggleButton1ActionPerformed(evt);}});jLabel1.setText("\u5bf9\u65b9IP:");jToggleButton2.setText("\u8fde\u63a5");jToggleButton2.addActionListener(new java.awt.event.ActionListener() {public void actionPerformed(java.awt.event.ActionEvent evt) {jToggleButton2ActionPerformed(evt);}});jTextArea1.setColumns(20);jTextArea1.setEditable(false);jTextArea1.setRows(5);jScrollPane1.setViewportView(jTextArea1);jTextArea2.setColumns(20);jTextArea2.setRows(5);jScrollPane2.setViewportView(jTextArea2);jButton1.setText("\u53d1\u9001");jButton1.addActionListener(new java.awt.event.ActionListener() {public void actionPerformed(java.awt.event.ActionEvent evt) {jButton1ActionPerformed(evt);}});jTextField3.setText("\u7528\u62372");jTextField3.setToolTipText("\u8bbe\u7f6e\u5bf9\u65b9\u6635\u79f0");jToggleButton3.setText("\u9501\u5b9a");jToggleButton3.addActionListener(new java.awt.event.ActionListener(){public void actionPerformed(java.awt.event.ActionEvent evt) {jToggleButton3ActionPerformed(evt);}});javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());getContentPane().setLayout(layout);layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addContainerGap().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup( javax.swing.GroupLayout.Alignment.TRAILING,layout.createSequentialGroup().addComponent(jScrollPane2,javax.swing.GroupLayout.DEFAULT_SIZE,505,Short.MAX_VALUE).addPreferredGap(ponentPlacement.RELATED) .addComponent(jButton1,javax.swing.GroupLayout.PREFERRED_SIZE,106,javax.swing.GroupLayout.PREFERRED_SIZE)).addComponent(jScrollPane1,javax.swing.GroupLayout.DEFAULT_SIZE,618,Short.MAX_VALUE).addGroup( layout.createSequentialGroup().addComponent(jTextField1,javax.swing.GroupLayout.PREFERRED_SIZE,80,javax.swing.GroupLayout.PREFERRED_SIZE).addPreferredGap(ponentPlacement.RELATED) .addComponent(jToggleButton1).addPreferredGap(ponentPlacement.RELATED) .addComponent(jTextField3,javax.swing.GroupLayout.PREFERRED_SIZE,80,javax.swing.GroupLayout.PREFERRED_SIZE).addPreferredGap(ponentPlacement.RELATED) .addComponent(jToggleButton3).addGap(84,84,84).addComponent(jLabel1).addPreferredGap(ponentPlacement.RELATED) .addComponent(jTextField2,javax.swing.GroupLayout.PREFERRED_SIZE,132,javax.swing.GroupLayout.PREFERRED_SIZE).addPreferredGap(ponentPlacement.RELATED) .addComponent(jToggleButton2,javax.swing.GroupLayout.DEFAULT_SIZE,javax.swing.GroupLayout.DEFAULT_SIZE,Short.MAX_VALUE))).addContainerGap()));layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addContainerGap().addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(jTextField1,javax.swing.GroupLayout.PREFERRED_SIZE,25,javax.swing.GroupLayout.PREFERRED_SIZE).addComponent(jToggleButton1,javax.swing.GroupLayout.PREFERRED_SIZE,25,javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jTextField3,javax.swing.GroupLayout.PREFERRED_SIZE, 25,javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jToggleButton2,javax.swing.GroupLayout.PREFERRED_SIZE, 25,javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jToggleButton3,javax.swing.GroupLayout.PREFERRED_SIZE, 25,javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jTextField2,javax.swing.GroupLayout.PREFERRED_SIZE,25,javax.swing.GroupLayout.PREFERRED_SIZE).addComponent(jLabel1)).addPreferredGap(ponentPlacement.RELATED).addComponent(jScrollPane1,javax.swing.GroupLayout.PREFERRED_SIZE,198,javax.swing.GroupLayout.PREFERRED_SIZE).addPreferredGap(ponentPlacement.RELATED).addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jButton1,javax.swing.GroupLayout.DEFAULT_SIZE,76,Short.MAX_VALUE).addComponent(jScrollPane2,javax.swing.GroupLayout.PREFERRED_SIZE,76,javax.swing.GroupLayout.PREFERRED_SIZE)).addContainerGap()));jToggleButton1.getAccessibleContext().setAccessibleName("");pack();setVisible(true);}// </editor-fold>//GEN-END:initComponentsprivate void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { Calendar c = Calendar.getInstance();//调用系统时间,使输出显示时间。

相关主题