⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chater.java

📁 简单清晰具有启发性的实例。 Net1和Client1类实现了TCP的简单应用。 Udp和Udpwatch类实现了UDP的简单应用。 Chater是一个聊天小工具
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
import java.util.*;
import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class Chater extends JFrame
{
  private static int state;
  static final int STARTED = 0;
  static final int CONNECTED = 1;
  static final int DISCONNECTED = 0;
  static final int CONNECTING = 2;
	private Chater parent;
	public Socket soc;
  static InetAddress remoteAddr;
  static int remotePort;
	public static final int PORT = 11111;
  private static JLabel stateLable = new JLabel("started", JLabel.RIGHT);
  static JTextField tf = new JTextField("",20);
  private static JTextArea mta = new JTextArea("",2,20);
  public JMenuItem command_connect = new JMenuItem("connect");
	public JMenuItem command_disconnect = new JMenuItem("disconnect");
	public JMenuItem file_send = new JMenuItem("send files");
	public JMenuBar mb = new JMenuBar();
	public JMenu m_File = new JMenu("File");
	public JMenu m_command = new JMenu("Command");
	Chater()
	{
			parent = this;
			command_disconnect.addActionListener(new ActionListener()
																							{
								 																		public void actionPerformed(ActionEvent e)
								 																		{
											 																		parent.m_command.remove(command_disconnect);
											 																		parent.setTheState(Chater.DISCONNECTED);
											  	   														  parent.m_File.remove(parent.file_send);
											 																		parent.command_connect.setEnabled(true);
											 																		parent.tf.setEditable(false);
											 																		parent.setMessage("You have disconnected from:"+remoteAddr);
											 																		setTheState(DISCONNECTED);
											 																		new Send("#DISCONNECT");
			 																							}
			 																				});
			JMenuItem file_quit = new JMenuItem("Quit");
			file_quit.addActionListener(new ActionListener()
																			{
			 																		public void actionPerformed(ActionEvent e)
			 																		{
			 																				try
			 																				{
			 																					soc.close();
			 																				}
			 																				catch(Exception ee)
			 																				{
			 																					ee.printStackTrace();
			 																				}
			 																				System.exit(0);
			 																		}
			 																}
			 														);
			JPanel p2 = new JPanel();
			JPanel p1 = new JPanel(new GridLayout(2,1));
			JButton b1 = new JButton("send");
			FileDialog fd = new FileDialog(this, "Chose a file to send");
	//GUI 
	//m_File.add(file_send);
			m_File.add(file_quit);
			command_connect.addActionListener(new Dlg_connect());
			m_command.add(command_connect);
			
			mb.add(m_File);
			mb.add(m_command);
			setJMenuBar(mb);
			
			mta.setEditable(false);
			mta.setLineWrap(true);
      mta.setWrapStyleWord(true);
      JScrollPane areaScrollPane = new JScrollPane(mta);
      areaScrollPane.setVerticalScrollBarPolicy(
                        JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
      areaScrollPane.setPreferredSize(new Dimension(100, 30));
      areaScrollPane.setBorder(
      BorderFactory.createCompoundBorder(
                BorderFactory.createCompoundBorder(
                                BorderFactory.createTitledBorder("Message"),
                                BorderFactory.createEmptyBorder(5,5,5,5)),
                areaScrollPane.getBorder()));
              
		  
		  tf.setEditable(false);
		  JScrollPane pane = new JScrollPane(tf);
      pane.setVerticalScrollBarPolicy(
                        JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
      pane.setPreferredSize(new Dimension(100, 30));
      pane.setBorder(
      BorderFactory.createCompoundBorder(
                BorderFactory.createCompoundBorder(
                                BorderFactory.createTitledBorder("Enter"),
                                BorderFactory.createEmptyBorder(5,5,5,5)),
                areaScrollPane.getBorder()));
              
		  p1.add(areaScrollPane);
			p1.add(pane);
			add(p1);  
			p2.add(b1);	
			p2.add(stateLable);	
			add("South",p2);
			try{
			ServerSocket s = new ServerSocket(PORT);
			new Server1(s,parent);
	   }catch(Exception e){
	   	e.printStackTrace();
		 }
		 
		 b1.addActionListener(new Send());
		}
class Send implements ActionListener{
	  public Send(){}
	  public Send(String s){
	  		try{  byte[] buff;
							DatagramSocket ds = new DatagramSocket();						
					    buff = s.getBytes();					    
					  	DatagramPacket dp = new DatagramPacket(buff,buff.length,remoteAddr,remotePort);
					  	ds.send(dp);
					}catch(Exception ex){
							ex.printStackTrace();
						}
	  	}
		public void actionPerformed(ActionEvent e){
			  byte[] buff;
				if(stateLable.getText().equals("connected")){
						try{
							String str = new String(tf.getText());
							DatagramSocket ds = new DatagramSocket();
							
					    buff = str.getBytes();
					    System.out.println("sending:"+new String(buff));
					    
					  	DatagramPacket dp = new DatagramPacket(buff,buff.length,remoteAddr,remotePort);
					  	ds.send(dp);
					}catch(Exception ex){
							ex.printStackTrace();
						}
			}else{
				JOptionPane.showMessageDialog(parent,
        "The connection has not been created yet",
        "Unable to send",
        JOptionPane.WARNING_MESSAGE);

			}
			tf.setText(null);	
	}
}
class Dlg_connect implements ActionListener{
		public void actionPerformed(ActionEvent e){
				DialogConnect dg_connect = new DialogConnect(parent);
				dg_connect.setVisible(true);
			}
	}
  public static void setMessage(String s){
			mta.setText(s);
		}
	public static void appendMessage(String s){	  
			mta.append(s);
			mta.setCaretPosition(mta.getText().length());
		}
	public void send(String s){
			new Send(s);
		}
	public static void setTheState(int s){
			switch(s){
					case 0:{
						if(state == STARTED)
							stateLable.setText("started");
						if(state == DISCONNECTED)
							stateLable.setText("disconnected");
						break;
						}
					case 1:{
							stateLable.setText("connected");
						break;
						}
					case 2:{
							stateLable.setText("CONNECTING");
						}
				}
		state = s;
		}
	public static int getTheState(){
			return state;
		}
	public static void main(String[] args){
			Chater f = new Chater();
			f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			f.setSize(400,400);
			f.setVisible(true);
		}
}

class DialogConnect extends JDialog{
	TextField t_addr = new TextField(20);
	TextField t_port = new TextField(20);
	JButton b_connect = new JButton("connect");
	JButton b_cancel = new JButton("cancel");
	private Chater mainFrame;
	public DialogConnect(Chater parent){
			 super(parent,"connect who?",false);
			 mainFrame = parent;
			 setSize(200,200);
			 setLayout(new FlowLayout());		 
			 try{
			 b_connect.addActionListener(new Connect());
			 }catch(Exception e){
			 		e.printStackTrace();
			 	}
			 b_cancel.addActionListener(new ActionListener(){
			 																		public void actionPerformed(ActionEvent e){
			 																		setVisible(false);}
			 																		});
			 add(new JLabel("address"));
			 add(t_addr);
			 add(new JLabel("port"));
			 add(t_port);
			 add(b_connect);
			 add(b_cancel);
		}
		class Connect implements ActionListener{
		private InetAddress addr;
		private int port;
	  public void actionPerformed(ActionEvent e){
			try{
		   		InetAddress addr = InetAddress.getByName(t_addr.getText());		 
			 		int port = Integer.valueOf(t_port.getText());
			 		System.out.println("try to connect:");
				  System.out.println("addr = "+addr);
				  System.out.println("port = "+port);
//				  new Receiver(mainFrame.PORT);
			  	Socket s = new Socket(addr, port);
			  	mainFrame.soc = s;
			  	mainFrame.setTheState(Chater.CONNECTING);		  	
          new Connecting(s,mainFrame);			  
			    setVisible(false);
			  }
			  catch(Exception ex){
			  System.out.println("connection failed");
			  Chater.setTheState(Chater.DISCONNECTED);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -