package com.test;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.StringTokenizer;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

@SuppressWarnings("serial")
public class Login extends JFrame implements ActionListener{
	private boolean flgAction = false;
	private JLabel l1,l2;
	private JTextField txtUser;
	private JButton btnOK;
	private Socket socket;
	private Parameterx parameterx;
	
	public Login(){
		setBounds(200, 300, 300, 200);
		setTitle("欢迎使用");
		init();
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setVisible(true);
		
		while(true){
			try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if(flgAction == true){
				try {
					String uuname = txtUser.getText().trim().toString();
					DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream());
					String login = "login|" + uuname;
					outputStream.writeUTF(login);
					outputStream.flush();
					flgAction = false;
					DataInputStream inputStream = new DataInputStream(socket.getInputStream());
					String result = inputStream.readUTF();
					
					StringTokenizer tokenizer = new StringTokenizer(result, "|");
					String head = tokenizer.nextToken();
					if(head.equals("login")){
						head = tokenizer.nextToken();
						if(head.equalsIgnoreCase("ok")){
							new Client(socket,uuname);
							dispose();
						}else if(head.equalsIgnoreCase("false")){
							JOptionPane.showMessageDialog(null, "该用户名已在使用中!");
						}
					}
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
			}
		}
		
	}
	public void init(){
		l1 = new JLabel("任意用户名");
		l2 = new JLabel("用户名:");
		txtUser = new JTextField(10);
		btnOK = new JButton("登录");
		JPanel p1 = new JPanel();
		p1.add(l2);
		p1.add(txtUser);
		JPanel p2 = new JPanel();
		p2.add(btnOK);
		Box box = Box.createVerticalBox();
		box.add(l1);
		box.add(Box.createVerticalStrut(3));
		box.add(p1);
		box.add(Box.createVerticalStrut(1));
		box.add(p2);
		add(box);
		
		btnOK.addActionListener(this);
		
		parameterx = new Parameterx();
		int kkl = parameterx.port;
		String klj = parameterx.ip;
		try {
			socket = new Socket(klj, kkl);
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}
	
	public void actionPerformed(ActionEvent e){
		if(e.getSource() == btnOK){
			String name = txtUser.getText().toString().trim();
			if(!name.equals("")){	
				flgAction = true;
				
			}else{
				JOptionPane.showMessageDialog(null, "用户名不能为空");
			}
		}
	}
	public static void main(String[] args) {
		new Login();
	}
}
最近下载更多
wanglinddad  LV51 2022年6月18日
微信网友_6003487859068928  LV5 2022年6月16日
zhendong  LV7 2022年3月6日
dfz12345  LV4 2021年12月8日
胡萝北呀  LV3 2021年11月28日
ComeDebug  LV6 2021年9月6日
忧麦紫  LV17 2021年6月26日
zktotti 2021年6月23日
暂无贡献等级
 LV2 2021年6月16日
wlp6688  LV2 2021年3月16日
最近浏览更多
CL200228  LV4 5月4日
代做毕业设计  LV13 4月18日
xingbing  LV9 2月10日
hhdbzz  LV1 2022年12月29日
666ing  LV1 2022年12月16日
nywgzhc  LV10 2022年12月7日
sslhss 2022年11月30日
暂无贡献等级
微信网友_6229090122354688 2022年11月21日
暂无贡献等级
罗清晨  LV6 2022年9月28日
胡佳文 2022年6月25日
暂无贡献等级
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友