新聞中心
急需一個java編程實現(xiàn)的簡單聊天窗口代碼
import java.awt.*;
按需規(guī)劃網(wǎng)站可以根據(jù)自己的需求進行定制,網(wǎng)站設計、成都網(wǎng)站建設構(gòu)思過程中功能建設理應排到主要部位公司網(wǎng)站設計、成都網(wǎng)站建設的運用實際效果公司網(wǎng)站制作網(wǎng)站建立與制做的實際意義
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
import java.io.*;
public class ClientDemo01 {
public static void main(String[] args){
JFrame f=new JFrame("AA");
JPanel p1=new JPanel();
JPanel p2=new JPanel();
JTextArea ta=new JTextArea(15,30);
ta.setEditable(false); //文本域只讀
JScrollPane sp=new JScrollPane(ta); //滾動窗格
JTextField tf=new JTextField(20);
JButton b=new JButton("發(fā)送");
p1.add(sp);
p2.add(tf);
p2.add(b);
f.add(p1,"Center");
f.add(p2,"South");
f.setBounds(300,300,360,300);
f.setVisible(true);
f.setResizable(false);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Socket socket=null;
BufferedInputStream bis=null;
BufferedOutputStream bos=null;
try{
socket=new Socket("192.168.0.4",5000);
bis=new BufferedInputStream(socket.getInputStream());
bos=new BufferedOutputStream(socket.getOutputStream());
MyThread01 mt=new MyThread01(bis,ta);
mt.start();
}catch(Exception e){
e.printStackTrace();
}
b.addActionListener(new ButtonActionListener01(tf,ta,bos));
}
}
class ButtonActionListener01 implements ActionListener{
JTextField tf;
JTextArea ta;
BufferedOutputStream bos;
public ButtonActionListener01(JTextField tf,JTextArea ta,BufferedOutputStream bos){
this.tf=tf;
this.ta=ta;
this.bos=bos;
}
public void actionPerformed(ActionEvent e){
String message=tf.getText();
if(!message.equals("")){
tf.setText(""); //清空文本框
ta.append("AA:"+message+"\n"); //添加到文本域并換行
try{
bos.write(message.getBytes());
bos.flush();
}catch(Exception ex){
System.out.println("發(fā)送失敗");
}
}
}
}
class MyThread01 extends Thread{
BufferedInputStream bis;
JTextArea ta;
public MyThread01(BufferedInputStream bis,JTextArea ta){
this.bis=bis;
this.ta=ta;
}
public void run(){
try{
while(true){
byte[] b=new byte[100];
int length=bis.read(b);
String message=new String(b,0,length);
ta.append("BB:"+message+"\n");
}
}catch(Exception e){
e.printStackTrace();
}
}
} import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
import java.io.*;
public class ServerDemo01{
public static void main(String[] args){
JFrame f=new JFrame("BB");
JPanel p1=new JPanel();
JPanel p2=new JPanel();
JTextArea ta=new JTextArea(12,30); //文本域,第一個參數(shù)為行數(shù),第二個參數(shù)為列數(shù)
ta.setEditable(false); //文本域只讀
JScrollPane sp=new JScrollPane(ta); //滾動窗格
JTextField tf=new JTextField(20);
JButton b=new JButton("發(fā)送");
p1.add(sp);
p2.add(tf);
p2.add(b);
f.add(p1,"Center");
f.add(p2,"South");
f.setBounds(300,300,360,300);
f.setVisible(true);
f.setResizable(false);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ServerSocket server=null;
Socket socket=null;
BufferedInputStream bis=null;
BufferedOutputStream bos=null;
try{
server=new ServerSocket(5000);
//ta.append("等待AA連接...\n");
socket=server.accept();
//ta.append("AA已連接\n");
bis=new BufferedInputStream(socket.getInputStream());
bos=new BufferedOutputStream(socket.getOutputStream());
MyThread1 mt=new MyThread1(bis,ta);
mt.start();
}catch(Exception e){
e.printStackTrace();
}
b.addActionListener(new ButtonActionListener1(tf,ta,bos));
}
}
class ButtonActionListener1 implements ActionListener{
JTextField tf;
JTextArea ta;
BufferedOutputStream bos;
public ButtonActionListener1(JTextField tf,JTextArea ta,BufferedOutputStream bos){
this.tf=tf;
this.ta=ta;
this.bos=bos;
}
public void actionPerformed(ActionEvent e){
String message=tf.getText(); //獲取文本框中的內(nèi)容
if(!message.equals("")){
tf.setText(""); //清空文本框
ta.append("BB:"+message+"\n"); //添加到文本域并換行
try{
bos.write(message.getBytes());
bos.flush();
}catch(Exception ex){
System.out.println("發(fā)送失敗!");
}
}
}
}
class MyThread1 extends Thread{
BufferedInputStream bis;
JTextArea ta;
public MyThread1(BufferedInputStream bis,JTextArea ta){
this.bis=bis;
this.ta=ta;
}
public void run(){
try{
while(true){
byte[] b=new byte[100];
int length=bis.read(b);
String message=new String(b,0,length);
ta.append("AA:"+message+"\n");
}
}catch(Exception e){
e.printStackTrace();
}
}
}
求一個用java編寫的套接字實現(xiàn)類似于QQ對話的程序,急用
有兩個類,服務器和客戶端、
服務器類代碼:
package chat;
import java.io.*;
import java.net.*;
import java.util.*;
/*服務器類*/
public class Server {
public ListSocket socketList = new ArrayListSocket();//存放所有連接的客戶端的集合
public ServerSocket server;//服務器
public int portNum;//端口號
public Server(int portNum){
this.portNum = portNum;
}
public void innit(){
try {
server = new ServerSocket(portNum);
System.out.println("服務器開啟成功!");
int socketNum = 0;
while(true){
Socket socket = server.accept();//被動等待客戶端的連接
socketNum++;
System.out.println("第"+socketNum+"個客戶端連接成功!!");
socketList.add(socket);//連接的客戶端存放到集合里面
new RWThread(socket).start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
class RWThread extends Thread{//接收和發(fā)送消息的線程
public Socket socket;
public RWThread(Socket socket){
this.socket = socket;
}
public void run() {
super.run();
try {
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
while(true){
String message = br.readLine();
System.out.println(message);//接收客戶端發(fā)來的消息
for(int i=0;isocketList.size();i++){//發(fā)送給所有連接的客戶端
PrintWriter pw = new PrintWriter(new OutputStreamWriter(socketList.get(i).getOutputStream()));
pw.println(message);
pw.flush();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
int portNum = 8888;//創(chuàng)建服務器的端口號
Server server = new Server(portNum);
server.innit();
}
}
客戶端類:
package chat;
import java.io.*;
import java.net.*;
import java.util.*;
/*客戶端類*/
public class Client {
public Socket socket;
public Client(){
Scanner sca = new Scanner(System.in);
try {
socket = new Socket("127.0.0.1",8888);//創(chuàng)建客戶端
new ReadThread(socket).start();//開啟讀取從服務器端發(fā)來的信息
while(true){
PrintWriter pw = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
String message = sca.nextLine();
pw.println(message);//向服務器發(fā)送信息
pw.flush();
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
System.out.println("錯誤:服務器未開啟!!!");
}
}
class ReadThread extends Thread{//讀取服務器發(fā)來信息的線程
public Socket socket;
public ReadThread(Socket socket){
this.socket = socket;
}
public void run() {
super.run();
try {
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
while(true){
String message = br.readLine();
System.out.println(message);//輸出信息
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
Client client = new Client();
}
}
現(xiàn)打開服務器類,然后可以打開多個客戶端進行聊天
java怎么創(chuàng)建消息對話框
復雜的對話消息框可以參考JDialog
說明: JDialog的寫法和JFrame基本類似. 可以自由添加組件等,代碼量偏多.
簡單的消息對話框可以使用JOptionPane
說明: 功能較少, 可拓展性不強,但是代碼非常簡潔. 適合大多數(shù)的應用場景.
效果圖
舉例:
public?class?Demo?{
public?static?void?main(String[]?args)?{
JOptionPane.showMessageDialog(null,?"提示:今天天氣不錯喲~");??
JOptionPane.showMessageDialog(null,?"提示:?6/0出錯,?被除數(shù)不能為0!?",?"警告",JOptionPane.ERROR_MESSAGE);??
}
}
關于觸發(fā)的舉例
效果圖
參考代碼
import?java.awt.*;
import?java.awt.event.*;
import?javax.swing.*;
//該窗口繼承自JFrame.?
public?class?DemoFrame?extends?JFrame?implements?ActionListener{
JTextField?jtf;
JButton?jb;
public?DemoFrame()?{
jtf?=?new?JTextField(8);
jtf.setText("Hello?~");
jb?=?new?JButton("顯示文本框的內(nèi)容");
jb.addActionListener(this);
JPanel?jp?=?new?JPanel();
jp.add(jtf);
jp.add(jb);
add(jp);
setTitle("窗口");//?窗口標題
setSize(380,?185);//?窗口大小
setLocationRelativeTo(null);//?窗口居中
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//?通常添加這行代碼,點擊窗口右下角的關閉時會結(jié)束程序
setVisible(true);
}
//?main方法
public?static?void?main(String[]?args)?{
new?DemoFrame();
}
@Override
public?void?actionPerformed(ActionEvent?e)?{
JButton?jb1?=?(JButton)?e.getSource();
if(jb==jb1)?{
JOptionPane.showMessageDialog(null,?"文本框的內(nèi)容是:"+jtf.getText());
}
}
}
拓展:
更多的關于JDialog和JOptionPane兩個組件的使用方法, 可以查看java API文檔
建議經(jīng)常查看java的 API文檔, 網(wǎng)上有很多的中文版. 不熟悉的類和方法,就看看, 是學習的利器~
關于仿QQ聊天對話框的JAVA代碼
1、swing的界面可以直接用netbeans畫出來嘛。
2、可以把輸出的聊天內(nèi)容都放在一個StringBuffer里,每打出一句話,就把這句話追加在StringBuffer,然后把StringBuffer里的內(nèi)容輸出到Textarea中。
3、好友列表可以用JList
本文標題:java生成對話代碼 如何用java語言實現(xiàn)聊天程序
鏈接分享:http://www.ef60e0e.cn/article/doghpph.html