package rgy.com.TT;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JColorChooser;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRootPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JToolBar;
import javax.swing.WindowConstants;
public class Client extends JFrame {
// 添加属性
private JPanel panel_west = new JPanel();
private JPanel panel_south = new JPanel();
private JPanel panel_main = new JPanel();
private JTextArea ta_show = new JTextArea();
private JScrollPane sp_show = new JScrollPane(ta_show);
private JTextArea ta_input = new JTextArea();
private JScrollPane sp_input = new JScrollPane(ta_input);
private JToolBar toolbar = new JToolBar(); // 创建工具栏
private JLabel imagelable;
private JButton button_link = new JButton("建立连接");
private JButton button_linkconfirm = new JButton("连 接");
private JLabel label_wordstyle = new JLabel("字号:");
private JCheckBox checkb_bold = new JCheckBox("粗体");
private JCheckBox checkb_italic = new JCheckBox("斜体");
private JLabel label_wordsize = new JLabel("字号:");
private JButton button_larger = new JButton("A+");
private JButton button_smaller = new JButton("A-");
private JButton button_color = new JButton("颜色");
private JButton button_send = new JButton("消息发送");
// 面板上的属性
private JTextField tf_search;
private String str_IP;
private MessageBox_link messageBox_link;
private MessageBox_interupt messageBox_interupt;
//
private Socket socket;
private PrintWriter out;
private BufferedReader in;
//
private Timer timer;
//
public Client(String str) {
super(str);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setBounds(500, 220, 480, 400);
// 添加功能代码
ImageIcon imageIcon = new ImageIcon("img/head.png");
imagelable = new JLabel("", imageIcon, JLabel.CENTER);
this.add(panel_west, BorderLayout.WEST);
panel_west.setLayout(new GridLayout(2, 1));
panel_west.add(imagelable);
panel_west.add(button_link);
this.add(panel_main);
panel_main.setLayout(new GridLayout(2, 1));
panel_main.add(sp_show);
ta_show.setEditable(false);
ta_show.setLineWrap(true);// 换行
ta_show.setFont(new Font("楷体", 0, 20));
panel_main.add(panel_south);
panel_south.setLayout(new BorderLayout());
panel_south.add(toolbar, BorderLayout.NORTH);
toolbar.add(label_wordstyle);
toolbar.add(checkb_bold);
toolbar.add(checkb_italic);
toolbar.add(label_wordsize);
toolbar.add(button_larger);
toolbar.add(button_smaller);
toolbar.add(button_color);
toolbar.add(button_send);
panel_south.add(sp_input);
ta_input.setLineWrap(true);// 换行
ta_input.setFont(new Font("楷体", 0, 20));
//
this.setUndecorated(true); // 去掉窗口的装饰
this.getRootPane().setWindowDecorationStyle(JRootPane.FRAME);
this.setVisible(false);
this.pack();
// messageBox_interupt.setVisible(false);
////////////////////////添加功能
button_link.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
messageBox_link=new MessageBox_link();
}
});
//
checkb_bold.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Font font = ta_input.getFont(); // 获得文本区的当前字体对象
String name = font.getName();
int style = font.getStyle();
int size = font.getSize();
style = style ^ 1;
ta_input.setFont(new Font(name, style, size));
// ta_show.setFont(new Font(name, style, size));
}
});
checkb_italic.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Font font = ta_input.getFont(); // 获得文本区的当前字体对象
String name = font.getName();
int style = font.getStyle();
int size = font.getSize();
style = style ^ 2;
ta_input.setFont(new Font(name, style, size));
// ta_show.setFont(new Font(name, style, size));
}
});
// ///字号大小
button_larger.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Font font = ta_input.getFont(); // 获得文本区的当前字体对象
String name = font.getName();
int style = font.getStyle();
int size = font.getSize() + 5;
ta_input.setFont(new Font(name, style, size));
// ta_show.setFont(new Font(name, style, size));
}
});
button_smaller.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Font font = ta_input.getFont(); // 获得文本区的当前字体对象
String name = font.getName();
int style = font.getStyle();
int size = font.getSize() - 5;
ta_input.setFont(new Font(name, style, size));
// ta_show.setFont(new Font(name, style, size));
}
});
// ///设置颜色
button_color.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Color color;
color = JColorChooser.showDialog(Client.this, "颜色选择",
Color.black);
ta_input.setForeground(color);// 设置颜色
// ta_show.setForeground(color);
}
});
// 通信
// 发送
button_send.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
out.println(ta_input.getText());
out.flush();
ta_show.append("客户端说:"+ta_input.getText()+"\n");
ta_input.setText("");
}
});
//发送消息的热键
ta_input.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent ke){
if(ke.isControlDown()&&ke.getKeyCode()==KeyEvent.VK_ENTER){
out.println(ta_input.getText());
out.flush();
ta_show.append("客户端说:"+ta_input.getText()+"\n");
ta_input.setText("");
}
}
});
//
messageBox_link=new MessageBox_link();
button_linkconfirm.addActionListener(new ActionListener() {//连接
public void actionPerformed(ActionEvent e) {
str_IP = tf_search.getText();
try {
socket = new Socket(str_IP, 5001);
out = new PrintWriter(socket.getOutputStream());
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
ta_show.setText("双方连接已建立!\n");
button_link.setEnabled(false);
//接收消息
timer = new Timer();//定时器,刷新接收消息
timer.schedule(new MyTimerTask_receive(), 100, 100);
//
} catch (Exception ev) {
ta_show.setText("连接建立失败!\n");
}
Client.this.setVisible(true);
Client.this.validate();//更新
messageBox_link.dispose();
}
});
//
}
// /////////////////////////////////////////////////////
private class MessageBox_link extends JDialog {
private JLabel lable_tip;
private JPanel panel_link = new JPanel();
private JPanel panel_search = new JPanel();
private JPanel panel_tip = new JPanel();
public MessageBox_link() {
super(Client.this, "连接提示");
this.setSize(300, 170);
this.setLocation(Client.this.getX() + 120, Client.this.getY() + 120);
this.setLayout(new GridLayout(3, 1));
//
ImageIcon imageIcon = new ImageIcon("img/search.png");
lable_tip = new JLabel("请输入对方的IP地址:", imageIcon, JLabel.LEFT);
lable_tip.setFont(new Font("楷体", 1, 20));
panel_tip.add(lable_tip);
this.add(panel_tip);
tf_search = new JTextField(20);
tf_search.setFont(new Font("楷体", 1, 25));
tf_search.setHorizontalAlignment(JTextField.CENTER);
panel_search.add(tf_search);
this.add(panel_search);
this.add(panel_link);
panel_link.add(button_linkconfirm);
button_linkconfirm.setFont(new Font("楷体", 1, 20));
//
this.setUndecorated(true);//去边框
this.setVisible(true);
this.pack();
}
}
//
private class MessageBox_interupt extends JDialog {
private JLabel lable_tip;
private JButton button_ok;
private JPanel panel_tip = new JPanel();
private JPanel panel_ok = new JPanel();
public MessageBox_interupt() {
super(Client.this, "中断提示");
this.setSize(150,60);
this.setLocation(Client.this.getX() + 60, Client.this.getY() + 120);
this.setLayout(new GridLayout(2, 1));
//
ImageIcon imageIcon = new ImageIcon("img/tip.png");
lable_tip = new JLabel("请注意:与服务端的连接已中断!", imageIcon, JLabel.LEFT);
lable_tip.setFont(new Font("楷体", 1, 17));
panel_tip.add(lable_tip);
this.add(panel_tip);
button_ok = new JButton("知道了");
button_ok.setFont(new Font("楷体", 1, 20));
panel_ok.add(button_ok);
this.add(panel_ok);
//
this.setUndecorated(true);//去边框
this.setVisible(true);
this.pack();
//
button_ok.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
MessageBox_interupt.this.dispose();
}
});
}
}
//
class MyTimerTask_receive extends TimerTask {
public void run() {
try {
ta_show.append("服务端说:"+in.readLine()+"\n");
} catch (Exception e) {
// JOptionPane.showMessageDialog(Client.this,"请注意:与服务端的连接已中断!!");
messageBox_interupt=new MessageBox_interupt();
ta_show.append("--------与服务端的连接已中断-------");
try{
socket.close();
out.close();
in.close();
}catch(Exception ev){
ta_show.append("————连接未正常中断!");
}
button_link.setEnabled(true);
timer.cancel();
}
}
}
public static void main(String args[]) {
new Client("客户端");
}
}
package rgy.com.TT;
import java.awt.*;
import java.awt.event.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.*;
public class Server extends JFrame {
// 添加属性
private JPanel panel_south = new JPanel();
private JPanel panel_main = new JPanel();
private JTextArea ta_show = new JTextArea();
private JScrollPane sp_show = new JScrollPane(ta_show);
private JTextArea ta_input = new JTextArea();
private JScrollPane sp_input = new JScrollPane(ta_input);
private JToolBar toolbar = new JToolBar(); // 创建工具栏
private JLabel label_wordstyle = new JLabel("字号:");
private JCheckBox checkb_bold = new JCheckBox("粗体");
private JCheckBox checkb_italic = new JCheckBox("斜体");
private JLabel label_wordsize = new JLabel("字号:");
private JButton button_larger = new JButton("A+");
private JButton button_smaller = new JButton("A-");
private JButton button_color = new JButton("颜色");
private JButton button_send = new JButton("消息发送");
private JButton button_openport = new JButton("重新打开端口");
//
private ServerSocket server;
private Socket socket;
private PrintWriter out;
private BufferedReader in;
//
private MessageBox messageBox_openport;
private MessageBox messageBox_interupt;
private MessageBox messageBox_waitlink;
//
private Timer timer;
//
public Server(String str) {
super(str);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setBounds(70, 10, 480, 400);
// 添加功能代码
this.add(panel_main);
panel_main.setLayout(new GridLayout(2, 1));
panel_main.add(sp_show);
ta_show.setEditable(false);
ta_show.setLineWrap(true);// 换行
ta_show.setFont(new Font("楷体", 0, 20));
panel_main.add(panel_south);
panel_south.setLayout(new BorderLayout());
panel_south.add(toolbar, BorderLayout.NORTH); // 工具栏添加到窗格北部
toolbar.add(label_wordstyle);
toolbar.add(checkb_bold);
toolbar.add(checkb_italic);
toolbar.add(label_wordsize);
toolbar.add(button_larger);
toolbar.add(button_smaller);
toolbar.add(button_color);
toolbar.add(button_send);
toolbar.add(button_openport);
panel_south.add(sp_input);
ta_input.setLineWrap(true);// 换行
ta_input.setFont(new Font("楷体", 0, 20));
//
this.setUndecorated(true); // 去掉窗口的装饰
// 设置为简单对话框风格
// this.getRootPane().setWindowDecorationStyle(JRootPane.COLOR_CHOOSER_DIALOG);
// this.getRootPane().setWindowDecorationStyle(JRootPane.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
this.getRootPane().setWindowDecorationStyle(JRootPane.FRAME);
this.setVisible(true);
////////////////////////添加功能
/////调节字体
checkb_bold.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Font font = ta_input.getFont(); // 获得文本区的当前字体对象
String name = font.getName();
int style = font.getStyle();
int size = font.getSize();
style = style ^ 1;
ta_input.setFont(new Font(name, style, size));
// ta_show.setFont(new Font(name, style, size));
}
});
checkb_italic.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Font font = ta_input.getFont(); // 获得文本区的当前字体对象
String name = font.getName();
int style = font.getStyle();
int size = font.getSize();
style = style ^ 2;
ta_input.setFont(new Font(name, style, size));
// ta_show.setFont(new Font(name, style, size));
}
});
// ///字号大小
button_larger.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Font font = ta_input.getFont(); // 获得文本区的当前字体对象
String name = font.getName();
int style = font.getStyle();
int size = font.getSize() + 5;
ta_input.setFont(new Font(name, style, size));
// ta_show.setFont(new Font(name, style, size));
}
});
button_smaller.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Font font = ta_input.getFont(); // 获得文本区的当前字体对象
String name = font.getName();
int style = font.getStyle();
int size = font.getSize() - 5;
ta_input.setFont(new Font(name, style, size));
// ta_show.setFont(new Font(name, style, size));
}
});
// ///设置颜色
button_color.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Color color;
color = JColorChooser.showDialog(Server.this, "颜色选择",Color.black);
ta_input.setForeground(color);// 设置颜色
// ta_show.setForeground(color);
}
});
// 通信
try {
server=new ServerSocket(5001);
ta_show.setText("端口已打开,等待客户端访问……"+"\n");
messageBox_openport=new MessageBox("端口已打开,等待客户端访问……");
socket=server.accept();
ta_show.append("客户端已接入,双方连接已建立!"+"\n");
out = new PrintWriter(socket.getOutputStream());
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
////接收
timer = new Timer();
timer.schedule(new MyTimerTask(), 100, 100);
} catch (Exception e) {
messageBox_openport=new MessageBox("请注意:端口未顺利打开!");
}
//发送
button_send.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
out.println(ta_input.getText());
out.flush();
ta_show.append("服务端说:"+ta_input.getText()+"\n");
ta_input.setText("");
}
});
//发送消息的热键
ta_input.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent ke){
if(ke.isControlDown()&&ke.getKeyCode()==KeyEvent.VK_ENTER){
out.println(ta_input.getText());
out.flush();
ta_show.append("服务端说:"+ta_input.getText()+"\n");
ta_input.setText("");
}
}
});
//
button_openport.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try{
server=new ServerSocket(5001);
ta_show.setText("端口已打开,等待客户端访问……"+"\n");
messageBox_waitlink=new MessageBox("端口已打开,等待客户端访问……");
socket=server.accept();
ta_show.append("客户端已接入,双方连接已建立!"+"\n");
out = new PrintWriter(socket.getOutputStream());
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
////接收
timer = new Timer();
timer.schedule(new MyTimerTask(), 100, 100);
}catch(Exception ev){
// JOptionPane.showMessageDialog(Server.this,"请注意:端口未顺利打开!!");
messageBox_openport=new MessageBox("请注意:端口未顺利打开!");
}
}
});
//
}
// ///////////////////////////////////////////////////
private class MessageBox extends JDialog {
private JLabel lable_tip;
private JButton button;
private JPanel panel_tip = new JPanel();
private JPanel panel_ok = new JPanel();
public MessageBox(String str_tip) {
super(Server.this);
this.setSize(150,60);
this.setLocation(Server.this.getX() + 55, Server.this.getY() + 120);
this.setLayout(new GridLayout(2, 1));
//
ImageIcon imageIcon = new ImageIcon("img/tip.png");
lable_tip = new JLabel(str_tip, imageIcon, JLabel.LEFT);
lable_tip.setFont(new Font("楷体", 1, 17));
panel_tip.add(lable_tip);
this.add(panel_tip);
button = new JButton("知道了");
button.setFont(new Font("楷体", 1, 20));
panel_ok.add(button);
this.add(panel_ok);
//
this.setUndecorated(true);//去边框
this.setVisible(true);
this.pack();
//
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
MessageBox.this.dispose();
}
});
}
}
//
class MyTimerTask extends TimerTask {
public void run() {
try {
ta_show.append("客户端说:" + in.readLine() + "\n");
} catch (Exception e) {
ta_show.append("-----------与客户端的连接已中断!-----------\n");
try {
server.close();
socket.close();
out.close();
in.close();
} catch (Exception ev) {
ta_show.append("————服务关闭异常!");
}
ta_show.append("--------------通信端口已关闭!--------------");
messageBox_interupt=new MessageBox("请注意:与客户端的连接已断开!");
timer.cancel();
}
}
}
public static void main(String args[]) {
new Server("服务端");
}
}
TT聊天软件v1.0版
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.