客戶端 UI
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import com.angelfate.net.ChatClientNet;
public class ChatClientUI extends JFrame{
private ImageIcon chatLogo = null;
private int width =850,height = 840;
private JTextArea showMsgField = null,inputMsgField = null;
private JButton sendMsgBtn = null;
private ImageIcon showUserImage = null;
private JLabel showUserImageLabel = null,showUserInfoLabel = null;
private String username = null;
private JPanel inputMsgPanel = null,showMsgPanel = null;
private ChatClientNet clientNet = null;
public ChatClientUI(String username) {
this.username = username;
initUI();
addComponent();
addLIstener();
showUI();
clientNet = new ChatClientNet(showMsgField);
}
public void initUI() {
showMsgField = new JTextArea(30,32);
showMsgField.setEditable(false);
showMsgField.setBackground(Color.LIGHT_GRAY);
showMsgField.setWrapStyleWord(true);
inputMsgField = new JTextArea(5,32);
inputMsgField.setLineWrap(true);
inputMsgField.setWrapStyleWord(true);
inputMsgField.setAutoscrolls(true);
inputMsgPanel = new JPanel();
showMsgPanel = new JPanel();
sendMsgBtn = new JButton("发送消息");
showUserImage = new ImageIcon(this.getClass().getResource("/images/chatlogo.gif"));
showUserImageLabel = new JLabel(showUserImage,JLabel.CENTER);
showUserInfoLabel = new JLabel("欢迎你:"+this.username,JLabel.CENTER);
showUserInfoLabel.setFont(new Font("宋体",Font.BOLD,16));
}
public void addComponent() {
showMsgPanel.add(new JScrollPane(showMsgField));
this.add(showMsgPanel);
showMsgPanel.setBounds(5,3,width-300,height-250);
inputMsgPanel.add(new JScrollPane(inputMsgField));
this.add(inputMsgPanel);
inputMsgPanel.setBounds(5,height-230,width-300,100);
this.add(sendMsgBtn);
sendMsgBtn.setBounds(515,height-120, 100, 35);
this.add(showUserImageLabel);
showUserImageLabel.setBounds(width-250,100,170,435);
this.add(showUserInfoLabel);
showUserInfoLabel.setBounds(width-300,550, 200, 35);
showUserInfoLabel.setForeground(new Color(0,176,101));
}
public void addLIstener() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
sendMsgBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String msg = inputMsgField.getText();
if(msg==null || msg.trim().length()<1) {
JOptionPane.showMessageDialog(null, "消息不能为空");
}else {
clientNet.sendMsgToClient(msg);
inputMsgField.setText("");
}
}
});
}
public void showUI() {
this.setTitle("客户端 聊天窗口 --- 欢迎你:"+this.username);
this.setIconImage(new ImageIcon(this.getClass().getResource("/images/chatlogo2.png")).getImage());
int screenWidth = (int)this.getToolkit().getScreenSize().getWidth();
int screenHeight = (int)this.getToolkit().getScreenSize().getHeight();
int x = (screenWidth-width)/2; 坐标
int y = (screenHeight-height)/2;
this.setLocation(x, y);
this.setSize(width, height);
this.setVisible(true);
this.setResizable(false);
}
public static void main(String[] args) {
new ChatClientUI("");
}
}
用户信息处理
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class FileUtils {
public static String getValue(String key) {
Properties p = new Properties();
try {
InputStream readDB = FileUtils.class.getClassLoader().getResourceAsStream("db.properties");
p.load(readDB);
return p.getProperty(key, null);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static boolean checkLogin(String uname,String pwd) {
String username = getValue("username");
String password = getValue("pwd");
if(username.equals(uname) && password.equals(pwd)) {
return true;
}
return false;
}
}
客户端网络通讯
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Date;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
public class ChatClientNet {
private PrintStream ps = null;
private BufferedReader br = null;
private JTextArea showMsgField = null;
public ChatClientNet(JTextArea showMsgField) {
this.showMsgField = showMsgField;
initNet();
getMessage();
}
private void initNet() {
try {
Socket socket = new Socket("127.0.0.1",8000);
OutputStream os = socket.getOutputStream();
br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
ps = new PrintStream(os);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void sendMsgToClient( String msg) {
ps.println(msg);
showMsgField.append("我发送的消息是:"+msg+"\r\n");
}
public void getMessage() {
new Thread(new Runnable() {
@Override
public void run() {
while(true) {
try {
String getMsg = br.readLine();
System.out.println("收到服务器端的消息:\r\n"+getMsg+"\r\n");
showMsgField.append("收到服务器端的消息:"+new Date().toLocaleString()+"\r\n"+getMsg+"\r\n");
} catch (IOException e) {
e.printStackTrace();
}
}
}
}).start();
}
public static void main(String[] args) {
}
}
突然想起来以前上微机课的时候用什么本地的软件进行局域网聊天来着。。