使用 Java 创建聊天客户端-1
1.聊天客户端文本框的搭建.
项目截图:java project
代码:
(1).ChatManager.java
package com.nantian.javachatclient.main;
import com.nantian.javachatclient.view.MainWindow;
public class ChatManager {
//将类单例化的步骤就是讲他的构造方法变成private
//ChatManager就会有唯一的实例
private ChatManager() {}
private static final ChatManager instance=new ChatManager();
public static ChatManager getChatManager() {
return instance;
}
//具体实现连接服务器的操作
public void connect(String ip) {
}
//"发送"按钮发送内容函数
public void send(String out) {
}
//界面中只能向chatManager发送数据,那么接收到数据后怎样放到界面上呢?
//需要对MainWindow有个引用,并为它实现setWindow方法
MainWindow window;
public void setWindow(MainWindow window) {
this.window=window;
//传递之后
window.appendText("文本框已经和ChatManager绑定了!");
}
}
(2).StartClient.java
package com.nantian.javachatclient.main;
import java.awt.EventQueue;
import com.nantian.javachatclient.view.MainWindow;
public class StartClient {
/**
* 将窗体主方法放到了这里
* @param args
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MainWindow frame = new MainWindow();
frame.setVisible(true);
//创建window的时候,对MainWindow有个引用
ChatManager.getChatManager().setWindow(frame);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
(3).MainWindow.java
new -->project-->web builder-->JFrame(需要下载web builder插件!)
package com.nantian.javachatclient.view;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import com.nantian.javachatclient.main.ChatManager;
import javax.swing.JTextArea;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class MainWindow extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private JPanel contentPane;
//文本控件声明到类中,让他作为类的变量
JTextArea txt;
private JTextField IP;
private JTextField send;
/**
* Create the frame.
*/
public MainWindow() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
txt = new JTextArea();
txt.setText("Ready...");
IP = new JTextField();
IP.setText("127.0.0.1");
IP.setColumns(10);
JButton button = new JButton("连接到服务器");
button.addMouseListener(new MouseAdapter() {
//mouse-->clicked自动创建的代码
@Override
public void mouseClicked(MouseEvent e) {
//为"连接到服务器"按钮添加动作
//使用函数把它和这个按钮的监听事件绑定起来
ChatManager.getChatManager().connect(IP.getText());
}
});
send = new JTextField();
send.setText("你好!");
send.setColumns(10);
JButton button_1 = new JButton("发送");
button_1.addMouseListener(new MouseAdapter() {
//为"发送"按钮添加动作
@Override
public void mouseClicked(MouseEvent e) {
//当前文本发送呢内容
ChatManager.getChatManager().send(send.getText());
appendText("我说:"+send.getText());
//发送出去后,需要将当前文本框清空
send.setText("");
}
});
GroupLayout gl_contentPane = new GroupLayout(contentPane);
gl_contentPane.setHorizontalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(Alignment.TRAILING, gl_contentPane.createSequentialGroup()
.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
.addComponent(txt, GroupLayout.DEFAULT_SIZE, 414, Short.MAX_VALUE)
.addGroup(Alignment.LEADING, gl_contentPane.createSequentialGroup()
.addComponent(IP)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(button))
.addGroup(gl_contentPane.createSequentialGroup()
.addComponent(send, GroupLayout.DEFAULT_SIZE, 295, Short.MAX_VALUE)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(button_1, GroupLayout.PREFERRED_SIZE, 129, GroupLayout.PREFERRED_SIZE)))
.addContainerGap())
);
gl_contentPane.setVerticalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
.addComponent(IP, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(button))
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(txt, GroupLayout.DEFAULT_SIZE, 193, Short.MAX_VALUE)
.addPreferredGap(ComponentPlacement.RELATED)
.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
.addComponent(send, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(button_1)))
);
contentPane.setLayout(gl_contentPane);
}
public void appendText(String in) {
txt.append("\n"+in);
}
}
操作截图:
=============================================================================================================
框架已搭建完成,聊天客户端的核心内容见:使用 Java 创建聊天客户端-2