catalogsubmission.java

来自「java编程开发技巧与实例的编译测试通过的所有例程」· Java 代码 · 共 123 行

JAVA
123
字号
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			=	5678;
	//
	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(25, 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, "Mail 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())
				//status.setText("NOT SUBMITTED: feature not implemented.");
				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 + =
减小字号Ctrl + -
显示快捷键?