java聊天室 提交预版 登录框(通)


import java.awt.Button;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class SignIn {
	public static void main(String[] args)
	{
		EventQueue.invokeLater(new Runnable(){
			public void run()
			{
				new SignInFrame();
			}
		});
	}
}

class SignInFrame extends JFrame
{
	public SignInFrame()
	{
		setTitle(TITLE);
		setBounds(300,200,DEFAULT_WIDTH,DEFAULT_HEIGHT);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setResizable(false);
		setVisible(true);
		setLayout(null);
		
		panelSignIn = new JPanel();
		panelButton = new JPanel();
		textSignIn = new JTextField(10);
		buttonSignIn = new Button("SignIn");
		panelID = new JPanel();
		textID = new JTextField(10);
		panelMobile = new JPanel();
		textMobile = new JTextField(10);

		buttonSignIn.addActionListener(new ButtonListener());
		
		
		JLabel lbBg;

		/** 加入背景图片 */
		lbBg = new JLabel(new ImageIcon("F:\\bg.jpg"));
		lbBg.setBounds(0, 0, 500, 400);
		
		
		
		panelSignIn.add(textSignIn);
		panelButton.add(buttonSignIn);
		panelSignIn.setBounds(176, 170,120,32);
		panelButton.setBounds(314,170,60,32);
		
		panelID.add(textID);
		panelID.setBounds(176, 210,120,32);
		
		panelMobile.add(textMobile);
		panelMobile.setBounds(176, 250,120,32);
		
		add(panelSignIn);
		add(panelButton);
		add(panelID);
		add(panelMobile);
		add(lbBg);
		
		
	}
	
	class ButtonListener implements ActionListener
	{

		@Override
		public void actionPerformed(ActionEvent e) {
			if(e.getSource() == buttonSignIn)
			{
				matcherID = patternID.matcher(textID.getText());
				matcherMobile = patternMobile.matcher(textMobile.getText());
				if(textSignIn.getText().trim().equals("") || textSignIn == null)
					JOptionPane.showMessageDialog(null, "We Need Your Name","What's Your Name",JOptionPane.ERROR_MESSAGE);
				else if(!matcherID.find())
					JOptionPane.showMessageDialog(null, "We Need Your ID","What's Your ID",JOptionPane.ERROR_MESSAGE);
				else if(!matcherMobile.find())
					JOptionPane.showMessageDialog(null, "We Need Your Moblie","What's Your Mobile",JOptionPane.ERROR_MESSAGE);
				else
				{
					name = textSignIn.getText();
					id = textID.getText();
					mobile = textMobile.getText();
					
					try
					{
						new ChatFrame(name,id,mobile,new Socket(InetAddress.getLocalHost(),4800));
					} 
					catch (UnknownHostException e1)
					{
						e1.printStackTrace();
					} 
					catch (IOException e1) 
					{
						e1.printStackTrace();
					}
					SignInFrame.this.setVisible(false);
				}
			}
		}
		
	}
	
	private static final long serialVersionUID = -8649115223425727109L;
	String regexID = "20[01][012]EN\\d{3}";
	Pattern patternID = Pattern.compile(regexID);
	Matcher matcherID;
	String regexMobile = "(13[0-9]|15[7-9]|153|156|18[7-9])[0-9]{8}";
	Pattern patternMobile = Pattern.compile(regexMobile);
	Matcher matcherMobile;
	JPanel panelSignIn;
	JPanel panelButton;
	JPanel panelID;
	JPanel panelMobile;
	JTextField textSignIn;
	JTextField textID;
	JTextField textMobile;
	JLabel jLabel;
	Button buttonSignIn;
	String name;
	String id;
	String mobile;
	static final String TITLE = "用户登录";
	static final int DEFAULT_WIDTH = 504;
	static final int DEFAULT_HEIGHT = 427;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值