📄 login.java
字号:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
public class Login extends JFrame implements ActionListener
{
JButton b1,b2,dlgb1;
JTextField text;
JPanel pane;
JLabel l1,dlgl1;
JComboBox cb1;
Dialog dlg;
Container con=this.getContentPane();
Mm mm;
public Login()
{
super("Title");
setSize(200,120);
String[] s=new String[2];
s[0]="男";
s[1]="女";
cb1=new JComboBox(s);
cb1.setBackground(new Color(254,227,224));
l1=new JLabel("注册名:");
dlgl1=new JLabel(" 登陆成功! ");
dlgl1.setFont(new Font("宋体", Font.BOLD, 15));
b1=new JButton("确定");
b1.setBackground(new Color(254,227,224));
b2=new JButton("退出");
b2.setBackground(new Color(254,227,224));
dlgb1=new JButton("确定");
dlgb1.setBackground(new Color(254,227,224));
text=new JTextField(10);
dlg=new Dialog(this,"登陆成功",true);
pane=new JPanel();
b1.addActionListener(this);
b2.addActionListener(this);
dlgb1.addActionListener(this);
dlg.setSize(200,100);
dlg.add(dlgl1);
dlg.add(dlgb1);
dlg.setLayout(new FlowLayout());
dlg.setBackground(new Color(237,240,241));
pane.add(l1);
pane.add(text);
pane.add(cb1);
pane.add(b1);
pane.add(b2);
pane.setLayout(new FlowLayout());
pane.setBackground(new Color(237,240,241));
con.add(pane);
setLocation(400,300);
mm=new Mm();
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
show();
}
public static void main(String[] args)
{
new Login();
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b1)
{
if(mm.connect(text.getText(),cb1.getSelectedIndex()))
{
this.dispose();
new Frame1().show();
dlg.show();
}
else
{
this.dispose();
mm.dlg();
}
}
if(e.getSource()==b2)
{
mm.exit();
}
if(e.getSource()==dlgb1)
{
mm.updata("OK");
dlg.dispose();
}
}
}
class Mm
{
String text,sex,ip,str;
void exit()
{
System.exit(0);
}
////////////////////////////////////////////
boolean connect(String text,int sex)
{
// System.out.print(text+" "+sex);
try
{
if(sex==0)
{
str=text+"(男)";
}
else
{
str=text+"(女)";
}
DatagramSocket ds1=new DatagramSocket();
DatagramPacket dp1=new DatagramPacket(str.getBytes(),str.getBytes().length,InetAddress.getByName("127.0.0.1"),8500);
ds1.send(dp1);
ds1.close();
DatagramSocket ds2=new DatagramSocket(8501);
byte[] buf=new byte[5];
DatagramPacket dp2=new DatagramPacket(buf,5);
ds2.receive(dp2);
str=new String(dp2.getData(),0,dp2.getLength());
System.out.println(str);
ds2.close();
}
catch(Exception e)
{
System.out.println(e);
}
finally
{
if(str.equals("true"))
return true;
else
return false;
}
}
///////////////////////////////////////////
void strsend(String str)
{
try
{
DatagramSocket ds3=new DatagramSocket();
DatagramPacket dp3=new DatagramPacket(str.getBytes(),str.getBytes().length,InetAddress.getByName("127.0.0.1"),8000);
ds3.send(dp3);
ds3.close();
}
catch(Exception e2)
{
e2.printStackTrace();
}
}
///////////////////////////////////////////
void dlg()
{
int rr=JOptionPane.showConfirmDialog(null,"这个用户已存在","确定窗口",JOptionPane.CLOSED_OPTION);
}
///////////////////////////////////////////
void updata(String updata)
{
try
{
DatagramSocket ds4=new DatagramSocket();
DatagramPacket dp4=new DatagramPacket(updata.getBytes(),updata.getBytes().length,InetAddress.getByName("127.0.0.1"),9500);
ds4.send(dp4);
ds4.close();
System.out.println(updata+" Sendover");
}
catch(Exception e2)
{
e2.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -