HiChat简单聊天程序

有人说swing编写程序可再用性比较差,通过学习我觉得也是这样,因为界面部分代码实在是很乱。

程序界面:

               

程序代码:

Code:
  1. import java.awt.*;   
  2. import java.awt.event.*;   
  3. import javax.swing.*;   
  4. import java.net.*;   
  5. import java.io.IOException;   
  6. import java.io.*;   
  7.   
  8.   
  9. public class chat {   
  10.     public static void main(String[] args) {   
  11.         new UDPMessage();   
  12.     }   
  13. }   
  14.   
  15. class UDPMessage extends JFrame implements ActionListener {   
  16.        
  17.     /**  
  18.      *   
  19.      */  
  20.     private static final long serialVersionUID = 1L;   
  21.     private JTextArea recordText = new JTextArea();   
  22.     private JTextArea sendText = new JTextArea();   
  23.     private DatagramSocket ds;   
  24.     private JButton btnSend = new JButton("发送");   
  25.     private JButton btnExit = new JButton("关闭");   
  26.     private JLabel otherTips = new JLabel("对方IP");   
  27.     private JTextField otherIp = new JTextField();    
  28.     private JLabel myTips = new JLabel("我的IP");   
  29.     private JTextArea myIp = new JTextArea();   
  30.     InputStreamReader in;                   //创建一个文件输入流   
  31.     OutputStreamWriter out;                 //创建一个文件输出流   
  32.     Container p;   
  33.     JScrollPane jspRecord;   
  34.     JMenuBar mb = new JMenuBar();   
  35.     JMenu[] m = {new JMenu("文件"),   
  36.                              new JMenu("好友"),   
  37.                    new JMenu("背景")};   
  38.     JMenuItem[][] mi = {{new JMenuItem("传送文件")},   
  39.                                             {new JMenuItem("Regina"),new JMenuItem("Taotao"),new JMenuItem("LiShuo")},   
  40.                           {new JMenuItem("默    认"),new JMenuItem("浅灰色"),new JMenuItem("淡紫色"),new JMenuItem("黄绿色")}};   
  41.        
  42.     public UDPMessage() {   
  43.         super("HiChat");   
  44.         p = getContentPane();   
  45.         setSize(550475);   
  46.         centerOnScreen();   
  47.         setResizable(false);   
  48.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   
  49.         for(int i=0; i<m.length; i++) {   
  50.             mb.add(m[i]);   
  51.             for(int j=0; j<mi[i].length; j++) {   
  52.                 m[i].add(mi[i][j]);   
  53.                 mi[i][j].addActionListener(this);   
  54.             }   
  55.         }   
  56.         setJMenuBar(mb);   
  57.         p.setBackground(new Color(218112214));   
  58.         p.setLayout(null);   
  59.         recordText.setEditable(false);   
  60.         recordText.setLineWrap(true);   
  61.         jspRecord = new JScrollPane(recordText);   
  62.         jspRecord.setBounds(25350270);   
  63.         p.add(jspRecord);   
  64.         sendText.setLineWrap(true);   
  65.         JScrollPane jspSend = new JScrollPane(sendText);   
  66.         jspSend.setBounds(2285350,100);   
  67.         p.add(jspSend);   
  68.         btnExit.setBounds(184,390,80,25);   
  69.         btnExit.setMargin(new Insets(0000));   
  70.         btnExit.addActionListener(this);   
  71.         p.add(btnExit);   
  72.         btnSend.setBounds(271,390,80,25);   
  73.         btnSend.setMargin(new Insets(0000));   
  74.         btnSend.addActionListener(this);   
  75.         p.add(btnSend);   
  76.         GridLayout gl = new GridLayout(20);   
  77.         gl.setVgap(15);   
  78.         JPanel infoPane = new JPanel(gl);   
  79.         infoPane.setOpaque(false);   
  80.         JPanel otherPane = new JPanel(null);   
  81.         otherTips.setBounds(10108020);   
  82.         otherTips.setForeground(Color.red);   
  83.         otherIp.setBounds(3535105,20);   
  84.         JLabel otherPhoto = new JLabel(new ImageIcon("kabb.jpg"));   
  85.         otherPhoto.setBounds(3065120120);   
  86.         otherPane.add(otherPhoto);   
  87.         otherPane.add(otherTips);   
  88.         otherPane.add(otherIp);   
  89.         infoPane.add(otherPane);   
  90.         JPanel myPane = new JPanel(null);   
  91.         myTips.setBounds(10108020);   
  92.         myTips.setForeground(Color.red);   
  93.         myIp.setBounds(3535105,20);   
  94.         try{   
  95.             myIp.setText(" "+InetAddress.getLocalHost().getHostAddress());   
  96.         } catch(UnknownHostException e) {   
  97.             e.printStackTrace();   
  98.         }   
  99.         myIp.setEditable(false);   
  100.         JLabel myPhoto = new JLabel(new ImageIcon("kabbn.jpg"));   
  101.         myPhoto.setBounds(3065120120);   
  102.         myPane.add(myPhoto);   
  103.         myPane.add(myTips);   
  104.         myPane.add(myIp);   
  105.         infoPane.add(myPane);   
  106.         infoPane.setBounds(3575185410);   
  107.         p.add(infoPane);   
  108.         server();   
  109.         setVisible(true);      
  110.     }   
  111.        
  112.     private void server() {   
  113.         try {   
  114.             ds = new DatagramSocket(9527);   
  115.             byte[] buf = new byte[1024];   
  116.             final DatagramPacket dp = new DatagramPacket(buf, buf.length);   
  117.             Runnable run = new Runnable() {   
  118.                 public void run() {   
  119.                     while(true) {   
  120.                         try {   
  121.                             Thread.sleep(100);   
  122.                             ds.receive(dp);   
  123.                             int length = dp.getLength();   
  124.                             String message = new String(dp.getData(), 0, length);   
  125.                             String ip = dp.getAddress().getHostAddress();   
  126.                             if(!InetAddress.getLocalHost().getHostAddress().equals(ip)) {   
  127.                                 recordText.append(dp.getAddress().getHostName()+":/n      "+message+"/n");   
  128.                             }   
  129.                             recordText.setCaretPosition(recordText.getText().length());   
  130.                         } catch(IOException e) {   
  131.                             e.printStackTrace();   
  132.                         } catch(InterruptedException e) {   
  133.                             e.printStackTrace();   
  134.                         }   
  135.                     }   
  136.                 }   
  137.             };   
  138.             new Thread(run).start();               
  139.         } catch(SocketException e) {   
  140.             e.printStackTrace();   
  141.         }   
  142.     }   
  143.        
  144.     public void actionPerformed(ActionEvent e) {   
  145.         if(e.getActionCommand() == "默    认") {   
  146.             p.setBackground(new Color(240255255));   
  147.         }   
  148.         if(e.getActionCommand() == "浅灰色") {   
  149.             p.setBackground(new Color(192192192));   
  150.         }   
  151.         if(e.getActionCommand() == "淡紫色") {   
  152.             p.setBackground(new Color(218112214));   
  153.         }   
  154.         if(e.getActionCommand() == "黄绿色") {   
  155.             p.setBackground(new Color(1272550));   
  156.         }   
  157.         if(e.getActionCommand() == "传送文件") {   //功能尚未实现,一下代码无法使用   
  158.             /*try {  
  159.                 ds = new DatagramSocket(9527);  
  160.                 byte[] buf = new byte[1024];  
  161.                 File f = new File("Test4.java");  
  162.                 FileInputStream fis = new FileInputStream(f);  
  163.                 //FileOutputStream fos = new FileOutputStream(f);  
  164.                 while(fis.read(buf)>0) {  
  165.                     final DatagramPacket dp = new DatagramPacket(buf, buf.length);  
  166.                     ds.receive(dp);  
  167.                 }         
  168.             } catch(IOException ee) {  
  169.                 ee.printStackTrace();  
  170.             }*/  
  171.         }   
  172.         if(e.getActionCommand() == "Regina") {   
  173.             otherIp.setText("对方ip地址");   
  174.         }   
  175.         if(e.getActionCommand() == "Taotao") {   
  176.             otherIp.setText("对方ip地址");   
  177.         }   
  178.         if(e.getActionCommand() == "LiShuo") {   
  179.             otherIp.setText("对方ip地址");   
  180.         }   
  181.         if(e.getSource() == btnSend) {   
  182.             try {   
  183.                 String ip = otherIp.getText();   
  184.                 InetAddress addr = InetAddress.getByName(ip);   
  185.                 byte[] data = sendText.getText().getBytes();   
  186.                 DatagramPacket dp = new DatagramPacket(data, data.length, addr, 9527);   
  187.                 String myName = InetAddress.getLocalHost().getHostName();   
  188.                 recordText.append(myName+":/n      "+sendText.getText()+"/n");   
  189.                 ds.send(dp);   
  190.                 sendText.setText(null);   
  191.             } catch(UnknownHostException ee) {   
  192.                 ee.printStackTrace();   
  193.             } catch(IOException ee) {   
  194.                 ee.printStackTrace();   
  195.             }   
  196.         }   
  197.         if(e.getSource() == btnExit) {   
  198.             System.exit(0);   
  199.         }   
  200.     }   
  201.        
  202.     public void centerOnScreen() {        //使窗体被创建后在屏幕中间显示   
  203.         Dimension displaySize = getToolkit().getScreenSize();   
  204.         Dimension winSize = getSize();   
  205.         int x = (displaySize.width - winSize.width) / 2;   
  206.         int y = (displaySize.height - winSize.height) / 2;   
  207.         if(x < 0) {   
  208.             x = 0;   
  209.         }   
  210.         if(y < 0) {   
  211.             y = 0;   
  212.         }   
  213.         setLocation(x, y);   
  214.     }   
  215. }  

swing本来就很庞杂,所以自学习中明白那种最适合自己就选用那种,因为要实现相同效果的程序可以用很多种不同的swing组件来实现,所以我一般是想到那里就学习那个的用法,然后组在一起构成程序。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值