📄 clientlogin.java
字号:
//package qiangdaqi;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.net.*;
class ClientLogin extends JFrame implements ActionListener,KeyListener
{
JPanel a;
JButton b;
JPanel p1;
JPanel p2;
JPanel p3;
JPanel p4;
JLabel name,num,server;
JTextField n,nb,s;
Socket s1;
OutputStream out1;
InputStream input;
JSpinner b2;
public ClientLogin()
{
this.setTitle("客户端登陆器");
this.setSize(400,120);
this.setLocation(420,400);
a=new JPanel();
this.add(a);
a.setBackground(new Color(85,141,142));
name=new JLabel("姓名");
num=new JLabel("序号");
server=new JLabel("服务器IP");
n=new JTextField(10);
nb=new JTextField(5);
s=new JTextField("",13);
b=new JButton("连接服务器");
//b.setBounds(350, 10, 20, 20);
b.setBackground(new Color(123,175,176));
b.setForeground(Color.red);
b2=new JSpinner();
p1=new JPanel();
p2=new JPanel();
p3=new JPanel();
p4=new JPanel();
//p2.setLayout(null);
a.add(p1);
a.add(p2);
p1.setBackground(new Color(85,141,142));
p2.setBackground(new Color(85,141,142));
p1.setLayout(new GridLayout(2,1));
p1.add(p3);
p1.add(p4);
p3.setLayout(new FlowLayout());
p4.setLayout(new FlowLayout());
p3.setBackground(new Color(85,141,142));
p4.setBackground(new Color(85,141,142));
p3.add(name);
p3.add(n);
p3.add(num);
p4.add(server);
p4.add(s);
p3.add(b2);
p2.add(b);
b.addActionListener(this);
//n.addKeyListener(this);
//nb.addKeyListener(this);
s.addKeyListener(this);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e)
{
action();
}
public void keyPressed(KeyEvent e)
{
Object obj=e.getSource();
if(e.getKeyCode()==KeyEvent.VK_ENTER )
{
action();
}
}
public void keyReleased(KeyEvent e)
{
}
public void keyTyped(KeyEvent e)
{
}
private void action()
{
try
{
if((n.getText().equals(""))|| (b2.getValue().toString().equals(""))||(s.getText().equals("")))
{
error ee1=new error("输入框不能为空");
}
else
{
s1=new Socket(s.getText(),9999);
out1=s1.getOutputStream();
out1.write((b2.getValue()+":"+n.getText()).getBytes());
//System.out.println(b2.getValue()+":"+n.getText());
input=s1.getInputStream();
byte[] by= new byte [1024];
int len=input.read(by);
String mm=new String(by,0,len);
System.out.println(mm);
if(mm.equals("0"))
{
error ee1=new error("此序号已经存在");
}
else
{
System.out.println(mm);
ClientMain cm=new ClientMain(n.getText(),b2.getValue().toString(),s1);
cm.setVisible(true);
this.setVisible(false);
}
}
}
catch(IOException e1)
{
e1.printStackTrace();
error ee1=new error("出现异常");
}
catch(SecurityException e2)
{
e2.printStackTrace();
error ee2=new error("出现异常");
}
}
public static void main(String[] args)
{
new ClientLogin();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -