本文目錄一覽:
- 1、誰有java的socket的GUI聊天室程序?
- 2、用JAVA如何實現GUI的聊天室?只需實現基本的聊天室功能即可。希望給出服務器源代碼和客戶端源代碼。
- 3、關於用java多線程做的一個多人聊天小程序的一個問題——如何實現進程間的通信?
- 4、java中GUI的實現方式是???
誰有java的socket的GUI聊天室程序?
給你個,但是不是很完美(可修改一下),可以完成簡單的聊天功能.
源代碼://liaotian.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.net.*;
public class liaotian extends JFrame implements ActionListener{
public static void main(String[] args){
liaotian frame=new liaotian();
}
JButton command,command2,command3;
JRadioButton rb[]=new JRadioButton[2];
JTextArea ta1;
JTextField tf1,tf2,tf3;
ServerSocket socket1;
Socket insocket1,socket2;
String inbuf;
BufferedReader in1;
PrintWriter out1;
mt625_server t1;
mt625_client t2;
public liaotian(){
super(“聊天”);
Container c=getContentPane();
c.setLayout(null);
JLabel lb=new JLabel(“TCP通信程序”);
lb.setFont(new Font(“宋體”,Font.BOLD,16));
lb.setForeground(Color.black);
lb.setSize(2000,20);
lb.setLocation(10,2);
c.add(lb);
String str1[]={“服務端”,”客戶端”};
ButtonGroup bg1=new ButtonGroup();
for(int i=0;i2;i++){
rb[i]=new JRadioButton(str1[i]);
rb[i].setFont(new Font(“宋體”,Font.BOLD,14));
rb[i].setForeground(Color.black);
rb[i].setSize(80,20);
rb[i].setLocation(10+i*80,27);
c.add(rb[i]);
bg1.add(rb[i]);
}
rb[0].setSelected(true);
JLabel lb1=new JLabel(“連接主機IP”);
lb1.setFont(new Font(“宋體”,Font.BOLD,16));
lb1.setForeground(Color.black);
lb1.setSize(120,25);
lb1.setLocation(16,55);
c.add(lb1);
tf1=new JTextField(“59.68.255.27”);
tf1.setForeground(Color.black);
tf1.setSize(250,25);
tf1.setLocation(120,55);
c.add(tf1);
command=new JButton(“連接”);
command.setFont(new Font(“宋體”,Font.BOLD,16));
command.setSize(110,20);
command.setLocation(380,55);
command.addActionListener(this);
c.add(command);
JLabel lb2=new JLabel(“接收到信息”);
lb2.setFont(new Font(“宋體”,Font.BOLD,16));
lb2.setForeground(Color.black);
lb2.setSize(120,20);
lb2.setLocation(10,85);
c.add(lb2);
ta1=new JTextArea();
ta1.setForeground(Color.black);
ta1.setSize(250,200);
ta1.setLocation(120,85);
c.add(ta1);
JLabel lb3=new JLabel(“發送信息”);
lb3.setFont(new Font(“宋體”,Font.BOLD,16));
lb3.setForeground(Color.black);
lb3.setSize(120,25);
lb3.setLocation(10,300);
c.add(lb3);
tf2=new JTextField();
tf2.setForeground(Color.black);
tf2.setSize(250,25);
tf2.setLocation(120,300);
c.add(tf2);
command2=new JButton(“發送信息”);
command2.setFont(new Font(“宋體”,Font.BOLD,16));
command2.setSize(110,25);
command2.setLocation(380,300);
command2.addActionListener(this);
command2.setEnabled(false);
c.add(command2);
JLabel lb4=new JLabel(“連接狀態: “);
lb4.setFont(new Font(“宋體”,Font.BOLD,14));
lb4.setForeground(Color.black);
lb4.setSize(120,25);
lb4.setLocation(180,27);
c.add(lb4);
tf3=new JTextField(“離線”);
tf3.setForeground(Color.black);
tf3.setSize(120,25);
tf3.setLocation(270,27);
c.add(tf3);
command3=new JButton(“結束連接”);
command3.setFont(new Font(“宋體”,Font.BOLD,16));
command3.setSize(110,20);
command3.setLocation(380,85);
command3.addActionListener(this);
command3.setEnabled(false);
c.add(command3);
t1=new mt625_server();
t2=new mt625_client();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500,400);
setVisible(true);
setLocation(300,300);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==command){
try{
if(rb[0].isSelected()==true){
inbuf=””;
tf2.setText(“”);
t1.start();
}
else{
inbuf=””;
tf2.setText(“”);
t2.start();
}
}
catch (Exception e2) {tf3.setText(“發生錯誤”);}
}
if(e.getSource()==command2){
out1.write(tf2.getText()+”\n”);
out1.flush();
tf2.setText(“”);
}
if(e.getSource()==command3){
try{
if(rb[0].isSelected()==true){
insocket1.close();
tf3.setText(“離線”);
command2.setEnabled(false);
command3.setEnabled(false);
}
else{
socket2.close();
tf3.setText(“離線!”);
command2.setEnabled(false);
command3.setEnabled(false);
}
}catch (Exception e2) {tf3.setText(“發生錯誤”);}
}
}
class mt625_server extends Thread{
public mt625_server(){}
public void run(){
try{
command.setEnabled(false);
tf3.setText(“正在等待連接!”);
tf1.setText(Inet4Address.getLocalHost().getHostAddress());
socket1=new ServerSocket(21);
insocket1=socket1.accept();
in1=new BufferedReader(new InputStreamReader(insocket1.getInputStream()));
out1=new PrintWriter(insocket1.getOutputStream(),true);
while(true){
if(socket1.isBound()==true){
tf3.setText(“正在連接!”);
command2.setEnabled(true);
command3.setEnabled(true);
break;
}
}
while(true){
inbuf=in1.readLine();
if(inbuf.length()0)
{
ta1.append(inbuf);
ta1.append(“\n”);
}
}
}
catch(Exception e){}
}
}
class mt625_client extends Thread{
public mt625_client(){}
public void run(){
try{
command.setEnabled(false);
tf3.setText(“正在等待連接!”);
socket2=new Socket();
socket2.connect(new InetSocketAddress(tf1.getText(),21),5000);
in1=new BufferedReader(new InputStreamReader(socket2.getInputStream()));
out1=new PrintWriter(socket2.getOutputStream(),true);
while(true){
if(socket2.isConnected()==true){
tf3.setText(“正在連接!”);
command2.setEnabled(true);
command3.setEnabled(true);
break;
}
}
inbuf=””;
while(true){
inbuf=in1.readLine();
if(inbuf.length()0)
{
ta1.append(inbuf);
ta1.append(“\n”);
}
}
}
catch(Exception e){}
}
}
}
用JAVA如何實現GUI的聊天室?只需實現基本的聊天室功能即可。希望給出服務器源代碼和客戶端源代碼。
轉自 Ian Darwin’s Java Cookbook,
用的是Socket通信 + AWT實現GUI。
Code比較長,這裡貼不下,可以去下面URL直接下載。
定義常量
Server端
Client端
關於用java多線程做的一個多人聊天小程序的一個問題——如何實現進程間的通信?
傳統的進程間通信的方式有大致如下幾種:
(1) 管道(PIPE)
(2) 命名管道(FIFO)
(3) 信號量(Semphore)
(4) 消息隊列(MessageQueue)
(5) 共享內存(SharedMemory)
(6) Socket
Java如何支持進程間通信。我們把Java進程理解為JVM進程。很明顯,傳統的這些大部分技術是無法被我們的應用程序利用了(這些進程間通信都是靠系統調用來實現的)。但是Java也有很多方法可以進行進程間通信的。
除了上面提到的Socket之外,當然首選的IPC可以使用Rmi,或者Corba也可以。另外Java nio的MappedByteBuffer也可以通過內存映射文件來實現進程間通信(共享內存)。
java中GUI的實現方式是???
其實無論在什麼平台下,GUI應用程序的基本開發方法都是相似的。一般都包括下面這樣四個步驟:
①創建容器
首先要創建一個GUI應用程序,需要創建一個用於容納所有其它GUI組件元素的載體,Java中稱為容器。典型的包括窗口(Window)、框架(Frame/JFrame)、對話框(Dialog/JDialog)、面板(Panel/JPanel)等。只有先創建了這些容器,其它界面元素如按鈕(Button/JButton)、標籤(Label/JLabel)、文本框(TextField/JTextField)等才有地方放。 ②添加組件
為了實現GUI應用程序的功能,為了與用戶交換,需要在容器上添加各種組件/控件。這需要根據具體的功能要求來決定用什麼組件。例如,如果需要提示信息,可用標籤(Label/JLabel);如果需要輸入少量文本,可用文本框(TextField/JTextField);如果需要輸入較多文本,可用文本區域(TextArea/JTextArea);如果需要輸入密碼,可用密碼域(JPasswordField)等等。
③安排組件
與傳統的Windows環境下的GUI軟件開發工具不同,為了更好地實現跨平台,Java程序中各組件的位置、大小一般不是以絕對量來衡量,而是以相對量來衡量。例如有時候,程序的組件的位置是按”東/East”、”西/West”、”南/South”、”北/North”、”中 /Center”這種方位來標識的。因此,在組織界面時,除了要考慮所需的組件種類外,還需要考慮如何安排這些組件的位置與大小。這一般是通過設置布局管理器(Layout Manager)及其相關屬性來實現的。事實上上述按方位來安排組件就是採用了Java中多種布局管理器里的BorderLayout布局管理器。
④處理事件
為了完成一個GUI應用程序所應具備的功能,除了適當地安排各種組件產生美觀的界面外,還需要處理各種界面元素事件,以便真正實現與用戶的交換,完成程序的功能。在Java程序中這一般是通過實現適當的事件監聽者接口來完成的。比如如果需要響應按鈕事件,就需要實現 ActionListener監聽者接口;如果需要響應窗口事件,就需要實現WindowListener監聽者接口。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/236471.html