?? client.java
字號:
import javax.swing.*;
import java.awt.*;
import java.net.*;
import java.io.*;
import java.awt.event.*;
public class Client extends JFrame implements Runnable,ActionListener
{ //客戶端程序
//創建Socket通信端口號常量
public static final int PORT = 8765;
//創建客戶端界面面板
JPanel jPanel1 = new JPanel();
JPanel jPanel2 = new JPanel();
JPanel jPanel3 = new JPanel();
JPanel jPanel4 = new JPanel();
//創建布局管理方式
BorderLayout borderLayout1 = new BorderLayout();
JScrollPane jScrollPane2 = new JScrollPane();
FlowLayout flowLayout1 = new FlowLayout();
GridLayout gridLayout1 = new GridLayout();
//創建客戶端界面中的按鈕
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
JButton jButton3 = new JButton();
//創建用戶名錄入對話框
JTextField jTextField1 = new JTextField();
//創建聊天對話顯示區域
JTextArea jTextArea1 = new JTextArea();
//創建發言輸入框
JTextField jTextField2 = new JTextField();
//創建服務器ip地址輸入框
JTextField jTextField3 = new JTextField();
//創建服務器端口號輸入框
JTextField jTextField4 = new JTextField();
//創建標簽對象
JLabel jLabel1 = new JLabel();
JLabel jLabel2 = new JLabel();
JLabel jLabel3 = new JLabel();
Socket socket; //聲明套接字對象
Thread thread; //聲明線程對象
//聲明客戶器端數據輸入輸出流
DataInputStream in;
DataOutputStream out;
boolean bool=false;
//聲明字符串,name存儲用戶名,chat_txt存儲發言信息,chat_in存儲從服務器接收到的信息
String name,chat_txt,chat_in;
String ip=null;
public Client()
{
super("Client");
//設置內容面板無布局方式
getContentPane().setLayout(null);
//初始化客戶端界面中的各個組件,包括位置、大小以及處初始顯示內容
//初始化輸入框組件
jTextArea1.setText("");
jTextField1.setText("用戶名輸入");
jTextField1.setBounds(new Rectangle(103, 4, 77, 25));
jTextField2.setText("");
jTextField2.setBounds(new Rectangle(4, 6, 311, 22));
jTextField3.setText("");
jTextField4.setText("8765");
//初始化按鈕組件
jButton1.setBounds(new Rectangle(210, 4, 89, 25));
jButton1.setToolTipText("");
jButton1.setText("進入聊天室");
jButton1.addActionListener(this);
jButton2.setBounds(new Rectangle(322, 5, 73, 25));
jButton2.setText("發送");
jButton2.addActionListener(this);
jButton3.setBounds(new Rectangle(302, 5, 97, 24));
jButton3.setText("退出聊天室");
jButton3.addActionListener(this);
//初始化面板組件
jPanel1.setLayout(null);
jPanel1.setBounds(new Rectangle(0, 0, 400, 35));
jPanel2.setBounds(new Rectangle(264, 35, 136, 230));
jPanel2.setLayout(gridLayout1);
jPanel3.setBounds(new Rectangle(0, 35, 255, 230));
jPanel3.setLayout(borderLayout1);
jPanel4.setBounds(new Rectangle(0, 265, 400, 35));
jPanel4.setLayout(null);
//初始化標簽組件
jLabel1.setText("用戶名");
jLabel1.setBounds(new Rectangle(46, 5, 109, 23));
jLabel2.setText("服務器地址");
jLabel3.setText("端口");
//添加組件至指定面板中的固定位置
gridLayout1.setColumns(1);
gridLayout1.setRows(4);
this.getContentPane().add(jPanel1, null);
jPanel4.add(jTextField2);
jPanel4.add(jButton2);
jPanel1.add(jTextField1, null);
jPanel1.add(jLabel1);
jPanel1.add(jButton1, null);
jPanel1.add(jButton3);
this.getContentPane().add(jPanel2, null);
jPanel2.add(jLabel2);
jPanel2.add(jTextField3);
jPanel2.add(jLabel3);
jPanel2.add(jTextField4);
this.getContentPane().add(jPanel4, null);
this.getContentPane().add(jPanel3, null);
jPanel3.add(jScrollPane2, java.awt.BorderLayout.CENTER);
jScrollPane2.getViewport().add(jTextArea1);
this.setSize(400,400);
this.setVisible(true);
}
public static void main(String[] args)
{
Client client = new Client();
client.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void run() //客戶端線程啟動后的動作
{
while(true) //循環執行
{
try
{
//讀取服務器發送來的數據信息,并顯示在對跨窗口中
chat_in = in.readUTF();
jTextArea1.append(chat_in);
}
catch(IOException e){}
}
}
//界面按鈕的事件處理機制
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == jButton1)
{
name=jTextField1.getText();
ip=jTextField3.getText();
if(name!="用戶名輸入"&&ip!=null)
{
try
{
//創建Socket對象
socket = new Socket(ip, PORT);
//創建客戶端數據輸入輸出流,用于對服務器端發送或接收數據
in = new DataInputStream(socket.getInputStream());
out = new DataOutputStream(socket.getOutputStream());
} catch (IOException e1) {System.out.println("can not connect");}
thread = new Thread(this);
thread.start();
bool = true;
}
}
else if(e.getSource() == jButton2)
{
chat_txt=jTextField2.getText();
if(chat_txt!=null)
{
//發言,向服務器發送發言的信息
try{out.writeUTF(jTextField1.getText()+"對大家說:"+chat_txt+"\n");}
catch(IOException e2){}
}
else
{
try{out.writeUTF("請說話");}
catch(IOException e3){}
}
}
else if(e.getSource() == jButton3)
{
if(bool==true)
{
try
{
socket.close();
} catch (IOException e4) {}
}
thread.destroy();
bool=false;
this.setVisible(false);
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -