📄 land.java
字号:
package MyPackage;
import java.awt.*;
import java.net.*;
import java.io.*;
import java.awt.event.*;
import javax.swing.*;
import MyPackage.*;
import MyPackage.GuiRegist;
import MyPackage.GuiClient;
//import MyPackage.Main;
public class Land{//登陆界面
public static Socket socket2=null;
public static TextField t1=null;
public static TextField t2=null;
public static Frame f=null;
Panel p1=null;
Panel p2=null;
Panel p21=null;
Panel p22=null;
Panel p3=null;
Panel p4=null;
Panel p5=null;
Panel p6=null;
Panel p7=null;
//按钮
Button b1=null;
Button b2=null;
Button b3=null;
Button b4=null;
//标签
Label l1=null;
Label l2=null;
Label l3=null;
Label l4=null;
Label l5=null;
Label l6=null;
Checkbox c1=null;
public Land(){
try{
socket2=new Socket("172.40.83.44",6700);
}catch(Exception e){}
f=new Frame("GUI多人聊天 - 用户登陆 ");
//面板
p1=new Panel();
p2=new Panel();
p21=new Panel();
p22=new Panel();
p3=new Panel();
p4=new Panel();
p5=new Panel();
p6=new Panel();
p7=new Panel();
//按钮
b1=new Button("R");
b2=new Button("登陆(L)");
b3=new Button("注册账号(R)");
b4=new Button("R");
//标签
l1=new Label("用户登陆");
l2=new Label("账号");
l3=new Label("密码");
l4=new Label("忘记密码");
l5=new Label("R");
l6=new Label("R");
//单行文本框
t1=new TextField();
t2=new TextField();
//复选框
c1=new Checkbox("自动登陆(A)",false);
//面板1(Panel 1)
t1.setEchoChar('*');
b2.addActionListener(new AscendsListener());//侦听“登陆"的事件
b3.addActionListener(new RegisterListener());//侦听"注册"事件
f.addWindowListener(new MyWindowsListener());
p1.setLayout(new FlowLayout(FlowLayout.LEFT));
//p1.setBackground(Color.white);
l5.setBackground(Color.lightGray);
p1.add(l5);
p1.add(l1);
//面板2(Panel 2它又由两个面板11、12组合而成)
p21.setLayout(new GridLayout(2,1,5,10));
p21.add(l2);
p21.add(l3);
p22.setLayout(new GridLayout(2,1,5,10));
p22.add(t2);
t1.setColumns(25);
p22.add(t1);
p2.setLayout(new FlowLayout(FlowLayout.LEFT,5,0));
p2.add(p21);
p2.add(p22);
//面板3(Panel 3)
p3.setLayout(new FlowLayout(FlowLayout.LEFT,30,0));
p3.add(c1);
//面板4(Panel 4)
p4.setLayout(new FlowLayout(FlowLayout.LEFT,5,0));
l6.setBackground(Color.lightGray);
p4.add(l6);
p4.add(l4);
//面板5(Panel 5)
p5.setLayout(new FlowLayout(FlowLayout.CENTER,30,2));
p5.add(b2);
p5.add(b3);
//frame界面
p6.setLayout(new BorderLayout(40,5));
p6.add("North",p1);
p6.add("Center",p2);
p6.add("South",p3);
p7.setLayout(new BorderLayout());
p7.add("North",p6);
p7.add("Center",p4);
p7.add("South",p5);
f.setLayout(new FlowLayout(FlowLayout.CENTER));
f.add(p7);
f.setSize(350,220);
f.setLocation(300,300);
f.setBackground(Color.gray);
f.setResizable(false);
f.setVisible(true);
}
/*public static void main(String[] args)
{
new Land();
}*/
}
class MyWindowsListener extends WindowAdapter{
public void windowClosing(WindowEvent e){
System.exit(1);
}
};
class RegisterListener implements ActionListener{
public void actionPerformed(ActionEvent e){
Land.f.setVisible(false);
new GuiRegist();
}
}
class AscendsListener implements ActionListener{
public void actionPerformed(ActionEvent e){
new SR();
}
}
class SR{
String s;
BufferedReader is=null;
PrintWriter os=null;
public SR(){
String line=null;
String s1,s2;
System.out.println("正式登陆");
try{
is=new BufferedReader(new InputStreamReader(Land.socket2.getInputStream()));
os=new PrintWriter(Land.socket2.getOutputStream());
}catch(Exception e1){System.out.println("异常1:"+e1);}
System.out.println("输出到字节流");
s1=Land.t2.getText();//取得用户名
s2=Land.t1.getText();//取得密码
if((s1.toLowerCase()).equals("everybody")){//标识所有用户不能用everybody这类用户名登陆
new Message1("3");
return;
}
s=s1+":"+s2;
System.out.println("用户名:密码"+s);
os.println(s);
os.flush();
System.out.println("输出结束");
while(true){
try{
line=is.readLine();
}catch(Exception e1){
System.out.println("异常2:"+e1);
}
System.out.println("输出:"+line);
if(line==null)break;
if(line.equals("0")){
System.out.println("用户名或密码错误");
new Message1("0");
break;
}
else if(line.equals("1")){
System.out.println("正常登陆");
new Message1("1");
// System.out.println("UserName.string"+UserName.string);
//UserName.string=s1;
// System.out.println("UserName.String:"+UserName.string);
try{
Land.socket2.close();
Land.f.setVisible(false);//设置此窗体不可见
}catch(IOException e2){}
new GuiClient(s1);//用用户名来实例化客户端。
break;
}
else if(line.equals("2")){
System.out.println("该用户已经在线");
new Message1("2");
return;
}
}
}
}
class Message1{
public Message1(String s){
int type=JOptionPane.PLAIN_MESSAGE;
String title="Message Dialog";
String message="";
if(s.equals("0")){
type=JOptionPane.WARNING_MESSAGE;
message="用户名或密码错误";
}
else if(s.equals("1")){
type=JOptionPane.INFORMATION_MESSAGE;
message="登陆成功\n用户名:"+Land.t2.getText()+"\n密码:"+Land.t1.getText();
}
else if(s.equals("2")){
type=JOptionPane.WARNING_MESSAGE;
message="该用户已经在线,请重新登陆";
}
else if(s.equals("3")){
type=JOptionPane.WARNING_MESSAGE;
message="您不能用该用户名登陆,您的权限不够";
}
JOptionPane.showMessageDialog(Land.f,message,title,type);
}
};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -