[Java] java在线聊天及文件传输系统5 →→→→→进入此内容的聊天室

来自 , 2019-03-26, 写在 Java, 查看 140 次.
URL http://www.code666.cn/view/69a5b599
  1. package MyFrame;
  2.  
  3. import java.awt.BorderLayout;
  4. import java.awt.Color;
  5. import java.awt.Font;
  6. import java.awt.event.ActionEvent;
  7. import java.awt.event.ActionListener;
  8. import java.io.FileFilter;
  9. import java.io.FileInputStream;
  10. import java.io.IOException;
  11. import java.io.InputStream;
  12. import java.io.ObjectInputStream;
  13. import java.io.ObjectOutputStream;
  14. import java.io.OutputStream;
  15. import java.net.InetAddress;
  16. import java.net.ServerSocket;
  17. import java.net.Socket;
  18. import java.net.UnknownHostException;
  19. import java.text.SimpleDateFormat;
  20. import java.util.ArrayList;
  21. import java.util.Date;
  22. import java.util.List;
  23.  
  24. import javax.swing.BorderFactory;
  25. import javax.swing.JButton;
  26. import javax.swing.JComboBox;
  27. import javax.swing.JFileChooser;
  28. import javax.swing.JOptionPane;
  29. import javax.swing.JPanel;
  30. import javax.swing.JScrollPane;
  31. import javax.swing.JTextArea;
  32. import javax.swing.JTextField;
  33.  
  34. import FileTran.FileClient;
  35.  
  36. public class MyPane extends JPanel {
  37.         // 默认服务器端口
  38.         private final String P = "9527";
  39.         // ////////////////////////////////
  40.         private JTextField msgjt;
  41.         private JComboBox ipjt;
  42.         private JTextField portjt;
  43.         private JButton clearjb;
  44.         private JButton sendjb;
  45.         private JButton exitjb;
  46.         private JButton filejb;
  47.         private JTextArea contendjt;
  48.         private JScrollPane scrolljs;
  49.         private Myframe mf;
  50.         private int n = 0;
  51.         private String fport = "9527";
  52.  
  53.         public MyPane(Myframe mf) {
  54.                 this.mf = mf;
  55.                 this.setBackground(Color.lightGray);
  56.                 // this.setBounds(0, 0, 600, 400);
  57.                 this.addContend();
  58.         }
  59.  
  60.         private void addContend() {
  61.                 // TODO Auto-generated method stub
  62.                 this.setLayout(null);
  63.                 // 端口
  64.                 this.portjt = new JTextField();
  65.                 this.portjt.setBounds(520, 320, 70, 30);
  66.                 this.portjt.setForeground(Color.RED);
  67.                 this.portjt.setFont(new Font("宋体", Font.PLAIN, 25));
  68.                 this.portjt.setText("9527");
  69.                 this.add(this.portjt);
  70.  
  71.                 // 输入框
  72.                 this.msgjt = new JTextField();
  73.                 this.msgjt.setBounds(10, 320, 250, 30);
  74.                 this.msgjt.setFont(new Font("宋体", Font.PLAIN, 20));
  75.                 this.msgjt.setForeground(Color.BLUE);
  76.                 this.add(this.msgjt);
  77.  
  78.                 // 文件发送按钮
  79.                 this.filejb = new JButton("文件发送");
  80.                 this.filejb.setBounds(505, 200, 90, 30);
  81.                 this.add(this.filejb);
  82.                 this.filejb.addActionListener(new ActionListener() {
  83.  
  84.                         @Override
  85.                         public void actionPerformed(ActionEvent e) {
  86.                                 new FileClient(mf).start();
  87.  
  88.                         }
  89.                 });
  90.  
  91.                 // 发送按钮
  92.                 this.sendjb = new JButton("发送");
  93.                 this.sendjb.setBounds(270, 320, 60, 30);
  94.                 this.add(this.sendjb);
  95.                 this.sendjb.addActionListener(new ActionListener() {
  96.  
  97.                         @Override
  98.                         public void actionPerformed(ActionEvent arg0) {
  99.                                 // TODO Auto-generated method stub
  100.                                 String msg = mf.getPane().getMsgjt().getText();
  101.                                 String port = mf.getPane().getPortjt().getText();
  102.                                 String nowip = (String) mf.getPane().getIpjt()
  103.                                                 .getSelectedItem();
  104.                                 if (msg.equals("")) {
  105.                                         JOptionPane.showMessageDialog(mf, "内容不能为空!");
  106.                                         // 目标端口是否改变
  107.                                         if (!(port.equals(fport))) {
  108.                                                 JOptionPane.showMessageDialog(mf, "正在向服务器发送端口...");
  109.                                                 new Client("客户端&端口改为:" + port, P, mf.getMyip());
  110.                                                 fport = mf.getPane().getPortjt().getText();
  111.  
  112.                                         }
  113.                                         // 获取目标电脑IP
  114.                                         else if (n == 0) {
  115.                                                 String myip = null;
  116.                                                 InetAddress myComputer = null;
  117.                                                 try {
  118.                                                         myComputer = InetAddress.getLocalHost(); // 取得主机位置对象
  119.                                                         myip = myComputer.getHostAddress();
  120.                                                         // this.getPane().getContendjt().setText("目标用户IP:"+this.ip);
  121.                                                         // port=mf.getPane().getPortjt().getText();
  122.                                                         JOptionPane
  123.                                                                         .showMessageDialog(mf, "正在向服务器发送测试消息...");
  124.                                                         new Client("客户端&端口:" + port + "   (测试 )", P, mf
  125.                                                                         .getMyip());
  126.                                                         fport = mf.getPane().getPortjt().getText();
  127.                                                         n = 1;
  128.                                                         return;
  129.                                                 } catch (UnknownHostException e) {
  130.                                                         myip = "暂时无法获取该用户IP";
  131.                                                         // String port=mf.getPane().getPortjt().getText();
  132.                                                         new Client(myip + "端口为 :" + port, port, mf
  133.                                                                         .getMyip());
  134.  
  135.                                                         fport = mf.getPane().getPortjt().getText();
  136.                                                 }
  137.                                                 n = 1;
  138.  
  139.                                         }
  140.                                         return;
  141.                                 } else if (!nowip
  142.                                                 .matches("([0-9]{1,2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])[.]"
  143.                                                                 + "([0-9]{1,2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])[.]"
  144.                                                                 + "([0-9]{1,2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])[.]"
  145.                                                                 + "([0-9]{1,2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])")) {
  146.                                         JOptionPane.showMessageDialog(mf, "IP不符合规范请确认....");
  147.                                 } else {
  148.                                         new Client(mf);
  149.                                         if (!mf.getPane().getMsgjt().getText().equals("")) {
  150.                                                 // System.out.println(mf.getPane().getContendjt().getText());
  151.                                                 mf.getPane()
  152.                                                                 .getContendjt()
  153.                                                                 .setText("我说:  "+ msg+ "  ("+ new SimpleDateFormat("MM-dd HH:mm:ss").format(new Date())
  154.                                                                                                 + ")\n"+ mf.getPane().getContendjt().getText());
  155.                                         }
  156.                                         fport = mf.getPane().getPortjt().getText();
  157.  
  158.                                 }
  159.                                 mf.getPane().getMsgjt().setText("");
  160.                         }
  161.                 });
  162.  
  163.                 // ip地址
  164.                 this.ipjt = new JComboBox();
  165.                 this.ipjt.setBounds(340, 320, 170, 30);
  166.                 this.ipjt.setEditable(true);
  167.                 this.ipjt.setFont(new Font("宋体", Font.PLAIN, 22));
  168.                 this.ipjt.setForeground(Color.RED);
  169.                 this.ipjt.addItem("127.0.0.1");
  170.                 this.ipjt.addItem(mf.getMyip());
  171.  
  172.                 this.add(this.ipjt);
  173.                 // 清屏按钮
  174.                 this.clearjb = new JButton("清屏");
  175.                 this.clearjb.setBounds(510, 100, 60, 30);
  176.                 this.add(this.clearjb);
  177.                 this.clearjb.addActionListener(new ActionListener() {
  178.  
  179.                         @Override
  180.                         public void actionPerformed(ActionEvent arg0) {
  181.                                 // TODO Auto-generated method stub
  182.                                 mf.getPane().getContendjt().setText("");
  183.                         }
  184.                 });
  185.  
  186.                 // 退出按钮
  187.                 this.exitjb = new JButton("退出");
  188.                 this.exitjb.setBounds(510, 30, 60, 30);
  189.                 this.add(this.exitjb);
  190.                 this.exitjb.addActionListener(new ActionListener() {
  191.  
  192.                         @Override
  193.                         public void actionPerformed(ActionEvent arg0) {
  194.                                 // TODO Auto-generated method stub
  195.                                 System.exit(0);
  196.  
  197.                         }
  198.                 });
  199.  
  200.                 // 内容栏
  201.                 this.contendjt = new JTextArea();
  202.                 this.contendjt.setFont(new Font("宋体", Font.PLAIN, 20));
  203.                 this.contendjt.setBorder(BorderFactory.createLineBorder(Color.BLACK));
  204.                 this.contendjt.setEditable(false);
  205.                 this.scrolljs = new JScrollPane(this.contendjt);
  206.                 this.scrolljs.setBounds(0, 0, 500, 300);
  207.                 this.add(this.scrolljs);
  208.  
  209.         }
  210.  
  211.         public JTextField getMsgjt() {
  212.                 return msgjt;
  213.         }
  214.  
  215.         public void setMsgjt(JTextField msgjt) {
  216.                 this.msgjt = msgjt;
  217.         }
  218.  
  219.         public JComboBox getIpjt() {
  220.                 return ipjt;
  221.         }
  222.  
  223.         public void setIpjt(JComboBox ipjt) {
  224.                 this.ipjt = ipjt;
  225.         }
  226.  
  227.         public JTextField getPortjt() {
  228.                 return portjt;
  229.         }
  230.  
  231.         public void setPortjt(JTextField portjt) {
  232.                 this.portjt = portjt;
  233.         }
  234.  
  235.         public JTextArea getContendjt() {
  236.                 return contendjt;
  237.         }
  238.  
  239.         public void setContendjt(JTextArea contendjt) {
  240.                 this.contendjt = contendjt;
  241.         }
  242.  
  243. }
  244. //源代码片段来自云代码http://yuncode.net
  245.                        

回复 "java在线聊天及文件传输系统5"

这儿你可以回复上面这条便签

captcha