import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontFormatException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Random;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
//多人在线聊天室程序
public class chatman {//启动入口
public static void main(String[] args) {
MianBan mb = new MianBan();
}
}
class MianBan extends JFrame implements ActionListener {//程序功能面板
JLabel[] jLabels = new JLabel[7];
JTextArea jTextArea_mem, jTextArea_msg, jTextArea_send;
JButton btn_start, btn_send, btn_save, btn_config, btn_clear;
JTextField tfd_ip, tfd_port, tfd_ID, tfd_name;
JRadioButton btn_client, btn_server;
ButtonGroup btn_Group;
Socket socket;// 客户连接
Servers servers;// 服务器
Client client; //客户端
public MianBan() {// 初始布局,设计界面框架,添加标签,文本框,按钮
this.setSize(550, 640);
this.setLayout(null);
//设置标签
jLabels[0] = new JLabel("在线人员");
jLabels[0].setBounds(20, 0, 180, 40);
jLabels[1] = new JLabel("消息记录");
jLabels[1].setBounds(220, 0, 180, 40);
jLabels[2] = new JLabel("控制台");
jLabels[2].setBounds(560, 0, 180, 60);
jLabels[3] = new JLabel("IP地址:");
jLabels[3].setBounds(560, 200, 180, 60);
jLabels[4] = new JLabel("端口号:");
jLabels[4].setBounds(560, 280, 180, 60);
jLabels[5] = new JLabel("接收人ID:");
jLabels[5].setBounds(20, 400, 180, 40);
jLabels[6] = new JLabel("我的昵称:");
jLabels[6].setBounds(20, 480, 180, 40);
for (int i = 0; i < jLabels.length; i++) {
this.add(jLabels[i]);
jLabels[i].setFont(new Font("宋体", 0, 24));
}
tfd_ID = new JTextField("0");
tfd_ID.setBounds(20, 440, 180, 40);
tfd_ID.setFont(new Font("宋体", 0, 24));
this.add(tfd_ID);
tfd_name = new JTextField("路人" + "甲乙丙丁戊己庚辛壬癸".charAt(new Random().nextInt(10)));
tfd_name.setBounds(20, 520, 180, 40);
tfd_name.setFont(new Font("宋体", 0, 24));
this.add(tfd_name);
tfd_ip = new JTextField("127.0.0.1");
tfd_ip.setBounds(560, 250, 160, 40);
tfd_ip.setFont(new Font("宋体", 0, 24));
this.add(tfd_ip);
tfd_port = new JTextField("6666");
tfd_port.setBounds(560, 330, 160, 40);
tfd_port.setFont(new Font("宋体", 0, 24));
this.add(tfd_port);
//设置功能按钮
btn_Group = new ButtonGroup();
btn_client = new JRadioButton("客户端模式");
btn_client.setBounds(560, 60, 180, 60);
this.add(btn_client);
btn_Group.add(btn_client);
btn_client.setFont(new Font("宋体", 0, 24));
btn_server = new JRadioButton("服务端模式");
btn_server.setBounds(560, 120, 180, 60);
this.add(btn_server);
btn_Group.add(btn_server);
btn_server.setFont(new Font("宋体", 0, 24));
jTextArea_mem = new JTextArea();//设置在线成员列表
//jTextArea_mem.setBounds(60,60 ,180 ,360 ); this.add(jTextArea_mem);
//jTextArea_mem.setEnabled(false);
jTextArea_mem.setEditable(false);
jTextArea_mem.setLineWrap(true);
jTextArea_mem.setWrapStyleWord(true);
jTextArea_mem.setFont(new Font("宋体", 0, 18));
jTextArea_mem.setForeground(Color.BLUE);
jTextArea_msg = new JTextArea();//设置消息显示界面
// jTextArea_msg.setBounds(300, 60, 300,360 ); this.add(jTextArea_msg);
//jTextArea_msg.setEnabled(false);
jTextArea_msg.setEditable(false);
jTextArea_msg.setLineWrap(true);
jTextArea_msg.setWrapStyleWord(true);
jTextArea_msg.setFont(new Font("宋体", 0, 18));
jTextArea_msg.setForeground(Color.red);
jTextArea_send = new JTextArea();//设置输入消息界面
// jTextArea_send.setBounds(300, 450, 300, 120); this.add(jTextArea_send);
jTextArea_send.setLineWrap(true);
jTextArea_send.setWrapStyleWord(true);
jTextArea_send.setFont(new Font("宋体", 0, 18));
jTextArea_send.setForeground(Color.BLACK);
// jTextArea_send.setFocusable(true);
//设置功能按钮
btn_send = new JButton("发送消息");
btn_send.setBounds(220, 540, 160, 40);
btn_send.setFont(new Font("宋体", 0, 20));
this.add(btn_send);
btn_config = new JButton("控制");
btn_config.setBounds(420, 540, 100, 40);
btn_config.setFont(new Font("宋体", 0, 20));
this.add(btn_config);
btn_save = new JButton("保存记录");
btn_save.setBounds(560, 455, 130, 40);
btn_save.setFont(new Font("宋体", 0, 20));
this.add(btn_save);
btn_clear = new JButton("清屏");
btn_clear.setBounds(560, 510, 130, 40);
btn_clear.setFont(new Font("宋体", 0, 20));
this.add(btn_clear);
btn_send = new JButton("传送文件");
btn_send.setBounds(560, 565, 130, 40);
btn_send.setFont(new Font("宋体", 0, 20));
this.add(btn_send);
btn_start = new JButton("启动服务");
btn_start.setBounds(560, 400, 130, 40);
btn_start.setFont(new Font("宋体", 0, 20));
this.add(btn_start);
JPanel panelOutput1, panelOutput2, panelOutput3;
panelOutput1 = new JPanel();//设计成员在线列表
panelOutput1.setLayout(new BorderLayout());
panelOutput1.add(new JScrollPane(jTextArea_mem));
panelOutput1.setBounds(20, 40, 180, 360);
panelOutput2 = new JPanel();//设计聊天窗口
panelOutput2.setLayout(new BorderLayout());
panelOutput2.add(new JScrollPane(jTextArea_msg));
panelOutput2.setBounds(220, 40, 300, 360);
panelOutput3 = new JPanel();//设计输入窗口
panelOutput3.setLayout(new BorderLayout());
panelOutput3.add(new JScrollPane(jTextArea_send));
panelOutput3.setBounds(220, 410, 300, 120);
this.add(panelOutput1);//添加到面板
this.add(panelOutput2);
this.add(panelOutput3);
//向组件增加监听器
btn_client.setSelected(true);
btn_clear.addActionListener(this);
btn_client.addActionListener(this);
btn_config.addActionListener(this);
btn_save.addActionListener(this);
btn_send.addActionListener(this);
btn_server.addActionListener(this);
btn_start.addActionListener(this);
this.setTitle("无连接!");
this.setResizable(false);// 禁止修改窗口大小
java.awt.Dimension pm = java.awt.Toolkit.getDefaultToolkit().getScreenSize();// 获取屏幕信息
setLocation(pm.width / 2 - getWidth() / 2, pm.height / 2 - getHeight() / 2);// 设置窗口居中
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public String getTime(String format) { //按一定得格式获取当前的时间
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new Simpl
评论0