catalog.java
来自「java编程开发技巧与实例的编译测试通过的所有例程」· Java 代码 · 共 116 行
JAVA
116 行
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.util.*;
import java.io.*;
import java.net.*;
public class Catalog extends Applet implements ActionListener, ItemListener
{
private Button orderButton = new Button("Order Item");
private Button submitButton = new Button("Submit Order");
private Button clearButton = new Button("Clear Order");
private java.awt.List catalog = new java.awt.List(5);
private TextArea description = new TextArea(5, 40);
private TextArea orders = new TextArea(5, 30);
private LinkedList data = null;
private CatalogImage theImage = new CatalogImage();
private CatalogOrder theBill = new CatalogOrder();
private CatalogSubmission submission = null;
public void init()
{
Color color = new Color(255, 255, 200);
Panel items = new Panel();
items.setLayout(new BorderLayout());
items.add("West", new PanelBox(catalog, "Catalog", color));
items.add("Center", new PanelBox(orders, "your dorder", color));
Panel buttons = new Panel();
buttons.setLayout(new FlowLayout());
buttons.add(orderButton);
orderButton.addActionListener(this);
buttons.add(submitButton);
submitButton.addActionListener(this);
buttons.add(clearButton);
clearButton.addActionListener(this);
Panel panel = new Panel();
panel.setLayout(new BorderLayout());
panel.add("West", theImage);
panel.add("Center", description);
setLayout(new BorderLayout());
add("North", items);
add("Center",new PanelBox(panel, "Description", color));
add("South", new PanelBox(buttons, "Click a button", color));
setSize(700, 300);
//setLocationRelativeTo(this);
catalog.addActionListener(this);
catalog.addItemListener(this);
readCatalog();
}
public void readCatalog()
{
try
{
URL url = new URL(getCodeBase(), "catalog.dat");
BufferedInputStream bin = new BufferedInputStream(url.openStream());
ObjectInputStream in = new ObjectInputStream(bin);
data = (LinkedList)in.readObject();
for (Iterator iterator = data.iterator(); iterator.hasNext(); )
catalog.add(((CatalogItem)iterator.next()).getName());
in.close();
clearOrders();
}
catch (Exception e)
{
catalog.add("not available");
description.setText("Catalog not available, try again later");
System.err.println(e);
}
}
public void showDescription(int i)
{
CatalogItem item = (CatalogItem)data.get(i);
description.setText(item.toString());
theImage.setImage(getCodeBase(), item.imageName);
}
public void orderItem(int i)
{
theBill.add((CatalogItem)data.get(i));
orders.setText(theBill.toString());
submitButton.setEnabled(true);
}
public void clearOrders()
{
theBill = new CatalogOrder();
orders.setText("No items selected for purchasing");
submitButton.setEnabled(false);
}
public void actionPerformed(ActionEvent ae)
{
if ((ae.getSource() == orderButton) || (ae.getSource() == catalog))
{
if (catalog.getSelectedIndex() >= 0)
orderItem(catalog.getSelectedIndex());
}
else if (ae.getSource() == clearButton)
clearOrders();
else if (ae.getSource() == submitButton)
{
if (submission == null)
submission = new CatalogSubmission(this, theBill);
else
submission.toFront();
}
}
public void itemStateChanged(ItemEvent ie)
{
showDescription(catalog.getSelectedIndex());
}
public void closeSubmission()
{
submission.setVisible(false);
submission.dispose();
submission = null;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?