📄 register.java
字号:
import java.awt.*;
import javax.swing.*;
import java.sql.*;
import java.net.*;
import java.io.*;
import java.awt.event.*;
//import UserData;
//import colorLabel;
/************
vUserName varchar(15) pk
vPassword varchar(20)
vName varchar(15) ,
cCredit varchar(20),
cSex char(5),
iAge int,
vCountry varchar(20),
vState varchar(20),
vEnjoy varchar(20)
************/
public class Register extends JApplet implements ActionListener
{
JTextField tfUser;
JPasswordField pfPass;
JPasswordField pfPassAgain;
JTextField tfName;
JTextField tfCredit;
Choice chSex;
JTextField tfAge;
JTextField tfCountry;
JTextField tfState;
JTextField tfEnjoy;
Label labelMessage;
Container con;
Panel pMain;
GridBagLayout gl;
GridBagConstraints gbc;
Button bnok,bncancel;
UserData data;
colorLabel topDisplay;
PrintStream lnToServer;
String stemp;
BufferedReader fromServer;
// ObjectOutputStream toServer;
Socket socket;
public void init()
{
//String sex[]={"Men","Women"};
topDisplay=new colorLabel("Register Your Own Name And Enjoy Yourself",50);
tfUser=new JTextField(10);
pfPass=new JPasswordField(10);
pfPassAgain=new JPasswordField(10);
tfName=new JTextField(10);
tfCredit=new JTextField(20);
chSex=new Choice();
chSex.addItem("Men");
chSex.addItem("Women");
tfAge=new JTextField(4);
tfCountry=new JTextField(10);
tfState=new JTextField(10);
tfEnjoy=new JTextField(10);
con=this.getContentPane();
con.setLayout(new BorderLayout());
gl=new GridBagLayout();
gbc=new GridBagConstraints();
pMain=new Panel();
pMain.setLayout(gl);
data=new UserData();
labelMessage=new Label("Please fill in your details");
bnok=new Button("OK");
bncancel=new Button("Cancel");
initComp();
addPanel();
initServer();
addAction();
this.setEmpty();
}
public void initComp()
{
gbc.insets=new Insets(1,1,1,1);
// add Label display
String label[]={"UserName","Password","Password Again","RealName"
,"Credit","Sex","Age","Country","State","Enjoy"};
gbc.anchor=GridBagConstraints.EAST;
//gbc.gridwidth=2;
for(int i=0;i<10;i++)
{
addComp(new Label(label[i]),0,i);
}
// add TextField next:::
gbc.anchor=GridBagConstraints.WEST;
int x=2;
addComp(tfUser,x,0);
addComp(pfPass,x,1);
addComp(pfPassAgain,x,2);
addComp(tfName,x,3);
addComp(tfCredit,2,4);
addComp(chSex,x,5);
addComp(tfAge,x,6);
addComp(tfCountry,x,7);
addComp(tfState,x,8);
addComp(tfEnjoy,2,9);
// add button two
addComp(bnok,4,10);
addComp(bncancel,6,10);
//add message
gbc.anchor=GridBagConstraints.CENTER;
//gbc.weightx=1;
}
public void initServer()
{
try
{
String ipAddress=this.getParameter("server");
this.getAppletContext().showStatus("connecting to server: "+ipAddress+":2880");
socket=new Socket(ipAddress,2880);
lnToServer=new PrintStream(socket.getOutputStream());
String local=InetAddress.getLocalHost().toString();
lnToServer.println(local);
fromServer=new BufferedReader(new InputStreamReader(socket.getInputStream()));
//toServer=new ObjectOutputStream(socket.getOutputStream());
//labelMessage.setText("Connecting......");
}
catch(Exception ex)
{
labelMessage.setText("Cannot connect to server...Please check");
}
}
public void addPanel()
{
pMain.setBackground(new Color(200,200,100));
con.add("Center",pMain);
labelMessage.setBackground(Color.pink);
labelMessage.setForeground(Color.blue);
labelMessage.setAlignment(Label.CENTER);
con.add("North",topDisplay);
con.add("South",labelMessage);
}
public void addAction()
{
bnok.addActionListener(this);
bncancel.addActionListener(this);
}
public void addComp(Component obj,int gridx,int gridy)
{
gbc.gridx=gridx;
gbc.gridy=gridy;
pMain.add(obj,gbc);
}
public boolean check()
{
copyData();
//first copy data from applet
if (data.username=="" ||data.password==""||stemp=="" ||data.name=="" ||data.credit=="" ||data.age<1
||data.country=="" ||data.state=="" ||data.enjoy=="" )
return false;
else
return true;
}
//the next code is for Data copy
public void copyData()
{
try
{
data.username=tfUser.getText().trim();
data.password=new String(pfPass.getPassword());
stemp=new String(pfPassAgain.getPassword());
//if ((data.password=pfPass.getText() {return false;}
//if ((stemp=pfPassAgain.getText() {return false;}
data.name=tfName.getText().trim();
data.credit=tfCredit.getText().trim();
data.age=Integer.parseInt(tfAge.getText().trim());
data.sex=chSex.getSelectedItem();
data.country=tfCountry.getText().trim() ;
data.state=tfState.getText().trim();
data.enjoy=tfEnjoy.getText().trim();
}
catch(Exception ue)
{
labelMessage.setText("Form filling in error :"+ue);
}
}
public void sendAdapter()
{
//test useing
//System.out.println(stemp);
copyData();
System.out.println(data.username +","+data.password +","+stemp +","+data.name
+","+data.credit +","+data.age +","+data.country +","+data.state +","+data.enjoy);
if (!check())
{
labelMessage.setText("User Details Can't be Empty !");
}
else
{
if (!(stemp.equals(data.password)))
{
labelMessage.setText("The two password filled in is diffient");
}
else
{
try
{
//data.age=Integer.parseInt(tfAge.getText().trim());
//toServer.writeObject(data);
lnToServer.println(data.username);
lnToServer.println(data.password);
lnToServer.println(data.name);
lnToServer.println(data.credit);
lnToServer.println(data.sex);
lnToServer.println(data.age);
lnToServer.println(data.country);
lnToServer.println(data.state);
lnToServer.println(data.enjoy);
//test
System.out.println(data.username +","+data.password +","+stemp +","+data.name
+","+data.credit +","+data.age +","+data.country +","+data.state +","+data.enjoy);
String smsg=fromServer.readLine();
labelMessage.setText(smsg);
if (smsg.equals("User Register Successful"))
{
setEmpty();
}
}
catch(Exception ioe)
{
labelMessage.setText(String.valueOf(ioe));
}
}
}
}
/*
tfUser;
JPasswordField pfPass;
JPasswordField pfPassAgain;
JTextField tfName;
JTextField tfCredit;
Choice chSex;
JTextField tfAge;
JTextField tfCountry;
JTextField tfState;
JTextField tfEnjoy;
Label labelMessage;
*/
public void setEmpty()
{
tfUser.setText("");
pfPass.setText("pass");
pfPassAgain.setText("pass");
tfName.setText("");
tfCredit.setText("");
tfAge.setText("0");
tfCountry.setText("");
tfState.setText("");
tfEnjoy.setText("");
}
public void actionPerformed(ActionEvent avt)
{
Button bn=(Button)avt.getSource();
if(bn==bnok)
{
sendAdapter();
}
else
{
setEmpty();
labelMessage.setText("Please fill in your details");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -