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();
}
}
最近下载更多
taoshen95 LV16
2024年1月19日
漫步的海星 LV4
2023年8月21日
2017143155 LV12
2023年6月24日
wanglinddad LV55
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日
忧麦紫 LV18
2021年6月26日
最近浏览更多
微信网友_7556333806571520
6月16日
暂无贡献等级
13133117021 LV5
2024年12月26日
srl2881552 LV10
2024年11月25日
微信网友_7004855557083136 LV1
2024年5月22日
taoshen95 LV16
2024年1月19日
陈小灏 LV18
2023年12月27日
求学的熊猫 LV11
2023年12月25日
wuxin427
2023年12月20日
暂无贡献等级
fuyouou LV5
2023年6月29日
2017143155 LV12
2023年6月24日

