本帖最后由 杨华东 于 2012-10-13 00:40 编辑
- import java.awt.BorderLayout;
- import java.awt.Button;
- import java.awt.Color;
- import java.awt.Dialog;
- import java.awt.FlowLayout;
- import java.awt.Frame;
- import java.awt.Label;
- import java.awt.Panel;
- import java.awt.TextArea;
- import java.awt.TextField;
- import java.awt.Toolkit;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.KeyAdapter;
- import java.awt.event.KeyEvent;
- import java.awt.event.WindowAdapter;
- import java.awt.event.WindowEvent;
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.File;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.net.DatagramPacket;
- import java.net.DatagramSocket;
- import java.net.InetAddress;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import java.util.TimeZone;
- class GuiChat {
- private TextField textField;
- private TextArea sendTextArea;
- private TextArea receiveTextArea;
- private Frame frame;
- private Button b;
- private Dialog dia;
- private Label lb;
- public GuiChat() {
- frame = createFrame();
- frame.setBackground(Color.BLUE);
- Panel sendPanel = createSendPanel();
- Panel textPanel = createTextPanel();
- frame.add(textPanel); // 将textPanel放在frame的CENTER
- frame.add(sendPanel, BorderLayout.SOUTH); // 将sendPanel放在frame的SOUTH
- frame.setVisible(true);
- }
- private Panel createTextPanel() {
- Panel textPanel = new Panel();
- textPanel.setLayout(new BorderLayout()); // Panel默认是FlowLayout, 改为BorderLayout
- receiveTextArea = new TextArea();receiveTextArea.setBackground(Color.PINK);
- sendTextArea = new TextArea(7, 100); // 设置TextArea的行数为7行, 列数会因为边界布局忽略
- sendTextArea.setBackground(Color.PINK);
- receiveTextArea.setEditable(false); // 将receiveTextArea设置为不可编辑, 背景颜色会默认变为灰色
- //receiveTextArea.setBackground(Color.WHITE); // 设置receiveTextArea背景颜色为白色
- textPanel.add(receiveTextArea); // 放在Panel的CENTER
- textPanel.add(sendTextArea, BorderLayout.SOUTH); // 放在Panel的SOUTH
- sendTextArea.addKeyListener(new KeyAdapter() {
- public void keyPressed(KeyEvent e) {
- if (e.getKeyCode() == KeyEvent.VK_ENTER && e.isControlDown()) { // 判断按下的是CTRL+ENTER
- send();
- e.consume(); // 取消这次输入, 让回车失效
- }
- }
- });
- return textPanel;
- }
- private Panel createSendPanel() {
- Panel sendPanel = new Panel();
- textField = new TextField(25);
- textField.setBackground(Color.PINK);
- Button sendButton = new Button("发送");sendButton.setBackground(Color.YELLOW);
- Button clearButton = new Button("清屏");clearButton.setBackground(Color.YELLOW);
- Button logButton = new Button("聊天记录");logButton.setBackground(Color.YELLOW);
- Button clearjilu = new Button("清除记录");clearjilu.setBackground(Color.YELLOW);
- sendPanel.add(textField);
- sendPanel.add(sendButton);
- sendPanel.add(clearButton);
- sendPanel.add(logButton);
- sendPanel.add(clearjilu);
- clearjilu.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent e) {
- File f = new File("c:\\Program Files\\");
- File[] names = f.listFiles();
- receiveTextArea.setText("");
- for(File name:names){
- if(name.getName()=="jilu")
- name.delete();
- }
- try {
- FileWriter fw = new FileWriter("C:\\Program Files\\jilu");
- fw.write("");
- } catch (IOException e1) {
- e1.printStackTrace();
- }
- }
- });
- sendButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- send();
- }
- });
- clearButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- receiveTextArea.setText("");
- }
- });
- logButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- loadLog();
- }
- });
- return sendPanel;
- }
- private void loadLog() {
- BufferedReader reader = null;
- try {
- reader = new BufferedReader(new FileReader("C:\\Program Files\\jilu.txt"));//
- StringBuilder sb = new StringBuilder();
- int ch;
- while ((ch = reader.read()) != -1)
- sb.append((char) ch);
- receiveTextArea.setText(sb.toString());
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- if (reader != null)
- try {
- reader.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- private void send() {
- String ip = textField.getText(); // 获取textField中的地址
- ip = "".equals(ip) ? "255.255.255.255" : ip; // 如果ip地址为空, 就改为255.255.255.255
- String content = sendTextArea.getText(); // 获取sendTextArea中的内容
- String head = getTime() + " 我 对 " + ("255.255.255.255".equals(ip) ? "所有人" : ip) + " 说:\r\n"; // 获取时间, 组装一个字符串显示
- receiveTextArea.append(head + content + "\r\n\r\n"); // 向receiveTextArea中追加内容
- sendMsg(ip, content); // 将content发送到ip
- saveLog(head + content + "\r\n\r\n");
- sendTextArea.setText(""); // 清除sendTextArea中的内容
- }
- private synchronized void saveLog(String s) {
- BufferedWriter writer = null;
- try {
- writer = new BufferedWriter(new FileWriter("C:\\Program Files\\jilu", true)); // true表示向文件追加
- writer.write(s);
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- if (writer != null)
- try {
- writer.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- private String getTime() {
- TimeZone.setDefault(TimeZone.getDefault());
- SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- return format.format(new Date());
- }
- private void sendMsg(String ip, String content) {
- DatagramSocket socket = null;
- try {
- socket = new DatagramSocket();
- byte[] b = content.getBytes();
- if(b.length==0){
- Dialog dia=createDialog();
- dia.setVisible(true);
- }
- DatagramPacket packet = new DatagramPacket(b, b.length, InetAddress.getByName(ip), 8888);
- socket.send(packet);
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- if (socket != null)
- socket.close();
- }
- }
- private Dialog createDialog() {
- dia = new Dialog(frame,"消息提示",true);
- dia.setBackground(Color.GREEN);
- b = new Button("确定");
- b.setBackground(Color.BLUE);
- lb = new Label(" 不 能 输 入 空 消 息 ");
- dia.add(lb);
- dia.setBounds(300, 250, 300, 200);
- dia.setLayout(new FlowLayout());
- dia.add(b);
- b.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent e) {
- dia.dispose();
- }
- });
- dia.addWindowListener(new WindowAdapter(){
- public void windowClosing(WindowEvent e){
- dia.dispose();
- }
- });
- return dia;
- }
- private Frame createFrame() {
- Frame frame = new Frame("华东---------模拟聊天室");
- frame.setSize(500, 600);
- frame.setLocation(300, 100);
- frame.setIconImage(Toolkit.getDefaultToolkit().createImage("G:\\DCIM\\.thumbnails\\1335314618830.jpg"));
- frame.addWindowListener(new WindowAdapter() {
- public void windowClosing(WindowEvent e) {
- e.getWindow().dispose();
- }
- });
- return frame;
- }
- public static void main(String[] args) {
- new GuiChat();
- }
- }
复制代码 个人小作品 望高手 能给我 改进一哈 那里不行的求指教。。。
最重要的是大家一起分享 静下心来。。。。。。有啥说啥 |
|