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

📄 catalogsubmission.java

📁 Java程序设计技巧与开发实例附书源代码。
💻 JAVA
字号:

import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;

public class CatalogSubmission
    extends Frame
    implements ActionListener
{
   private final static String CLIENT_ID = "JBDOrderClient";
   private final static double CLIENT_VERSION = 1.0;
   private final static String SERVER_ID = "JBDOrderServer";
   private final static double SERVER_VERSION = 1.0;
   private final static String HOST = "localhost";
   private final static int PORT = 5670;

   private Catalog owner = null;
   private TextField name = new TextField();
   private TextArea address = new TextArea(3, 20);
   private TextField credit = new TextField();
   private TextArea orders = new TextArea(6, 30);
   private CatalogOrder theBill = null;
   private Button submit = new Button("Submit Order");
   private Button cancel = new Button("Cancel Order");
   private Label status = new Label("Enter personal and mailing info");

   public CatalogSubmission(Catalog _owner, CatalogOrder _theBill)
   {
      super("Submit your Order");
      owner = _owner;
      theBill = _theBill;
      Color color = new Color(255, 255, 200);
      Panel buttons = new Panel();
      buttons.setLayout(new FlowLayout());
      buttons.add(submit);
      submit.addActionListener(this);
      buttons.add(cancel);
      cancel.addActionListener(this);
      Panel cInfo = new Panel();
      cInfo.setLayout(new GridLayout(2, 2));
      cInfo.add(new Label("Full Name:"));
      cInfo.add(name);
      cInfo.add(new Label("Credit Card: "));
      cInfo.add(credit);
      Panel pInfo = new Panel();
      pInfo.setLayout(new BorderLayout());
      pInfo.add("West", new PanelBox(cInfo, "Personal Info", color));
      pInfo.add("East", new PanelBox(address, "Mailing Address", color));
      Panel oInfo = new Panel();
      oInfo.setLayout(new BorderLayout());
      oInfo.add("Center", new PanelBox(orders, "Your order: ", color));
      oInfo.add("South", new PanelBox(status, "Status:", color));
      setLayout(new BorderLayout());
      add("North", pInfo);
      add("Center", oInfo);
      add("South", buttons);
      orders.setText(theBill.toString());
      orders.setEditable(false);
      validate();
      pack();
      setVisible(true);
      submit.setEnabled(true);
      addWindowListener(new WindowAdapter()
      {
         public void windowClosing(WindowEvent we)
         {
            owner.closeSubmission();
         }
      });
   }

   public void actionPerformed(ActionEvent ae)
   {
      if (ae.getSource() == cancel)
      {
         owner.closeSubmission();
      }
      else if (ae.getSource() == submit)
      {
         theBill.setInfo(name.getText(), credit.getText(),
                         address.getText());
         if (theBill.isComplete())
         {
            submitOrder();
         }
         else
         {
            status.setText("NOT SUBMITTED: incomplete information.");
         }
      }
   }

   private void submitOrder()
   {
      status.setText("Connecting to " + HOST);
      try
      {
         Socket connection = new Socket(HOST, PORT);
         ObjectOutputStream out = new ObjectOutputStream(
             connection.getOutputStream());
         ObjectInputStream in = new ObjectInputStream(
             connection.getInputStream());
         out.writeUTF(CLIENT_ID);
         out.flush();
         if (in.readUTF().equals(SERVER_ID))
         {
            out.writeDouble(CLIENT_VERSION);
            out.flush();
            if (in.readDouble() == SERVER_VERSION)
            {
               status.setText("Connected. Submitting order    ...");
               out.writeObject(theBill);
               out.flush();
               status.setText("Order submitted. Thank you.");
            }
            else
            {
               status.setText("Invalid server version, contact Service");
            }
         }
         else
         {
            status.setText("Invalid server, contact Service");
         }
         out.close();
         in.close();
         connection.close();
         submit.setEnabled(false);
         cancel.setLabel("Done");
      }
      catch (UnknownHostException uhe)
      {
         status.setText("Server " + HOST + " unknown.");
      }
      catch (IOException ioe)
      {
         status.setText("Error submitting order, please try again.");
      }
   }
}

⌨️ 快捷键说明

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