java聊天軟體
『壹』 java 圖形界面 聊天軟體
package QQ;
import java.net.*;
import java.util.*;
import java.io.*;
public class QQServer {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
ServerSocket ss = new ServerSocket(9000);
Map map = new HashMap();
while (true) {
Socket s = ss.accept();
int len = 1;
Thread t = new ServerThread(s, len, map);
t.start();
}
}
}
class ServerThread extends Thread {
Socket s;
int len;
Map map;
public ServerThread(Socket s, int len, Map map) {
super();
this.s = s;
this.len = len;
this.map = map;
}
String s4 = null;
String s3 = null;
String[] string = new String[3];
public void run() {
try {
InputStream is = s.getInputStream();
InputStreamReader ir = new InputStreamReader(is);
BufferedReader in = new BufferedReader(ir);
while ((s3 = in.readLine()) != null) {
if (s3.indexOf(":name") != -1) {
StringTokenizer st = new StringTokenizer(s3, ":");
if (st.hasMoreTokens()) {
String str = st.nextToken();
s4 = str;
map.put(str, s);
Collection c=map.values();
Iterator it=c.iterator();
while(it.hasNext()){
Socket socket=(Socket )it.next();
OutputStream ops = socket.getOutputStream();
PrintWriter pw = new PrintWriter(ops);
Set set=map.keySet();
Iterator it1=set.iterator();
while(it1.hasNext()){
String s5=(String)it1.next();
pw.println(s5 + ":name");
pw.flush();
}
pw.println(s4+":welcome");
pw.flush();
}
}
/*
Set set = map.keySet();
Iterator it = set.iterator();
while (it.hasNext()) {
Object o = it.next();
String s = (String) o;
Collection c = map.values();
Iterator it1 = c.iterator();
while (it1.hasNext()) {
Socket socket = (Socket) it1.next();
OutputStream ops = socket.getOutputStream();
PrintWriter pw = new PrintWriter(ops);
pw.println(s + ":name");
pw.flush();
}
}
*/
} else {
StringTokenizer st = new StringTokenizer(s3, ":");
int z = 0;
while (st.hasMoreTokens()) {
string[z] = st.nextToken();
z++;
}
Set set = map.keySet();
Iterator it = set.iterator();
int i = 0;
while (it.hasNext()) {
Object o = it.next();
String s = (String) o;
if (string[1].equals(s)) {
Socket s1 = (Socket) map.get(s);
OutputStream ops = s1.getOutputStream();
PrintWriter pw = new PrintWriter(ops);
pw.println(string[0] + string[2]);
pw.flush();
i = i + 1;
}
}
if (i == 0) {
Collection c = map.values();
Iterator it1 = c.iterator();
while (it1.hasNext()) {
Socket s = (Socket) it1.next();
OutputStream ops = s.getOutputStream();
PrintWriter pw = new PrintWriter(ops);
pw.println(s3);
pw.flush();
}
}
}
}
// 用戶離開......................................
} catch (IOException e) {
//e.printStackTrace();
}finally{
String temp = s4;
map.remove(s4);
Collection c = map.values();
Iterator it1 = c.iterator();
try {
while (it1.hasNext()) {
Socket socket = (Socket) it1.next();
OutputStream ops = socket.getOutputStream();
PrintWriter pw = new PrintWriter(ops);
pw.println(temp + ":離開聊天室");
pw.flush();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
試試
『貳』 java聊天軟體中的文件傳輸功能構架是怎麼回事
簡單說一下吧。
過程:
首先我下載了電腦上的java模擬器,然後選用了QQ2008和QQ2007正式版和lite版這3個版本研究。
首先說2007lite,大家都知道,2007lite用代理能登陸。
這個原因很簡單,QQ2007lite用的是http代理協議,所以代理時,用的是http連接伺服器。
經過試驗,電腦上用代理連接時,只是用了http協議的網路。
然後說說QQ2007正式版和QQ2008(這2個版本區別不大,以下簡稱QQ2008)
在電腦java模擬器上運行時,QQ2008登陸時用的是socks協議(看清楚是socks,不是http)
然後,用QQ2008的瀏覽器上網,用了http協議。所以說,要登陸QQ2008,手機和網路必須支持socks。
厄。。用QQ來說明。。不知道你怎麼看。。MSN我沒測試過
『叄』 java聊天軟體設計論文
java聊天軟體設計論文
摘 要
隨著互聯網的快速發展,網路聊天工具已經作為一種重要的信息交流工具,受到越來越多的網民的青睞。目前,出現了很多非常不錯的聊天工具,其中應用比較廣泛的有Netmeeting、騰訊QQ、MSN-Messager等等。該系統開發主要包括一個網路聊天伺服器程序和一個網路聊天客戶程序兩個方面。前者通過Socket套接字建立伺服器,伺服器能讀取、轉發客戶端發來信息,並能刷新用戶列表。後者通過與伺服器建立連接,來進行客戶端與客戶端的信息交流。其中用到了區域網通信機制的原理,通過直接繼承Thread類來建立多線程。開發中利用了計算機網路編程的基本理論知識,如TCP/IP協議、客戶端/伺服器端模式(Client/Server模式)、網路編程的設計方法等。在網路編程中對信息的讀取、發送,是利用流來實現信息的交換,其中介紹了對實現一個系統的信息流的分析,包含了一些基本的軟體工程的方法。經過分析這些情況,該區域網聊天工具採用Eclipse為基本開發環境和java語言進行編寫,首先可在短時間內建立系統應用原型,然後,對初始原型系統進行不斷修正和改進,直到形成可行系統
關鍵詞:區域網 聊天 socket java
1 緒論
隨著互聯網逐步普及,人們的生活和工作也越來越離不開信息網路的支持, 而聊天室是人們最常見, 最直接的網上交流的方式。本聊天系統以聊天交流為主,為廣大用戶提供一個藉助網路進行人際交往的平台,也是網路與現實最貼近的實用型網站。本文所介紹的網路聊天系統是基於開放的JAVA應用程序開發設計的,其主要特性是能動態、實時的完成信息的傳遞,且具有高效的交互性,更有效的處理客戶請求,易於維護和更新,其運行所需環境及其工作流程和各個功能控制項的工作原理將在本文依次介紹,並且文中提供了部分程序源代碼。
2 規劃設計
2.1 課題來源
根據當前網路的需求,網路聊天越來越受各種網民所青睞。因此開發網路聊天是相當有必要,而且在網站內增加聊天功能,它不僅可以提高網站的訪問量,同時可以留著訪客,更重要的是讓訪客透過聊天室實時的互相交流。而本人也學習過JAVA語言,對網路編程也較有興趣,為了更好的考驗自己對JAVA語言的掌握程度,本人就決定以《基於JAVA聊天設計與實現》為畢業設計,希望通過這一次的能進一步提高本人的網路開發編程的能力。
2.2需求分析
聊天系統不外乎兩個方面,伺服器端和客戶端。簡單分析一下兩個方面所要完成的任務,對設計這個程序來說,等於完成了一半。首先來看一下伺服器端的任務:
1.伺服器端應當建立一個ServerSocket,並且不斷進行偵聽是否有客戶端連接或者斷開連接(包括判斷沒有響應的連接超時)。
2.伺服器端應當是一個信息發送中心,所有客戶端的信息都傳到伺服器端,由伺服器端根據要求分發信息。
以上就是伺服器端最主要的兩個任務。不難看出,伺服器端的任務並不復雜。
客戶端應該完成的工作包括:
1.與伺服器端建立通信通道,向伺服器端發送信息。
2.接收來自伺服器的信息。
相對伺服器而言,客戶端的任務更加簡單,有了以上的簡單分析,可以知道,解決上述四個問題,即完成了該聊天系統的核心。
3 系統分析與設計方案
3.1 聊天系統的總體設計要點
聊天系統的設計跟普通網站設計有著許多不同的地方,普通網站設計所考慮的因素,例如,普通網站需要對布局進入大量美化以及動畫設計等等,而聊天室只要提供滿足訪客雙方直接實時聊天即可。因此,在設計聊天系統的過程中,必須要考慮好以下幾個設計要點:
1、實現思想
在Internet上的聊天程序一般都是以伺服器提供服務端連接響應,使用者通過客戶端程序登錄到伺服器,就可以與登錄在同一伺服器上的用戶交談,這是一個面向連接的通信過程。因此,程序要在TCP/IP環境下,實現伺服器端和客戶端兩部分程序。
2、伺服器端工作流程
伺服器端通過socket()系統調用創建一個Socket數組後(即設定了接受連接客戶的最大數目),與指定的本地埠綁定bind(),就可以在埠進行偵聽listen()。如果有客戶端連接請求,則在數組中選擇一個空Socket,將客戶端地址賦給這個Socket。然後登錄成功的客戶就可以在伺服器上聊天了。
3、客戶端工作流程
客戶端程序相對簡單,只需要建立一個Socket與伺服器端連接,成功後通過這個Socket來發送和接收數據就可以了。
『肆』 java聊天軟體程序設計
客戶端
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
public class ChatClient extends Frame {
Socket s = null;
DataOutputStream dos = null;
DataInputStream dis = null;
private boolean bConnected = false;
TextField tfTxt = new TextField();
TextArea taContent = new TextArea();
Thread tRecv = new Thread(new RecvThread());
public static void main(String[] args) {
new ChatClient().launchFrame();
}
public void launchFrame() {
setLocation(400, 300);
this.setSize(300, 300);
add(tfTxt, BorderLayout.SOUTH);
add(taContent, BorderLayout.NORTH);
pack();
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent arg0) {
disconnect();
System.exit(0);
}
});
tfTxt.addActionListener(new TFListener());
setVisible(true);
connect();
tRecv.start();
}
public void connect() {
try {
s = new Socket("127.0.0.1", 8888);
dos = new DataOutputStream(s.getOutputStream());
dis = new DataInputStream(s.getInputStream());
System.out.println("connected!");
bConnected = true;
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void disconnect() {
try {
dos.close();
dis.close();
s.close();
} catch (IOException e) {
e.printStackTrace();
}
/*
try {
bConnected = false;
tRecv.join();
} catch(InterruptedException e) {
e.printStackTrace();
} finally {
try {
dos.close();
dis.close();
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
*/
}
private class TFListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String str = tfTxt.getText().trim();
//taContent.setText(str);
tfTxt.setText("");
try {
//System.out.println(s);
dos.writeUTF(str);
dos.flush();
//dos.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
private class RecvThread implements Runnable {
public void run() {
try {
while(bConnected) {
String str = dis.readUTF();
//System.out.println(str);
taContent.setText(taContent.getText() + str + '\n');
}
} catch (SocketException e) {
System.out.println("退出了,bye!");
} catch (EOFException e) {
System.out.println("推出了,bye - bye!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
伺服器端
import java.io.*;
import java.net.*;
import java.util.*;
public class ChatServer {
boolean started = false;
ServerSocket ss = null;
List<Client> clients = new ArrayList<Client>();
public static void main(String[] args) {
new ChatServer().start();
}
public void start() {
try {
ss = new ServerSocket(8888);
started = true;
} catch (BindException e) {
System.out.println("埠使用中....");
System.out.println("請關掉相關程序並重新運行伺服器!");
System.exit(0);
} catch (IOException e) {
e.printStackTrace();
}
try {
while(started) {
Socket s = ss.accept();
Client c = new Client(s);
System.out.println("a client connected!");
new Thread(c).start();
clients.add(c);
//dis.close();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
ss.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class Client implements Runnable {
private Socket s;
private DataInputStream dis = null;
private DataOutputStream dos = null;
private boolean bConnected = false;
public Client(Socket s) {
this.s = s;
try {
dis = new DataInputStream(s.getInputStream());
dos = new DataOutputStream(s.getOutputStream());
bConnected = true;
} catch (IOException e) {
e.printStackTrace();
}
}
public void send(String str) {
try {
dos.writeUTF(str);
} catch (IOException e) {
clients.remove(this);
System.out.println("對方退出了!我從List裡面去掉了!");
//e.printStackTrace();
}
}
public void run() {
try {
while(bConnected) {
String str = dis.readUTF();
System.out.println(str);
for(int i=0; i<clients.size(); i++) {
Client c = clients.get(i);
c.send(str);
//System.out.println(" a string send !");
}
/*
for(Iterator<Client> it = clients.iterator(); it.hasNext(); ) {
Client c = it.next();
c.send(str);
}
*/
/*
Iterator<Client> it = clients.iterator();
while(it.hasNext()) {
Client c = it.next();
c.send(str);
}
*/
}
} catch (EOFException e) {
System.out.println("Client closed!");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(dis != null) dis.close();
if(dos != null) dos.close();
if(s != null) {
s.close();
//s = null;
}
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
}
『伍』 java 做聊天軟體
網編?
除了要用swing做界面,你還需要Socket 和 ServerSocket 類來進行網路連接。最好還得用多線程,以實現多人聊天。
不知道你是要做成群類的,還是一對一的對話框類的。
那裡不會,你在補充吧
『陸』 怎麼用JAVA做個聊天工具
先要做個登錄界面, 再做聊天界面 ,伺服器端, 再連資料庫, 我這剛剛寫過這個程序,
這只是一個登錄了
import java.awt.* ;
import javax.swing.* ;
import java.awt.event.*;
import java.net.* ;
public class Login extends JFrame implements ActionListener {
JTextField t_username = new JTextField() ;
JPasswordField t_password = new JPasswordField() ;
Login() {
//設置窗體屬性
this.setSize(250 , 150) ;
this.setTitle("QQ登錄") ;
int width = (int)Toolkit.getDefaultToolkit().getScreenSize().getWidth() ;
int height = (int)Toolkit.getDefaultToolkit().getScreenSize().getHeight() ;
this.setLocation((width-250)/2,(height-150)/2) ;
//new一大堆組件
JLabel l_username = new JLabel("用戶名") ;
JLabel l_password = new JLabel("密碼") ;
JButton b_login = new JButton("登錄") ;
JButton b_cancel = new JButton("取消") ;
JButton b_reg = new JButton("注冊") ;
//注冊事件監聽
b_login.addActionListener(this) ;
b_cancel.addActionListener(this) ;
b_reg.addActionListener(this) ;
//布置輸入面板
JPanel p_input = new JPanel() ;
p_input.setLayout(new GridLayout(2 ,2 )) ;
p_input.add(l_username) ;
p_input.add(t_username) ;
p_input.add(l_password) ;
p_input.add(t_password) ;
//布置按鈕面板
JPanel p_button = new JPanel() ;
p_button.setLayout(new FlowLayout()) ;
p_button.add(b_login) ;
p_button.add(b_cancel) ;
p_button.add(b_reg) ;
//布置窗體
this.setLayout(new BorderLayout()) ;
this.add(p_input , BorderLayout.CENTER) ;
this.add(p_button , BorderLayout.SOUTH) ;
}
public static void main(String args[]){
Login w = new Login() ;
w.setVisible(true) ;
}
/**
* Method actionPerformed
*
*
* @param e
*
*/
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("登錄")){
//將用戶名和密碼發送到伺服器
try {
Socket s = new Socket("127.0.0.1" , 8000) ;
MyNet mn = new MyNet(s) ;
mn.sender(t_username.getText()+"%"+t_password.getText()) ;
//接收伺服器發送來的確認信息
if(mn.receive().equals("ok")){
Main w = new Main(t_username.getText()) ;
w.setMyNet(mn) ;
w.setVisible(true) ;
this.setVisible(false) ;
}
}
catch (Exception ex) {
}
}
if(e.getActionCommand().equals("取消")){
System.exit(0) ;
}
if(e.getActionCommand().equals("注冊")){
}
}
}
『柒』 如何用Java寫聊天軟體
做界面肯定要swing 然後結合Socket編寫網路程序 多個客戶端的話 要啟動線程來配置每個客戶端
『捌』 怎麼樣用JAVA做個聊天軟體
/**
* 基於UDP協議的聊天程序
*
* 2007.9.18
* */
//導入包
import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.net.*;
public class Chat extends JFrame implements ActionListener
{
//廣播地址或者對方的地址
public static final String sendIP = "127.0.0.1";
//發送埠9527
public static final int sendPort = 8000;
JPanel p = new JPanel();
List lst = new List(); //消息顯示
JTextField txtIP = new JTextField(18); //填寫IP地址
JTextField txtMSG = new JTextField(20); //填寫發送消息
JLabel lblIP = new JLabel("IP地址:");
JLabel lblMSG = new JLabel("消息:");
JButton btnSend = new JButton("發送");
byte [] buf;
//定義DatagramSocket的對象必須進行異常處理
//發送和接收數據報包的套接字
DatagramSocket ds = null;
//=============構造函數=====================
public Chat()
{
CreateInterFace();
//注冊消息框監聽器
txtMSG.addActionListener(this);
btnSend.addActionListener(this);
try
{
//埠:9527
ds =new DatagramSocket(sendPort);
}
catch(Exception ex)
{
ex.printStackTrace();
}
//============接受消息============
//匿名類
new Thread(new Runnable()
{
public void run()
{
byte buf[] = new byte[1024];
//表示接受數據報包
while(true)
{
try
{
DatagramPacket dp = new DatagramPacket(buf,1024,InetAddress.getByName(txtIP.getText()),sendPort);
ds.receive(dp);
lst.add("【消息來自】◆" + dp.getAddress().getHostAddress() + "◆"+"【說】:" + new String (buf,0,dp.getLength()) /*+ dp.getPort()*/,0);
}
catch(Exception e)
{
if(ds.isClosed())
{
e.printStackTrace();
}
}
}
}
}).start();
//關閉窗體事件
this.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent w)
{
System.out.println("test");
int n=JOptionPane.showConfirmDialog(null,"是否要退出?","退出",JOptionPane.YES_NO_OPTION);
if(n==JOptionPane.YES_OPTION)
{
dispose();
System.exit(0);
ds.close();//關閉ds對象//關閉數據報套接字
}
}
});
}
//界面設計布局
public void CreateInterFace()
{
this.add(lst,BorderLayout.CENTER);
this.add(p,BorderLayout.SOUTH);
p.add(lblIP);
p.add(txtIP);
p.add(lblMSG);
p.add(txtMSG);
p.add(btnSend);
txtIP.setText(sendIP);
//背景顏色
lst.setBackground(Color.yellow);
//JAVA默認風格
this.setUndecorated(true);
this.getRootPane().setWindowDecorationStyle(JRootPane.FRAME);
this.setSize(600,500);
this.setTitle("〓聊天室〓");
this.setResizable(false);//不能改變窗體大小
this.setLocationRelativeTo(null);//窗體居中
this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
this.setVisible(true);
txtMSG.requestFocus();//消息框得到焦點
}
//===============================Main函數===============================
public static void main(String[]args)
{
new Chat();
}
//================================發送消息===============================
//消息框回車發送消息事件
public void actionPerformed(ActionEvent e)
{
//得到文本內容
buf = txtMSG.getText().getBytes();
//判斷消息框是否為空
if (txtMSG.getText().length()==0)
{
JOptionPane.showMessageDialog(null,"發送消息不能為空","提示",JOptionPane.WARNING_MESSAGE);
}
else{
try
{
InetAddress address = InetAddress.getByName(sendIP);
DatagramPacket dp = new DatagramPacket(buf,buf.length,InetAddress.getByName(txtIP.getText()),sendPort);
ds.send(dp);
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
txtMSG.setText("");//清空消息框
//點發送按鈕發送消息事件
if(e.getSource()==btnSend)
{
buf = txtMSG.getText().getBytes();
try
{
DatagramPacket dp = new DatagramPacket(buf,buf.length,InetAddress.getByName(txtIP.getText()),sendPort);
}
catch(Exception ex)
{
ex.printStackTrace();
}
txtMSG.setText("");//清空消息框
txtMSG.requestFocus();
}
}
}
『玖』 誰知道有像米聊一樣的聊天軟體,是JAVA通用版本
目前還有愛悠悠和QQ通訊錄支持發送語音和圖片,具了解沃友也將推出通用的JAVA版本
『拾』 JAVA編寫的聊天工具
//以下引入包
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.applet.*;
public class regit extends JApplet implements ActionListener,ItemListener//介面
{
String s=null;
//對象的聲明
JLabel labelname,labelpass,labelsxe,labeladdress,labelmail,labelphone;//標簽
JTextField textname,textaddress,textmail,textphone;//文本框
JRadioButton r1,r2;//單選按紐
ButtonGroup bg;//組
JPasswordField textpass;//密碼域
JButton buttonregit,buttonreset;//注冊按紐 重寫按紐
JPanel p;//面板
String sex;//定義性別字元串
URL url;//統一資源定位
BufferedWriter out1,out2;//流
BufferedReader in;
//布局方式
GridBagLayout gbl;
GridBagConstraints gc;
AppletContext co;//介面
//初始化
public void init()
{
//new 對象
labelname=new JLabel("用 戶 名:");
labelpass=new JLabel("用戶密碼:");
labelsxe=new JLabel("性別:");
labeladdress=new JLabel("地址:");
labelmail=new JLabel("電子郵件:");
labelphone=new JLabel("聯系電話:");
textname=new JTextField(15); textname.setForeground(Color.red);
textname.setToolTipText("請在這輸入你的用戶名");
textaddress=new JTextField(15);textaddress.setForeground(Color.red);
textaddress.setToolTipText("請在這輸入你的地址");
textmail=new JTextField(15); textmail.setForeground(Color.red);
textmail.setToolTipText("請在這里輸入你的E-mail地址");
textphone=new JTextField(15); textphone.setForeground(Color.red);
textphone.setToolTipText("請在這輸入你的電話號碼");
r1=new JRadioButton("男"); r1.setBackground(new Color(47,177,210));//設置顏色
r2=new JRadioButton("女"); r2.setBackground(new Color(47,177,210));//設置顏色
bg=new ButtonGroup();
bg.add(r1);bg.add(r2);//加入組,實現單選
textpass=new JPasswordField(15);
textpass.setToolTipText("在這里輸入密碼");
textpass.setForeground(Color.red);
buttonregit=new JButton("注冊"); buttonregit.setBackground(new Color(47,177,210));//設置顏色
buttonregit.setToolTipText("點擊按紐完成注冊");
buttonreset=new JButton("填寫"); buttonreset.setBackground(new Color(47,177,210));//設置顏色
buttonreset.setToolTipText("點擊按紐刷新重寫");
gbl=new GridBagLayout(); ///////////////////////////////////////
gc=new GridBagConstraints(); //////採用GridBagLayout布局方式////////
p=new JPanel();
p.setLayout(gbl);
p.setBackground(new Color(47,177,210));
this.getContentPane().add(p);//加入面板
gc.anchor=GridBagConstraints.NORTHWEST;
gc.gridx=2;
gc.gridy=2;
gbl.setConstraints(labelname,gc);
p.add(labelname);
gc.anchor=GridBagConstraints.NORTHWEST;
gc.gridx=4;
gc.gridy=2;
gbl.setConstraints(textname,gc);
p.add(textname);
gc.anchor=GridBagConstraints.NORTHWEST;
gc.gridx=2;
gc.gridy=4;
gbl.setConstraints(labelpass,gc);
p.add(labelpass);
gc.anchor=GridBagConstraints.NORTHWEST;
gc.gridx=4;
gc.gridy=4;
gbl.setConstraints(textpass,gc);
p.add(textpass);
gc.anchor=GridBagConstraints.NORTHWEST;
gc.gridx=2;
gc.gridy=6;
gbl.setConstraints(labelsxe,gc);
p.add(labelsxe);
gc.anchor=GridBagConstraints.NORTHWEST;
gc.gridx=4;
gc.gridy=6;
gbl.setConstraints(r1,gc);
p.add(r1);
gc.anchor=GridBagConstraints.NORTHWEST;
gc.gridx=5;
gc.gridy=6;
gbl.setConstraints(r2,gc);
p.add(r2);
gc.anchor=GridBagConstraints.NORTHWEST;
gc.gridx=2;
gc.gridy=8;
gbl.setConstraints(labeladdress,gc);
p.add(labeladdress);
gc.anchor=GridBagConstraints.NORTHWEST;
gc.gridx=4;
gc.gridy=8;
gbl.setConstraints(textaddress,gc);
p.add(textaddress);
gc.anchor=GridBagConstraints.NORTHWEST;
gc.gridx=2;
gc.gridy=10;
gbl.setConstraints(labelmail,gc);
p.add(labelmail);
gc.anchor=GridBagConstraints.NORTHWEST;
gc.gridx=4;
gc.gridy=10;
gbl.setConstraints(textmail,gc);
p.add(textmail);
gc.anchor=GridBagConstraints.NORTHWEST;
gc.gridx=2;
gc.gridy=12;
gbl.setConstraints(labelphone,gc);
p.add(labelphone);
gc.anchor=GridBagConstraints.NORTHWEST;
gc.gridx=4;
gc.gridy=12;
gbl.setConstraints(textphone,gc);
p.add(textphone);
gc.anchor=GridBagConstraints.NORTHWEST;
gc.gridx=2;
gc.gridy=16;
gbl.setConstraints(buttonregit,gc);
p.add(buttonregit);
gc.anchor=GridBagConstraints.NORTHWEST;
gc.gridx=4;
gc.gridy=16;
gbl.setConstraints(buttonreset,gc);
p.add(buttonreset);
/////////////////////////////////////////////
co=this.getAppletContext();
/////////////////////////////////////////////
buttonregit.addActionListener(this);//按紐事件的監聽
buttonreset.addActionListener(this);//按紐事件的監聽
r1.addItemListener(this);//選擇事件的監聽
r2.addItemListener(this);//選擇事件的監聽
textphone.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
//注冊按紐事件
if(e.getSource()==buttonregit)
{
String s1=textname.getText(); ////////////////////
String s2=new String(textpass.getPassword());////////////////////
String s3=textaddress.getText(); // 定義字元串 //
String s4=textmail.getText(); ////////////////////
String s5=textphone.getText(); ////////////////////
//判斷注冊資料 信息 是否為空
if(s1.length()==0 || s2.length()==0 ||s3.length()==0 ||s4.length()==0 ||s5.length()==0)
{
int error=JOptionPane.INFORMATION_MESSAGE;
JOptionPane.showMessageDialog(null,"資料不能為空,請重新注冊!","【溫馨提示】",error);
return;//彈出對話框並返回
}
try//寫入到txt文件
{
in=new BufferedReader(new FileReader("d:\\迷離視線聊天室\\password.txt"));
}
catch(Exception ee){}
String ss=s1;
try
{
while((s=in.readLine())!=null)
{
if(s.startsWith(ss))
{
JOptionPane.showMessageDialog(null,"用戶名已經存在,請更換名字!");
textname.setText("");//設置為空,重新輸入
textpass.setText("");
textaddress.setText("");
textmail.setText("");
textphone.setText("");
return;
}
}
}
catch(Exception ee){}
///////////////////////////以上代碼判斷是否有同名
{
try
{
out1=new BufferedWriter(new FileWriter("d:\\迷離視線聊天室\\password.txt",true));
out2=new BufferedWriter(new FileWriter("d:\\迷離視線聊天室\\message.txt",true));
}//創建文件
catch(Exception ee)
{}
try
{
out1.write(s1+"#"+s2);//寫
out1.newLine();
out2.write("用戶名:"+s1);
out2.newLine();
out2.write("密碼:"+s2);
out2.newLine();
out2.write("性別:"+sex);
out2.newLine();
out2.write("地址:"+s3);
out2.newLine();
out2.write("電子郵件:"+s4);
out2.newLine();
out2.write("電話:"+s5);
out2.newLine();
out1.flush();
out2.flush();//清理緩沖
out1.close();
out2.close();
}
catch(Exception ee)
{}
JOptionPane.showMessageDialog(null,"注冊成功!");
try
{
String qss="http://localhost/chatroom/chatjiemian.htm";
url=new url(/qss);//連接上網址
co.showDocument(url);
}
catch(Exception exx)
{}
}
}
//////////////////////以下為回車事件
if(e.getSource()==textphone)
{
String s1=textname.getText(); ////////////////////
String s2=new String(textpass.getPassword());////////////////////
String s3=textaddress.getText(); // 定義字元串 //
String s4=textmail.getText(); ////////////////////
String s5=textphone.getText(); ////////////////////
//判斷注冊資料 信息 是否為空
if(s1.length()==0 || s2.length()==0 ||s3.length()==0 ||s4.length()==0 ||s5.length()==0)
{
int error=JOptionPane.INFORMATION_MESSAGE;
JOptionPane.showMessageDialog(null,"資料不能為空,請重新注冊!","【溫馨提示】",error);
return;//彈出對話框並返回
}
try//寫入到txt文件
{
in=new BufferedReader(new FileReader("d:\\迷離視線聊天室\\password.txt"));
}
catch(Exception ee){}
String ss=s1;
try
{
while((s=in.readLine())!=null)
{
if(s.startsWith(ss))
{
JOptionPane.showMessageDialog(null,"用戶名已經存在,請更換名字!");
textname.setText("");//設置為空,重新輸入
textpass.setText("");
textaddress.setText("");
textmail.setText("");
textphone.setText("");
return;
}
}
}
catch(Exception ee){}
///////////////////////////以上代碼判斷是否有同名
{
try
{
out1=new BufferedWriter(new FileWriter("d:\\迷離視線聊天室\\password.txt",true));
out2=new BufferedWriter(new FileWriter("d:\\迷離視線聊天室\\message.txt",true));
}//創建文件
catch(Exception ee)
{}
try
{
out1.write(s1+"#"+s2);//寫
out1.newLine();
out2.write("用戶名:"+s1+"密碼:"+s2+"性別:"+sex+"地址:"+s3+"電子郵件:"+s4+"電話:"+s5);//寫
out2.newLine();
out1.flush();
out2.flush();//清理緩沖
out1.close();
out2.close();
}
catch(Exception ee)
{}
JOptionPane.showMessageDialog(null,"注冊成功!");
try
{
String qss="http://localhost/chatroom/chatjiemian.htm";
url=new url(/qss);//連接上網址
co.showDocument(url);
}
catch(Exception exx)
{}
}
}
if(e.getSource()==buttonreset)//刷新重寫事件
{
textname.setText("");
textpass.setText("");
textaddress.setText("");
textmail.setText("");
textphone.setText("");
}
}
//////////////////////////////////////////
//
public void itemStateChanged(ItemEvent ex)
{
if(ex.getSource()==r1)
{
sex=new String("男");
}
else if(ex.getSource()==r2)
{
sex=new String("女");
}
}
}