📄 login.java
字号:
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
import javax.swing.border.*;
import java.net.*;
public class login extends JFrame implements ActionListener
{
JFrame frame = new JFrame();
JPanel contentPane;
JPanel loginpanel;
TextField name_txt; //用户名
JPasswordField password_txt; //密码
JButton log_in ,cancel,apply;
JLabel label1,label2;
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); //获得屏幕尺寸
Socket soc=null; //定义连接套接字
DataInputStream dis=null; //定义用来实现客户端接受服务器数据的输入流
DataOutputStream dos=null; //定义用来实现从客户端发送数据到服务器的输出流
chatfram login1=null;
applynum applyframe=null;
public login()
{
contentPane = (JPanel) frame.getContentPane();
contentPane.setLayout(null);
contentPane.setBackground(new Color(240,248,255));
loginpanel = new JPanel();
loginpanel.setLayout(null);
loginpanel.setBounds(16,16,300,200);
loginpanel.setBackground(new Color(240,248,255));
frame.setTitle("PP用户登录");
frame.setResizable(false);
label1 = new JLabel();
label1.setText("PP号码");
label1.setBounds(30,40,60,25);
loginpanel.add(label1);
label2 = new JLabel();
label2.setText("PP密码");
label2.setBounds(30,90,60,25);
loginpanel.add(label2);
log_in = new JButton(); //登录按钮
log_in.setLabel("登录");
log_in.setToolTipText("点击后连接服务器验证信息");
log_in.setBounds(80,130,65,25);
log_in.setBackground(new Color(169,215,255));
log_in.setBorder(new BevelBorder(BevelBorder.RAISED));
log_in.addActionListener(this);
loginpanel.add(log_in);
cancel = new JButton(); //取消按钮
cancel.setLabel("取消");
cancel.setToolTipText("点击退出程序");
cancel.setBounds(150,130,65,25);
cancel.setBackground(new Color(169,215,255));
cancel.setBorder(new BevelBorder(BevelBorder.RAISED));
cancel.addActionListener(this);
loginpanel.add(cancel);
apply = new JButton(); //注册按钮
apply.setLabel("申请号码");
apply.setToolTipText("点击进入号码申请界面");
apply.setBounds(218,38,65,25);
apply.setBackground(new Color(169,215,255));
apply.setBorder(new BevelBorder(BevelBorder.RAISED));
apply.addActionListener(this);
loginpanel.add(apply);
name_txt = new TextField(); //帐号输入区域
name_txt.setText(null);
name_txt.setBounds(80,40,120,20);
loginpanel.add(name_txt);
password_txt = new JPasswordField(); //密码输入区域
password_txt.setText(null);
password_txt.setBounds(80,90,120,20);
password_txt.setBorder(new BevelBorder(BevelBorder.LOWERED));
loginpanel.add(password_txt);
contentPane.add(loginpanel);
frame.setSize(330,250);
frame.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
disconnect();
System.exit(0);
}
});
frame.setLocation(screenSize.width/2-175,screenSize.height/2-125); //使得窗口居中显示
frame.setVisible( true );
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==log_in)
{
try{
soc=new Socket("localhost",1234); //使用端口1234实例化一个本地套接字
System.out.println(soc); //在控制台打印实例化的结果
dis=new DataInputStream(soc.getInputStream()); //将dis指向soc的输入流
dos=new DataOutputStream(soc.getOutputStream()); //将dos指向soc的输出流
}
catch(Exception event){}
if(name_txt.getText().equals("")==false&&password_txt.getText().equals("")==false)
{
try
{
dos.writeUTF("Confim:"+name_txt.getText()+":"+password_txt.getText()+":"+InetAddress.getLocalHost().toString());
String confim=dis.readUTF();
System.out.println(confim);
String[] shortString=confim.split(":");
String cnum=shortString[0];
String cps=shortString[1];
if(shortString[0].equals("true")&&shortString[1].equals("true"))
{
login1=new chatfram(name_txt.getText(),dis,dos);
frame.setVisible(false);
}
else if(shortString[0].equals("false")&&shortString[1].equals("false"))
{
dos.writeUTF("QUIT");
JFrame ff=new JFrame();
JOptionPane.showMessageDialog(ff,"此PP帐号不存在!","警告",JOptionPane.WARNING_MESSAGE);
}
else if(shortString[0].equals("true")&&shortString[1].equals("false"))
{
dos.writeUTF("QUIT");
JFrame ff=new JFrame();
JOptionPane.showMessageDialog(ff,"您输入的密码错误,请重新输入!","警告",JOptionPane.WARNING_MESSAGE);
}
}
catch (Exception event){}
}
else if(name_txt.getText().equals("")==true||password_txt.getText().equals("")==true)
{
JFrame ff=new JFrame();
JOptionPane.showMessageDialog(ff,"帐号或者密码不能为空,请正确填写!","警告",JOptionPane.WARNING_MESSAGE);
}
else
{
JFrame ff=new JFrame();
JOptionPane.showMessageDialog(ff,"帐号或者密码错误,请重新输入!","警告",JOptionPane.WARNING_MESSAGE);
}
}//end of if
else if(e.getSource()==cancel)
{
System.exit(0);
}
else if(e.getSource()==apply)
{
frame.setVisible(false);
applyframe=new applynum(frame);
}
}
public void disconnect() //客户端点击断开连接要运行的方法
{
if(soc!=null)
{
try
{
dos.writeUTF("QUIT"); //用输出流发送QUIT信息通知服务器断开此次通信
soc.close(); //关闭套接字
soc=null;
}
catch(IOException e)
{
System.out.println("Error:"+e);
}
}// end of if
}
public static void main(String[] args)
{
login cc=new login();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -