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

📄 clientwindow.java

📁 聊天程序
💻 JAVA
字号:
package com.javahomework.Client.SWT;

import org.eclipse.jface.dialogs.InputDialog;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.SWT;

import java.io.*;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Button;

public class ClientWindow {

	private Shell sShell = null;  //  @jve:decl-index=0:visual-constraint="7,17"
	private List listUsers = null;
	private Text txtMessageOutput = null;
	private Text txtMessageToSend = null;
	private Label lblTO = null;
	private Label lblUserList = null;
	private Label lbMessagel = null;
	private ClientService clientService=null;
	//private Display display=null;
	private Button btnSend = null;

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		/* Before this is run, be sure to set up the launch configuration (Arguments->VM Arguments)
		 * for the correct SWT library path in order to run with the SWT dlls. 
		 * The dlls are located in the SWT plugin jar.  
		 * For example, on Windows the Eclipse SWT 3.1 plugin jar is:
		 *       installation_directory\plugins\org.eclipse.swt.win32_3.1.0.jar
		 */
		Display display = Display.getDefault();
		ClientWindow thisClass = new ClientWindow();
		thisClass.createSShell();
		thisClass.sShell.open();

		while (!thisClass.sShell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		display.dispose();
	}

	/**
	 * This method initializes sShell
	 */
	private void createSShell() {
		sShell = new Shell(SWT.BORDER | SWT.SHELL_TRIM);
		sShell.setText("欢迎使用Sunever聊天系统客户端v1.0");
		sShell.setSize(new org.eclipse.swt.graphics.Point(611,478));
		listUsers = new List(sShell, SWT.V_SCROLL | SWT.BORDER);
		listUsers.setBounds(new org.eclipse.swt.graphics.Rectangle(10,40,153,351));
		txtMessageOutput = new Text(sShell, SWT.MULTI | SWT.WRAP | SWT.V_SCROLL | SWT.BORDER | SWT.READ_ONLY);
		txtMessageOutput.setBounds(new org.eclipse.swt.graphics.Rectangle(182,42,404,225));
		txtMessageOutput.setEditable(false);
		txtMessageToSend = new Text(sShell, SWT.MULTI | SWT.WRAP | SWT.V_SCROLL | SWT.BORDER);
		txtMessageToSend.setBounds(new org.eclipse.swt.graphics.Rectangle(187,317,402,77));
		lblTO = new Label(sShell, SWT.NONE);
		lblTO.setBounds(new org.eclipse.swt.graphics.Rectangle(183,286,107,24));
		lblTO.setText("请输入您的消息:");
		lblUserList = new Label(sShell, SWT.NONE);
		lblUserList.setBounds(new org.eclipse.swt.graphics.Rectangle(12,7,56,24));
		lblUserList.setText("用户列表:");
		lbMessagel = new Label(sShell, SWT.NONE);
		lbMessagel.setBounds(new org.eclipse.swt.graphics.Rectangle(185,7,63,25));
		lbMessagel.setText("消息记录: ");
		btnSend = new Button(sShell, SWT.NONE);
		btnSend.setBounds(new org.eclipse.swt.graphics.Rectangle(468,413,57,23));
		btnSend.setText("发送");
		btnSend.addMouseListener(new org.eclipse.swt.events.MouseAdapter() {
			public void mouseUp(org.eclipse.swt.events.MouseEvent e) {
				System.out.println("Send"); 
				
				int i =listUsers.getSelectionIndex();
				if(i!=-1){
					String content = txtMessageToSend.getText();
					if(content.length()>0) {
						final String msg="TO "+listUsers.getItem(i)+" "+ content;
						new Thread(new Runnable(){
							public void run(){
								try{
									clientService.response(msg);
								} catch(Exception e){
									MessageBox dialog=new MessageBox(sShell,SWT.OK);
								    dialog.setText("警告");
								    dialog.setMessage(e.getMessage());
								    dialog.open();
								}
								
							}
						}).start();
					} else {
						MessageBox dialog=new MessageBox(sShell,SWT.OK);
					    dialog.setText("警告");
					    dialog.setMessage("请不要发送空消息!");
					    dialog.open();
					}
				} else {
					MessageBox dialog=new MessageBox(sShell,SWT.OK);
				    dialog.setText("警告");
				    dialog.setMessage("请选择一个接收人!");
				    dialog.open();
				}
			}
		});
		
		login();
	}
	private void login () {
		try {
			boolean leaveCycle=true;
			do {
				InputDialog id =new InputDialog(sShell,"用户名","请输入您的用户名",null,null);
				id.open();
				String username = id.getValue();
				if(username==null) {
					System.exit(0);
				} else {
					if (clientService==null)
						clientService=new ClientService(this);
		
					clientService.response("USER "+username);
				}
				String ret=clientService.readUTF();
				
				if(!(ret.startsWith("200"))){
					MessageBox dialog=new MessageBox(sShell,SWT.OK);
				    dialog.setText("警告");
				    dialog.setMessage(ret.substring(4));
				    dialog.open();
				    leaveCycle=false;
				}
				else {
					leaveCycle=true;
				}
				
				//System.out.print(username);
			} while(!leaveCycle);
			clientService.start();
		} catch(IOException e) {
			e.printStackTrace();
		}
	}
	public void refreshUserList(final String[] users){
		try{
			if(sShell.isDisposed())
				System.exit(0);
			Display display=sShell.getDisplay();
			if(!display.isDisposed()){
				display.syncExec( 
						new Runnable(){
							public void run(){
								int i = listUsers.getSelectionIndex();
								
								listUsers.removeAll();
								for(String user : users){
									listUsers.add(user);
								}
								if(i!=-1 && i<listUsers.getItemCount())
									listUsers.select(i);
							}
						}
				);
			}
		} catch(Exception e){
			
			System.exit(0);
		}
			
		
	}
	public void showMessage(final String msg){
		try{
			if(sShell.isDisposed())
				System.exit(0);
			Display display=sShell.getDisplay();
			if(!display.isDisposed()){
				display.syncExec( 
						new Runnable(){
							public void run(){
								String str = txtMessageOutput.getText();
								if(str.length()>0)
									str+="\r\n"+msg;
								else
									str=msg;
								System.out.print(str);
								txtMessageOutput.setText(str);
							}
						}
				);
			}
		} catch(Exception e){
			
			System.exit(0);
		}
		
	}

}

⌨️ 快捷键说明

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