java可视化编程

/**
 * 
 */
package com.netty.client;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.net.DatagramPacket;
import java.net.SocketAddress;
import java.util.Map;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JTextArea;
import javax.swing.JTextField;

/**
 * @author HUANGLIAO322
 *
 */
public class ClientP2P extends Thread implements ActionListener {
	
    public JFrame frame;  
    //聊天信息  
    public JTextArea info;  
    //在线用户  
    public JTextArea onlineUser;  
    public JTextField msgText;  
      
    public JButton sendButton;  
    
    public ClientP2P(){
    	frame=new JFrame("P2P聊天");  
		frame.setSize(800, 400);
		
		sendButton=new JButton("发送");  
        JScrollBar scroll=new JScrollBar();  
        info=new JTextArea(10,30);
        
        //激活自动换行功能   
        info.setLineWrap(true);  
        info.setWrapStyleWord(true);  
        info.setEditable(false);  
        scroll.add(info); 
        
        onlineUser=new JTextArea(10,30);  
        onlineUser.setLineWrap(true);  
        onlineUser.setWrapStyleWord(true);  
        onlineUser.setEditable(false);
        
        JPanel infopanel=new JPanel();  
        infopanel.add(info,BorderLayout.WEST);  
        JPanel infopanel1=new JPanel();  
        JLabel label=new JLabel("在线用户");  
        infopanel1.add(label, BorderLayout.NORTH);  
        infopanel1.add(onlineUser, BorderLayout.SOUTH);  
        infopanel.add(infopanel1,BorderLayout.EAST); 
        
        JPanel panel=new JPanel();
        
        msgText=new JTextField(30);  
        
        panel.add(msgText);  
        panel.add(sendButton);  
        frame.add(infopanel,BorderLayout.NORTH);  
        frame.add(panel,BorderLayout.SOUTH);  
        frame.setVisible(true);  
          
        sendButton.addActionListener(this);  
          
        frame.addWindowListener(new   WindowAdapter(){   
            public   void   windowClosing(WindowEvent   e){   
                System.exit(0);  
            }   
         });  
    }
	
	public static void main(String[] args) {
		ClientP2P clientP2P = new ClientP2P();
		
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		  
        if(e.getSource()==this.sendButton)  
        {  
            try{  
                String msg=this.msgText.getText();  
                if(msg.length()>0)  
                {  
//                    this.info.append("我说:"+msg);  
//                    this.info.append("\n");  
//                    for (Map.Entry<String, SocketAddress> entry : userMap.entrySet()) {  
//                        DatagramPacket data=new DatagramPacket(msg.getBytes(),msg.getBytes().length,entry.getValue());  
//                        client.send(data);  
//                    }  
                      
//                    this.msgText.setText("");  
                }  
            }  
            catch(Exception ee){}  
        }  
          
    
	}

}

猜你喜欢

转载自blog.csdn.net/qq5132834/article/details/89104820
今日推荐