一、登录
import lombok.SneakyThrows;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
* 登录页面
*/
public class LoginFrame extends JFrame {
private JPanel contentPane;
private JTextField textField;
private JPasswordField passwordField;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
LoginFrame frame = new LoginFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public LoginFrame() throws Exception{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 400, 600);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblNewLabel = new JLabel("登录页面");
lblNewLabel.setFont(new Font("微软雅黑", Font.PLAIN, 26));
lblNewLabel.setBounds(120, 63, 482, 83);
contentPane.add(lblNewLabel);
JLabel lblNewLabel_1 = new JLabel("账户:");
lblNewLabel_1.setFont(new Font("微软雅黑", Font.PLAIN, 20));
lblNewLabel_1.setBounds(50, 163, 130, 49);
contentPane.add(lblNewLabel_1);
textField = new JTextField();
textField.setBounds(150, 175, 150, 33);
contentPane.add(textField);
textField.setColumns(10);
JLabel lblNewLabel_1_1 = new JLabel("密码:");
lblNewLabel_1_1.setFont(new Font("微软雅黑", Font.PLAIN, 20));
lblNewLabel_1_1.setBounds(50, 264, 130, 49);
contentPane.add(lblNewLabel_1_1);
passwordField = new JPasswordField();
passwordField.setBounds(150, 264, 150, 39);
contentPane.add(passwordField);
JButton btnNewButton = new JButton("登录");
btnNewButton.addActionListener(new ActionListener() {
@SneakyThrows
public void actionPerformed(ActionEvent e) {
//将信息传给controller
new Friend();
return;
}
});
btnNewButton.setFont(new Font("微软雅黑", Font.PLAIN, 20));
btnNewButton.setBounds(105, 441, 118, 49);
contentPane.add(btnNewButton);
JButton btnNewButton_2 = new JButton("退出");
btnNewButton_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
setVisible(false);
dispose();
return;
}
});
btnNewButton_2.setFont(new Font("微软雅黑", Font.PLAIN, 20));
btnNewButton_2.setBounds(613, 441, 118, 49);
contentPane.add(btnNewButton_2);
setVisible(true);
}
}
二、好友列表
import lombok.SneakyThrows;
import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.UnknownHostException;
public class Friend extends JFrame implements ActionListener {
public static void main(String[] args) throws IOException {
new Friend();
}
public Friend() throws IOException {
super();
init();
setVisible(true);
}
JButton button1 = new JButton("首页");
JButton button2 = new JButton("理财");
JButton button3 = new JButton("生活");
JButton button4 = new JButton("消息");
JButton button5 = new JButton("我的");
JTextArea text1 = new JTextArea(10, 4);
JTextArea text2 = new JTextArea(2, 1);
JPanel jp1 = new JPanel();
JScrollPane jp2 = new JScrollPane();
JPanel jp3 = new JPanel();
JPanel jp4 = new JPanel();
JScrollPane jp5 = new JScrollPane(text2);
JScrollPane jp6 = new JScrollPane(text1);
private void init() throws UnknownHostException, IOException {
setTitle("好友列表");
setSize(400, 600);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
DefaultListModel<String> dl = new DefaultListModel<String>();//用来修改JList
dl.add(0, "卢俊义");
dl.add(1, "宋江");
dl.add(2, "林冲");
dl.add(3, "鲁智深");
dl.add(4, "吴用");
dl.add(5, "扈三娘");
dl.add(6, "李逵");
dl.add(7, "武松");
dl.add(8, "阮小二");
JList jl = new JList(dl);
jl.setFixedCellWidth(400);
jl.setFixedCellHeight(35);
jl.setBorder(new TitledBorder("好友列表"));
jp2.setViewportView(jl);
jp3.setLayout(new BorderLayout());
jp4.add(button1);
jp4.add(button2);
jp4.add(button3);
jp4.add(button4);
jp4.add(button5);
button4.addActionListener(new ActionListener() {
@SneakyThrows
@Override
public void actionPerformed(ActionEvent e) {
new Message();
}
});
jp3.add(jp4, BorderLayout.SOUTH);
//整个布局
add(jp1, BorderLayout.CENTER);
add(jp2, BorderLayout.WEST);
add(jp3, BorderLayout.SOUTH);
button1.addActionListener(this);
button2.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
String button = e.getActionCommand();
if (button == "消息") {
text2.setText("");
}
if (button == "发送") {
}
}
}
三、聊天
package com.io.chat;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
public class Message extends JFrame implements ActionListener {
public Message() throws Exception {
super();
init();
setVisible(true);
this.initServer();
}
public void initServer() throws IOException {
ServerSocket serverSocket = new ServerSocket(7002);
while (true) {
Socket socket = serverSocket.accept();
BufferedReader reader;
BufferedWriter writer;
try {
reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));//读取客户端消息
writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));//向客户端写消息
String lineString = "";
while (!(lineString = reader.readLine()).equals("bye")) {
System.out.println("收到来自客户端的发送的消息是7002:" + lineString);
text1.append(lineString + "\n");
writer.write("7002:" + lineString + "\n");
writer.flush();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) throws Exception {
new Message();
}
Socket socket1;
JLabel label = new JLabel("连接到127.0.0.1:9999端口!");
JButton button1 = new JButton("发送");
JButton button2 = new JButton("清空");
JTextArea text1 = new JTextArea(10, 4);
JTextArea text2 = new JTextArea(2, 1);
JPanel jp1 = new JPanel();
JPanel jp3 = new JPanel();
JPanel jp4 = new JPanel();
JScrollPane jp5 = new JScrollPane(text2);
JScrollPane jp6 = new JScrollPane(text1);
private void init() throws UnknownHostException, IOException {
setTitle("7002聊天室");
setSize(400, 600);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
jp6.setBorder(BorderFactory.createTitledBorder("聊天记录"));
text1.setEditable(false);
jp1.setLayout(new BorderLayout());
label.setHorizontalAlignment(JLabel.RIGHT);
jp1.add(label, BorderLayout.NORTH);
jp1.add(jp6, BorderLayout.CENTER);
jp5.setBorder(BorderFactory.createTitledBorder("请输入文字"));
jp3.setLayout(new BorderLayout());
jp4.add(button1);
jp4.add(button2);
jp3.add(jp5, BorderLayout.NORTH);
jp3.add(jp4, BorderLayout.SOUTH);
add(jp1, BorderLayout.CENTER);
add(jp3, BorderLayout.SOUTH);
button1.addActionListener(this);
button2.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
String button = e.getActionCommand();
if (button == "清空") {
text2.setText("");
}
if (button == "发送") {
String mess = text2.getText();
try {
text1.append(mess + "\n");
sendmess(mess);
text2.setText("");
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
class thread extends Thread {
Socket socket;
public thread(Socket socket) {
this.socket = socket;
}
public void run() {
while (true) {
try {
BufferedReader br = null;
br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String msg;
msg = br.readLine();//读取服务器是否发送了消息给该客户端
if (msg != null) {
text1.append(msg + "\n");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
private void sendmess(String mess) throws IOException {
socket1 = new Socket("127.0.0.1", 7001);
PrintStream ps = new PrintStream(socket1.getOutputStream());
ps.println(mess);
ps.flush();
new thread(socket1).start();
}
}
四、服务端
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.io.PrintStream;
public class Server {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(7002);
while (true) {
Socket socket = serverSocket.accept();
BufferedReader reader;
BufferedWriter writer;
try {
reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));//读取客户端消息
writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));//向客户端写消息
String lineString = "";
while (!(lineString = reader.readLine()).equals("bye")) {
System.out.println("收到来自客户端的发送的消息是7002:" + lineString);
text1.append(lineString + "\n");
writer.write("7002:" + lineString + "\n");
writer.flush();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}