⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 initial.java.bak

📁 这是一个简单的聊天程序
💻 BAK
📖 第 1 页 / 共 2 页
字号:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.util.*;
import javax.swing.event.*;
public class initial extends JFrame implements ActionListener,Runnable,KeyListener
{
JPanel jp;
JLabel jl;
JTextField jtf,jtf1;
JButton jb_c,jb_q;
String ip;
Thread thr;
int key;
public initial()
{
super("IP connect");
jp=new JPanel();
setContentPane(jp);
jp.setBackground(/*new Color(60,217,173)*/Color.yellow);
setDefaultCloseOperation(EXIT_ON_CLOSE);
this.addKeyListener(this);

jl=new JLabel("IP Address:");
jtf=new JTextField(10);
jtf1=new JTextField(20);
jb_c=new JButton("确定");
jb_c.setMnemonic('c');
jb_c.addActionListener(this);
jb_q=new JButton("取消");
jb_q.setMnemonic('q');
jb_q.addActionListener(this);
jb_c.setBackground(new Color(255,128,64));
jb_q.setBackground(new Color(255,128,64));

jp.add(jl);jp.add(jtf);
jp.add(jb_c);jp.add(jb_q);jp.add(jtf1);

setSize(200,100);
setVisible(true);
//setResizable(false);
setLocation(400,300);
thr=new Thread(this);
thr.start();
}
public void run()
{
Date d;
try
{
while(true)
{
d=new Date();
// String time=d.getHours()+":"+d.getMinutes()+":"+d.getSeconds();
jtf1.setText(""+" "+d);
thr.sleep(1000);
}
}
catch(Exception e){}
}
public void keyPressed(KeyEvent e)
{

key=e.getKeyCode();//////////////////////////////111111
if(key==KeyEvent.VK_ALT)////////////////keyevent.vk_alt/////////////////********
JOptionPane.showMessageDialog(null,"67");
//send();

}
public void keyReleased(KeyEvent e){}
public void keyTyped(KeyEvent e){}
public void actionPerformed(ActionEvent ae)
{
Object o=ae.getSource();
if(o==jb_c)
{
ip=jtf.getText().trim();
new dl(ip);
dispose();
}
if(o==jb_q)
{
jtf.setText("");
jtf.grabFocus();
}
}
public static void main(String[] args)
{
new initial();
}
}
class Mess implements Serializable
{
public String name,pwd;
}
class dl extends JFrame implements ActionListener,Runnable
{
JPanel jp;
JLabel jl_u,jl_m;
JPasswordField jpf;
JTextField jtf_u;
JButton jb_d,jb_z,jb_q;
Thread t;
Socket sk;
String ip;
GridBagLayout gbl;
GridBagConstraints gbc;
Icon i;

public dl(String ip)
{
super("登陆");
this.ip=ip;
jp=new JPanel();
gbl=new GridBagLayout();
gbc=new GridBagConstraints();
jp.setLayout(gbl);
jp.setBackground(new Color(60,217,173));
setContentPane(jp);
setDefaultCloseOperation(EXIT_ON_CLOSE);

jl_u=new JLabel("用户名");
jl_m=new JLabel("密码");
//i=new ImageIcon("f:/04912111/project/button.gif");
jtf_u=new JTextField(10);
jpf=new JPasswordField(10);
jb_d=new JButton("登陆");
jb_d.setMnemonic('d');
jb_d.setBackground(new Color(40,210,164));
jb_d.addActionListener(this);
jb_z=new JButton("注册");
jb_z.setBackground(new Color(40,210,164));
jb_z.addActionListener(this);
jb_q=new JButton("取消");
jb_q.setBackground(new Color(40,210,164));
jb_q.addActionListener(this);


set(1,1,1,1,jl_u);
set(2,1,2,1,jtf_u);
set(1,2,1,1,jl_m);
set(2,2,2,1,jpf);
set(1,3,1,1,jb_d);
set(2,3,1,1,jb_z);
set(3,3,1,1,jb_q);

/*jp.add(jl_u);jp.add(jtf_u);
jp.add(jl_m);jp.add(jpf);
jp.add(jb_d);jp.add(jb_z);jp.add(jb_q);*/

setSize(200,200);
setVisible(true);
setLocation(400,300);
try
{
sk=new Socket(ip,1001);
}
catch(Exception e)
{
System.out.println("1"+e.getMessage());
}
System.out.println("1");
t=new Thread(this);
t.start();
}
public void run()
{ 
try
{
String flag;
while(true)
{
DataInputStream dis=new DataInputStream(sk.getInputStream());
flag=dis.readUTF();
if(flag.equals("s"))
{
JOptionPane.showMessageDialog(null,"legal user");
new chat(ip);/////////
dispose();
}
if(flag.equals("f"))
{
JOptionPane.showMessageDialog(null,"ilegal user try again");
jtf_u.setText("");
jpf.setText("");
jtf_u.grabFocus();
}
} 
}
catch(Exception e)
{
}
finally
{
System.out.println("close");
try
{
sk.close();
}
catch(Exception e){}
}
}
public void actionPerformed(ActionEvent ae)
{
Object o=ae.getSource();
if(o==jb_d)
{
Mess m=new Mess();
m.name=jtf_u.getText().trim();
m.pwd=new String(jpf.getPassword());
ObjectOutputStream oos;
try
{
oos=new ObjectOutputStream(sk.getOutputStream());
oos.writeObject(m);////////////////
System.out.println("2");
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null,e.getMessage()+"156");
}

}
if(o==jb_z)
{
new zc(ip);
dispose();
}
if(o==jb_q)
{
jtf_u.setText("");
jpf.setText("");
jtf_u.grabFocus();
}
}
public void set(int i,int j,int k,int r,Component con)
{
gbc.anchor=GridBagConstraints.WEST;
gbc.gridx=i;gbc.gridy=j;
gbc.gridwidth=k;gbc.gridheight=r;
Insets in=new Insets(5,1,8,1);
gbc.insets=in;
gbl.setConstraints(con,gbc);
jp.add(con);
}
}
class document implements Serializable
{
public String name,pwd/*,age,old,tel,num,sex,si*/;
};
class zc extends JFrame implements Runnable,ActionListener////////////注册类
{
JPanel jp;
JLabel j1,j2,j3,j4,j5,j6,j7,j8,j9,j10,j11,j12,j13,j14,j15,j16,j17,j18,j19,j20,j21;
JTextField t1,t4,t5,t6,t7,t8,t9;
JRadioButton b1,b2;
JComboBox c1,c2,c3,c4;
JPasswordField t2,t3;
JButton o1,o2;
GridBagLayout gbl;
GridBagConstraints gbc;
String pw,nm;
TextArea ta;
Socket sk;
String ip;
Thread th;

public zc(String ip)
{
super("注册窗口");
this.ip=ip;
jp=new JPanel();
jp.setBackground(new Color(60,217,173));
setContentPane(jp);
setDefaultCloseOperation(EXIT_ON_CLOSE);
ta=new TextArea("",10,15,ta.SCROLLBARS_NONE);
ta.setBackground(new Color(168,249,210));

j1=new JLabel("用户名");
j2=new JLabel("密码");
j3=new JLabel("确认密码");
j4=new JLabel("姓名");
j5=new JLabel("年龄");
j6=new JLabel("年");
j7=new JLabel("月");
j8=new JLabel("日");
j9=new JLabel("地址");
j10=new JLabel("联系电话");
j11=new JLabel("证件类");
j12=new JLabel("号码");
j13=new JLabel("性别");
j14=new JLabel("*不准使用/*%#等符号");
j15=new JLabel("*输入密码长度不能小于6");
j16=new JLabel("*请重新输入密码");
j17=new JLabel("*必填部分");
j18=new JLabel("*必填部分");
j19=new JLabel("*必填部分");
j20=new JLabel("注册资料");
j21=new JLabel("个人资料");
Font o=new Font("隶书",Font.BOLD,25);
j20.setFont(o);
j1.setForeground(Color.red);
j2.setForeground(Color.red);
j3.setForeground(Color.red);
j4.setForeground(Color.red);
j5.setForeground(Color.red);
j6.setForeground(Color.red);
j7.setForeground(Color.red);
j8.setForeground(Color.red);
j9.setForeground(Color.red);

j14.setForeground(new Color(0,130,0));
j15.setForeground(new Color(0,130,0));
j16.setForeground(new Color(0,130,0));
j17.setForeground(new Color(0,130,0));
j18.setForeground(new Color(0,130,0));
Font g=new Font("宋体",Font.BOLD,12);
j1.setFont(g);j2.setFont(g);
j3.setFont(g);j4.setFont(g);
j5.setFont(g);j6.setFont(g);
j7.setFont(g);j8.setFont(g);
j9.setFont(g);j10.setFont(g);
j11.setFont(g);j12.setFont(g);
j13.setFont(g);j21.setFont(g);
Font t=new Font("新宋体",Font.BOLD,12);
j14.setFont(t);j15.setFont(t);
j16.setFont(t);j17.setFont(t);
j18.setFont(t);j19.setFont(t);



b1=new JRadioButton("男");
b1.setBackground(new Color(102,244,176));
b2=new JRadioButton("女");
b2.setBackground(new Color(102,244,176));
ButtonGroup b=new ButtonGroup();
b.add(b1); b.add(b2);

o1=new JButton("确认");
o1.setBackground(new Color(27,228,163));
o1.addActionListener(this);
o2=new JButton("返回");
o2.setBackground(new Color(27,228,163));
o2.addActionListener(this);
t1=new JTextField(10);
t2=new JPasswordField(10);
t3=new JPasswordField(10);
t4=new JTextField(10);
t5=new JTextField(10);
t6=new JTextField(10);
t7=new JTextField(10);
t8=new JTextField(10);

c1=new JComboBox();
for(int i=1980;i<2000;i++)
{
c1.addItem(""+i);
}
c1.setBackground(Color.white);
c2=new JComboBox();
for(int j=1;j<13;j++)
{
c2.addItem(""+j);
}
c2.setBackground(Color.white);
c3=new JComboBox();
for(int k=1;k<32;k++)
{
c3.addItem(""+k);
}
c3.setBackground(Color.white);
String pa[]={"身份证","学生证","军官证"};
c4=new JComboBox(pa);
c4.setBackground(Color.white);

gbc=new GridBagConstraints();
gbl=new GridBagLayout();
jp.setLayout(gbl);


// set(1,11,3,3,ta); 
set(4,0,2,1,j20);
set(0,1,1,1,j1);
set(1,1,3,1,t1);
set(6,1,2,1,j14);
set(0,2,1,1,j2);
set(1,2,3,1,t2);
set(6,2,2,1,j15);
set(0,3,1,1,j3);
set(1,3,3,1,t3);
set(6,3,2,1,j16);
set(0,4,1,1,j4);
set(1,4,3,1,t4);
set(6,4,2,1,j17);
set(0,5,1,1,j5);
set(1,5,1,1,c1);
set(3,5,1,1,j6);
set(4,5,1,1,c2);
set(5,5,1,1,j7);
set(6,5,1,1,c3);
set(7,5,1,1,j8);
set(0,6,1,1,j9);
set(1,6,3,1,t5);
set(0,7,1,1,j10);
set(1,7,3,1,t6);
set(6,7,2,1,j18);
set(0,8,1,1,j11);
set(1,8,1,1,c4);
set(4,8,1,1,j12);
set(5,8,2,1,t7);
set(0,9,1,1,j13);
set(1,9,1,1,b1);
set(4,9,1,1,b2);
// set(0,11,1,1,j21);
//set(1,10,3,3,ta); 
set(4,13,1,1,o1);
set(5,13,1,1,o2);

setSize(550,550);
setVisible(true);
// setResizable(false);
setLocation(400,100);

try
{
sk=new Socket(ip,1002);
}
catch(Exception e)
{
}
th=new Thread(this);
th.start();
}
public void run()
{
DataInputStream dis;
String hear;
try
{
System.out.println("357");
while(true)
{
dis=new DataInputStream(sk.getInputStream());
hear=dis.readUTF();
if(hear.equals("regist finish"))
{
JOptionPane.showMessageDialog(null,"^_^注册成功,单击返回登陆");
}
if(hear.equals("regist error"))
{
JOptionPane.showMessageDialog(null,"注册失败");
}
}
}
catch(Exception e)
{
System.out.println("366");
}
}
public void set(int i,int j,int k,int r,Component com)
{

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -