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

📄 f054c12873d700191f48b4d0fe87e2ab

📁 使用TCP和UDP协议
💻
📖 第 1 页 / 共 2 页
字号:
				    {
				        PublicChatGUI.this.appendMessageAreaText(ERROR + "尚未连接,请选择\"系统\\建立连接\"以建立与服务器的TCP连接");
				        return;
				    }
				    //未登录则进行登陆:
					if (!hasLogin)
					{
						if((inputArea.getText().equals("")) || (inputArea.getText().indexOf(" ") != -1))
						{
						    messageArea.append(ERROR + "您没有输入或者输入的登录名中存在空格");
						    messageArea.append(PROMPT + "请输入用户名,点击登陆按钮进行登录");
						}
						else
						{
						    //debug
					        System.out.println("Run here");
						    loginSuccess = tcpConnection.sendLoginInfo(inputArea.getText());
						    //如果没有重名用户,登陆成功,然后去得到欢迎信息&端口号和用户列表
						    if (loginSuccess)
						    {
						        afterLogin();
						    }
						    //如若有重名用户,重新开始连接登陆
						    else
						    {
						        disConnect();
						        messageArea.append(ERROR + "!存在重名用户,请重新连接");
						    }
						}
					}
					//否则,用UDP连接发送消息:
					else
					{ 
					    if (privateReceiver != null)
					        messageToBeSent = new String("PRIVATE FROM " + userName + " TO " + (String)privateReceiver + " MSG " + inputArea.getText());
					    else
					        messageToBeSent = new String("PUBLIC " + userName + ": " + inputArea.getText());
					    if (messageToBeSent.length() > 1024)
					    {
					        PublicChatGUI.this.appendMessageAreaText(ERROR + "输入信息太长,服务器无法接受,请重新输入");
					        PublicChatGUI.this.setinputAreaText("");
					        return ;
					    }
					    sendUDPMessage(messageToBeSent);
					}
				}
			});
		}
		if (exitButton == null)
		{
		    exitButton = new JButton(new ImageIcon(getClass().getResource("Icons\\exit.gif")));
		    //用Alt + Escape 退出
		    exitButton.setMnemonic(KeyEvent.VK_ESCAPE);
		    exitButton.setToolTipText("Alt + Escape退出");
		    exitButton.addActionListener(new ActionListener() {
		        public void actionPerformed(ActionEvent e)
		        {
		            if (hasLogin)
		            {
		                disConnect();
		            }
		            System.exit(0);
		        }
            });
		}
		//设置Panel:
				//将messagePane放入messagePanel中
				messagePanel.add(BorderLayout.CENTER, messagePane);
				//以下是sendPanel的部分:
				toolbarPanel.add(BorderLayout.CENTER, toolBar);
				inputPanel.add(BorderLayout.CENTER, inputPane);
				
				buttonPanel.add(sendButton);
				buttonPanel.add(exitButton);
				
				sendPanel.add(BorderLayout.NORTH, toolbarPanel);
				sendPanel.add(BorderLayout.CENTER, inputPanel);
				sendPanel.add(BorderLayout.SOUTH, buttonPanel);
			//将messagePanel和sendPanel包装于communicationPanel
			communicationPanel.add(messagePanel);
			communicationPanel.add(sendPanel);
			//将用户列表栏添加到listPanel中
			
			listPanel.add(BorderLayout.NORTH, new JLabel("沦落人家"));
			listPanel.add(BorderLayout.CENTER, listPane);	
		panel.add(BorderLayout.CENTER, communicationPanel);
		panel.add(BorderLayout.EAST, listPanel);
	}
	
	void initMenuBar()
	{
		if (menuBar == null)
			menuBar = new JMenuBar();
		
		JMenu fileMenu = new JMenu("文件(F)");
		fileMenu.setMnemonic(KeyEvent.VK_F);
		JMenuItem exitItem = new JMenuItem("退出(X)");
		exitItem.setMnemonic(KeyEvent.VK_X);
		exitItem.addActionListener(new ActionListener() {
		    public void actionPerformed(ActionEvent e)
		    {
		        if (PublicChatGUI.this.hasLogin)
		        {
			        disConnect();
		        }
		        System.exit(0);
		    }
		});
		fileMenu.add(exitItem);
		
		JMenu systemMenu = new JMenu("系统(S)");
		systemMenu.setMnemonic(KeyEvent.VK_S);
		JMenuItem connectItem = new JMenuItem("连接(C)");
		connectItem.setMnemonic(KeyEvent.VK_C);
		connectItem.addActionListener(new ActionListener() {
		    public void actionPerformed(ActionEvent e) {
                //建立TCP连接:
                connectToServer();
            }
		});
		JMenuItem disconnectItem = new JMenuItem("断开连接(N)");
		disconnectItem.setMnemonic(KeyEvent.VK_N);
		disconnectItem.addActionListener(new ActionListener() {
		    public void actionPerformed(ActionEvent e)
		    {
		        disConnect();
		        lItems.removeElement(userName);
		    }
        });
		systemMenu.add(connectItem);
		systemMenu.add(disconnectItem);
		
		JMenu settingMenu = new JMenu("选项(E)");
		settingMenu.setMnemonic(KeyEvent.VK_E);
		JMenuItem serverNameItem = new JMenuItem("服务器地址(D)..");
		serverNameItem.setMnemonic(KeyEvent.VK_D);
		JMenuItem portNumberItem = new JMenuItem("服务器TCP端口(P)..");
		portNumberItem.setMnemonic(KeyEvent.VK_P);
		
		serverNameItem.addActionListener(new ActionListener() {
		    public void actionPerformed(ActionEvent e)
		    {
		        ConfigDialog configDialog = new ConfigDialog(PublicChatGUI.this, host, port);
				configDialog.portField.setEditable(false);
				
				configDialog.show();
				host = configDialog.serverName;
				//调试
//				System.out.println("configDialog.serverName:" + configDialog.serverName);
		    }
        });
		portNumberItem.addActionListener(new ActionListener() {
		    public void actionPerformed(ActionEvent e)
		    {
				ConfigDialog configDialog = new ConfigDialog(PublicChatGUI.this, host, port);
				configDialog.nameField.setEditable(false);
				
				configDialog.show();
				port = configDialog.serverTcpPort;
		    }
        });
		
		settingMenu.add(serverNameItem);
		settingMenu.add(portNumberItem);
		
		JMenu helpMenu = new JMenu("帮助(H)");
		helpMenu.setMnemonic(KeyEvent.VK_H);
		JMenuItem about = new JMenuItem("关于\"别问我是谁,请与我面对\"聊天软件..");
		about.addActionListener(new ActionListener() {
		    public void actionPerformed(ActionEvent e)
		    {
		        JOptionPane.showMessageDialog(PublicChatGUI.this, 
		                "版权所有:梅南翔\n版权单位:同济大学软件软件学院\n日期:2005年6月",
		                "About..", JOptionPane.OK_OPTION);
		    }
		});
		helpMenu.add(about);
		
		menuBar.add(fileMenu);
		menuBar.add(systemMenu);
		menuBar.add(settingMenu);
		menuBar.add(helpMenu);
	}
	
	void initFrame()
	{
	    this.addWindowListener(new WindowAdapter() {
	        public void windowClosing(WindowEvent frameEvent)
	        {
	            if (hasLogin)
	            {
	                disConnect();
	            }
	            System.exit(0);
	        }
	    });
		this.setSize(500, 550);
		this.setTitle("别问我是谁,请与我面对");
	}
	
	PublicChatGUI()
	{
		initFrame();
		initPanel();
		initMenuBar();
	}
	
	public static void main(String[] args)
	{
		PublicChatGUI chatClient = new PublicChatGUI();
		
		chatClient.setContentPane(chatClient.panel);
		
		chatClient.setJMenuBar(chatClient.menuBar);
		chatClient.show();
	}
}

class ConfigDialog extends JDialog
{
    String serverName = null;
    int serverTcpPort = 0;
    JTextField nameField = new JTextField();
    JTextField portField = new JTextField();
    
    public ConfigDialog(JFrame parent, String server, int tcpport)
    {
        super(parent, "配置服务器地址和TCP端口", true);
        this.serverName = server;
        this.serverTcpPort = tcpport;
        Container cp = getContentPane();
        cp.setLayout(new BorderLayout());
        
        JPanel configPanel = new JPanel(new GridLayout(2,2));
        JPanel buttonPanel = new JPanel(new FlowLayout());
        
        JLabel nameLabel = new JLabel("输入服务器主机名或IP地址");
        JLabel portLabel = new JLabel("输入服务程序TCP端口号码");
        nameField.setText(serverName);
        portField.setText(String.valueOf(serverTcpPort));
        configPanel.add(nameLabel);
        configPanel.add(nameField);
        configPanel.add(portLabel);
        configPanel.add(portField);
        
        JButton ok = new JButton("Ok");
        ok.addActionListener(new ActionListener() {
            	public void actionPerformed(ActionEvent e)
            	{
            	    //如果是要设置端口号,对其进行合法性检测:
            	    if ( true == portField.isEditable())
            	        //合法就进行修改:
            	        if (islegal())
            	        {
            	          ConfigDialog.this.serverTcpPort = Integer.parseInt(portField.getText());
            	          dispose();
            	        }
            	        //非法时,显示出错信息,直到输入正确或取消为止:
            	        else
            	        {
            	            portField.setText("");
            	            JOptionPane.showMessageDialog(null, "输入了非数字或是端口号无效", "Wrong", JOptionPane.ERROR_MESSAGE);
            	        }
            	    //如果是输入主机名,就不进行检测:
            	    else
            	    {
            	        ConfigDialog.this.serverName = nameField.getText();
            	        dispose();
            	    }
            	    //debug:
//            	    System.out.println("ConfigDialog.this.servername:" + ConfigDialog.this.serverName);
            	}
            	private boolean islegal()
            	{
            	    int temp = 0;
            	    boolean legal = true;
            	    try
            	    {
            	        temp = Integer.parseInt(portField.getText());
            	    } catch(NumberFormatException ep)
            	    {
            	        //如果输入中有非数值出现,就是非法的
            	        legal = false;
            	    }
            	    //端口号要在1024到65535的范围之内
            	    if (legal == true)
            	        if (temp < 1024 || temp > 65536)
            	            legal = false;
            	    return legal;
            	}
        });
        JButton cancel = new JButton("Cancel");
        cancel.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e)
            {
                dispose();
            }
        });
        buttonPanel.add(ok);
        buttonPanel.add(cancel);
        
        cp.add(BorderLayout.CENTER, configPanel);
        cp.add(BorderLayout.SOUTH, buttonPanel);
        setSize(500, 120);
    }
}

⌨️ 快捷键说明

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