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;
}
java聊天室 提交预版 登录框(通)
最新推荐文章于 2024-01-09 09:29:54 发布