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版
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.