📄 mainpane.java
字号:
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import java.net.*;
import java.io.*;
import java.sql.*;
import colorLabel;
import toyPane;
import logPanel;
import toyData;
import UserData;
import toyList;
public class MainPane extends JApplet
implements ActionListener
{
Container con;
// the component of user making
Socket meClient;
BufferedReader fromServer;
PrintStream toServer;
ObjectInputStream toyfromServer;
// ObjectOutputStream ordertoServer;
colorLabel clheader;
toyPane tpshow;
logPanel lplog;
GridBagLayout gl;
GridBagConstraints gbc;
CardLayout cl;
BorderLayout bl;
colorLabel jtaDesc;
//JPanel plog;
JPanel plbm;//panel left's bottom panel
Label labelMsg;
//sub main panel
Panel pl,pr,psr;
URL urlpath;
//toyData toydata;
toyList toydata;
UserData userdata;
//order component ::::::::::::
Button bncompute;
Label labelPay;
Choice chTraff;
Choice chCasing;
TextArea taAddress;
TextArea taWord;
Label labelSubmit;
//order defined
Button bnSubmit;
public void init()
{
varComp();
//System.out.println("varcomp");
addPanel();
//System.out.println("addPanel");
paintColor();
//System.out.println("paintcolor");
addAction();
//System.out.println("addAction");
//new Frame("ncfire").show();
}
//var all components of the applet
public void varComp()
{
con=getContentPane();
urlpath=this.getCodeBase();
gl=new GridBagLayout();
gbc=new GridBagConstraints();
clheader=new colorLabel("Toy Universe",40);
System.out.println("ok,the applet end here is well");
lplog=new logPanel();
labelMsg=new Label("Enjoy Yourself !!");
labelMsg.setAlignment(Label.CENTER);
userdata=new UserData();
//plog=new JPanel();
plbm=new JPanel();
bl=new BorderLayout();
cl=new CardLayout();
try
{
String serverAddress=this.getParameter("server");
meClient=new Socket(serverAddress,1880);
this.getAppletContext().showStatus("connecting to Server:"+serverAddress+":1880");
fromServer=new BufferedReader(new InputStreamReader(meClient.getInputStream()));
toServer=new PrintStream(meClient.getOutputStream());
toyfromServer=new ObjectInputStream(meClient.getInputStream());
//ordertoServer=new ObjectOutputStream(meClient.getOutputStream());
}
catch(Exception eio)
{
fail(eio,"i/o error");
}
//getToydata::::::::::::::
try
{
//labelMsg.setText(fromServer.readLine());
InetAddress localAddress=InetAddress.getLocalHost();
String ipAddress=localAddress.toString();
toServer.println(ipAddress);
toydata=(toyList)toyfromServer.readObject();
//:test:::::::
System.out.println("toydata read ok");
tpshow=new toyPane(urlpath,toydata);
System.out.println("tpshow init ok");
}
catch(Exception ecp)
{
fail(ecp,"toyData :");
System.out.println(ecp);
}
}
//public void getToydata()
//{
public void fail(Exception ept,String str)
{
labelMsg.setText(str+":"+ept);
}
public void addPanel()
{
con.setLayout(gl);
//this.setLayout(gl);
Panel pl=new Panel();
Panel pr=new Panel();
//set color of the two panel;
pl.setBackground(Color.red);
pr.setBackground(Color.green);
gbc.fill=GridBagConstraints.BOTH;
gbc.weightx=1;
gbc.weighty=1;
//add pl and pr
gbc.gridx=0;
//this.add(pl,gbc);
con.add(pl,gbc);
gbc.gridx=1;
gbc.weightx=4;
con.add(pr,gbc);
pl.setLayout(new GridLayout(3,1));
//add the sub panel of pl
Panel p[]=new Panel[3];
for(int i=0;i<3;i++)
{
p[i]=new Panel();
pl.add(p[i]);
switch(i)
{
case 0:
{
try{
URL imgpath=new URL(urlpath,"../image/headerin.gif");
Icon img=new ImageIcon(imgpath);
JLabel ltop=new JLabel(img);
ltop.setBackground(Color.black);
Label ltxt=new Label("Toy Universe");
ltxt.setBackground(Color.green);
ltxt.setForeground(Color.red);
ltxt.setAlignment(Label.CENTER);
p[i].setBackground(Color.black);
p[i].setLayout(bl);
p[i].add("Center",ltop);
p[i].add("South",ltxt);
}
catch(Exception e)
{}
break;
}
case 1:
{
try{
p[i].setLayout(gl);
p[i].add(lplog,gbc);
//JLabel lgif=new JLabel(ipic);
//Panel pgif=new Panel();
//pgif.add(lgif);
//lplog.plged.add("Center",pgif);
}
catch(Exception eimg)
{}
break;
}
case 2:
{
//add the bottom of the left panel now is empty
p[i].setBackground(Color.black);
p[i].setLayout(gl);
jtaDesc=new colorLabel("!!!Toys New!!!",12);
gbc.gridy=0;
p[i].add(jtaDesc,gbc);
//jtaDesc.setEditable(false);
Panel pfh=new Panel();
//pfh.setLayout(bl);
URL urlipf;
try{
urlipf=new URL(urlpath,"../image/flashtoy.gif");
Icon ipicflash=new ImageIcon(urlipf);
JLabel labeltoyfh=new JLabel(ipicflash);
gbc.gridy=1;
pfh.add(labeltoyfh);
p[i].add(pfh,gbc);
}
catch(Exception ure)
{
System.out.println(ure);
}
break;
}
}
}
System.out.println(" next we'll add new components of pr");
//next we'll add new components of pr
pr.setLayout(gl);
gbc.gridy=0;
gbc.weighty=1;
pr.add(clheader,gbc);
Panel pshow=new Panel();
gbc.gridy=1;
gbc.weighty=4;
pr.add(pshow,gbc);
System.out.println("//the sub panel of pshow,the main show of the applet");
//the sub panel of pshow,the main show of the applet
Panel psl=new Panel();
psr=new Panel();
//paintcolor for last two panel
psl.setBackground(Color.cyan);
psr.setBackground(Color.orange);
//add the Order Panel
bnSubmit=new Button("Submit");
bnSubmit.setEnabled(false);
labelSubmit=new Label();
psr.setLayout(new GridLayout(3,1));
//psr.setEnabled(false);
Panel psrs[]=new Panel[3];
/*
Button bncompute;
Label labelPay;
Choice chTraff;
Choice chCasing;
TextArea taAddress;
TextArea taWord;
*/
System.out.println("now add the rightest Panel's component :::::::::::::: recently edit");
//now add the rightest Panel's component :::::::::::::: recently edit
bncompute=new Button("Compute Payment");
labelPay=new Label("Payment Display");
labelPay.setAlignment(Label.CENTER);
chTraff=new Choice();
chCasing=new Choice();
chTraff.addItem("Train");
chTraff.addItem("Ship");
chTraff.addItem("Air");
chCasing.addItem("Yes");
chCasing.addItem("No");
taAddress=new TextArea(3,8);
taWord=new TextArea(3,8);
taAddress.setText("");
taWord.setText("");
for(int k=0;k<3;k++)
{
psrs[k]=new Panel();
psr.add(psrs[k]);
gbc.insets=new Insets(1,1,1,1);
switch(k)
{
case 0:
{
psrs[k].setLayout(gl);
//GridBagConstraints gbc=new GridBagConstraints();
gbc.weightx=0;
gbc.weighty=0;
gbc.gridy=0;
psrs[k].add(bncompute,gbc);
gbc.gridy=1;
psrs[k].add (labelPay,gbc);
gbc.gridy=2;
psrs[k].add(bnSubmit,gbc);
gbc.gridy=3;
psrs[k].add(labelSubmit,gbc);
//gbc.weightx=1;
//gbc.weighty=1;
break;
}
case 1:
{
psrs[k].setLayout(gl);
gbc.gridx=0; gbc.gridy=0;
psrs[k].add (new Label("Traffic"),gbc);
gbc.gridx=1;
psrs[k].add (chTraff,gbc);
gbc.gridx=0; gbc.gridy=1;
psrs[k].add (new Label("Casing"),gbc);
gbc.gridx=1;
psrs[k].add (chCasing,gbc);
break;
}
case 2:
{
psrs[k].setLayout(gl);
gbc.gridy=0;
psrs[k].add (new Label("Enter The Address You Want:"),gbc);
gbc.gridy ++;
psrs[k].add (taAddress,gbc);
gbc.gridy ++;
psrs[k].add (new Label("Enter The Word You Want To Say"),gbc);
gbc.gridy ++;
psrs[k].add(taWord,gbc);
gbc.weightx=1;
gbc.weighty=1;
}
}
}
System.out.println("10 Test: add the rightest panel");
pshow.setLayout(gl);
gbc.gridx=0;
gbc.weightx=5;
pshow.add(psl,gbc);
gbc.gridx=1;
gbc.weightx=1;
pshow.add(psr,gbc);
psl.setLayout(new BorderLayout());
//test 11:
System.out.println("Test 11");
psl.add("North",labelMsg);
System.out.println("test labelMsg");
psl.add("Center",tpshow);
System.out.println("Test tpshow");
}
private void paintColor()
{
lplog.setBackground(Color.blue);
plbm.setBackground(Color.yellow);
//con.show();
}
public void addAction()
{
lplog.bnok.addActionListener(this);
lplog.bnreg.addActionListener(this);
bnSubmit.addActionListener(this);
bncompute.addActionListener(this);
//add ActionListener for every toypane
int toynum=tpshow.dataNum;
/********
for(int ct=0;ct<toynum;ct++)
{
tpshow.bnPic[ct].addActionListener(this);
}
********/
//added finished
}
private void saveData()
{
tpshow.compute();
userdata.username=lplog.tfName.getText().trim();
userdata.toybuy=tpshow.toyOrder;
userdata.traff=chTraff.getSelectedItem();
userdata.casing=chCasing.getSelectedItem();
userdata.moneytotal=tpshow.rateTotal;
userdata.address=taAddress.getText().trim();
userdata.withword=taWord.getText().trim();
}
public void actionPerformed(ActionEvent ae)
{
Button bn=(Button)ae.getSource();
//String strlabel=bn.getLabel();
String msg,sname,spass;
//if (strlabel.equals("OK"))
if(bn==lplog.bnok)
{
//add the button ok actions
sname=lplog.tfName.getText().trim();
spass=new String(lplog.pfPass.getPassword()).trim();
if (sname.equals(""))
{
lplog.ldisp.setText("Your Name ?");
}
else
{
if (spass.equals(""))
{
lplog.ldisp.setText("Your Password ?");
}
else
{
try
{
toServer.println("ok");
toServer.println(sname);
toServer.println(spass);
msg=fromServer.readLine();
if (msg.equals("ok"))
{
URL urlgif=new URL(urlpath,"../image/okgif.gif");
Icon ipic=new ImageIcon(urlgif);
lplog.changeCard(ipic);
bnSubmit.setEnabled(true);
}
else
{
if (msg.length()>10)
msg.substring(1,10);
lplog.ldisp.setText(msg);
}
}
catch(Exception epp)
{
fail(epp,"login failed");
}
}
}
}
//// case "Register":
else
{
//if (strlabel.equals("Register"))
if(bn==lplog.bnreg)
{
//add the Register actions
try
{
this.getAppletContext().showDocument(new URL(urlpath,"../html/register.htm"));
}
catch(Exception urle)
{}
}
////case "Submit":
else
{
//if (strlabel.equals("Submit"))
if(bn==bnSubmit)
{
//add the submit actions
saveData();
labelSubmit.setText("Submiting Now");
toServer.println("submit");
labelPay.setText("$"+String.valueOf(tpshow.rateTotal));
if (tpshow.rateTotal<=0.0)
labelSubmit.setText("No Toys Selected");
else
{
try
{
//ordertoServer.writeObject(userdata);
toServer.println(userdata.username);
toServer.println(userdata.toybuy);
toServer.println(userdata.traff);
toServer.println(userdata.casing);
toServer.println(userdata.moneytotal);
toServer.println(userdata.address);
toServer.println(userdata.withword);
System.out.println("sended Object");
msg=fromServer.readLine();
labelSubmit.setText(msg);
}
catch(Exception ioex)
{
msg=new String("I/O Error");
fail(ioex,msg);
}
}
}
else
{
if (bn==bncompute)
{
saveData();
labelPay.setText("$"+String.valueOf(tpshow.rateTotal));
}
}
}
}//end actionPerformed
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -