import java.util.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import java.io.*;
/**
* This class displays the catalog of the gourmet coffee system.
*
* @author Michael
* @version 1.1.0
* @see Product
* @see Coffee
* @see CoffeeBrewer
* @see Catalog
* @see CatalogLoader
* @see FileCatalogLoader
* @see DataFormatException
* @see DataField
*/
public class CatalogGUI extends JPanel {
/* Standar error stream */
static private PrintWriter stdErr = new PrintWriter(System.err, true);
/* Window width in pixels */
static private int WIDTH = 380;
/* Window height in pixels */
static private int HEIGHT = 360;
/* Size of the list cell in pixels */
static private int CELL_SIZE = 50;
/* Visible rows in list */
static private int LIST_ROWS = 10;
/* Rows in status text area */
static private int STATUS_ROWS = 5;
/* Rows in status text area */
static private int STATUS_COLS = 40;
private JList catalogList;
private JPanel productPanel;
private JTextArea statusTextArea;
private Catalog catalog;
/**
* Loads a product catalog and starts the application.
*
* @param args String arguments. Not used.
* @throws IOException if there are errors in the loading
* the catalog.
*/
public static void main(String[] args) throws IOException {
String filename = "";
if (args.length != 1) {
filename = "catalog.dat";
} else {
filename = args[0];
}
try {
Catalog catalog =
(new FileCatalogLoader()).loadCatalog(filename);
JFrame frame = new JFrame("Catalog Gourmet Coffee");
frame.setContentPane(new CatalogGUI(catalog));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(WIDTH, HEIGHT);
frame.setResizable(true);
frame.setVisible(true);
} catch (FileNotFoundException fnfe) {
stdErr.println("The file does not exist");
System.exit(1);
} catch (DataFormatException dfe) {
stdErr.println("The file contains malformed data: "
+ dfe.getMessage());
System.exit(1);
}
}
/**
* Instantiates the components and arranges them in a window.
*
* @param initialCatalog a product catalog.
*/
public CatalogGUI(Catalog initialCatalog) {
catalog = initialCatalog;
// create the components
catalogList = new JList(catalog.getCodes());
catalogList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
catalogList.setVisibleRowCount(LIST_ROWS);
catalogList.setFixedCellWidth(CELL_SIZE);
statusTextArea = new JTextArea(STATUS_ROWS, STATUS_COLS);
statusTextArea.setEditable(false);
// create product information panel
productPanel = new JPanel();
productPanel.setBorder(
BorderFactory.createTitledBorder("Product Information"));
// create panel to hold list and Display button
JPanel catalogPanel = new JPanel();
catalogPanel.setBorder(BorderFactory.createTitledBorder("Catalog"));
catalogPanel.add (
new JScrollPane(catalogList,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER));
setLayout(new BorderLayout());
add(catalogPanel, BorderLayout.WEST);
add(productPanel, BorderLayout.CENTER);
add(statusTextArea, BorderLayout.SOUTH);
// start listening for list events
catalogList.addListSelectionListener(new CatalogListListener());
}
/*
* Obtains a {@link JPanel} object with the information of a product.
*
* @param dataFields an {@link ArrayList} of {@link DataField}
* with the product information.
* @return a {@link JPanel} with the product information.
*/
private JPanel getDataFieldsPanel(ArrayList<DataField> dataFields) {
/* PLACE YOUR CODE HERE */
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
JPanel leftPanel = new JPanel();
JPanel rightPanel = new JPanel();
panel.add(leftPanel, BorderLayout.WEST);
panel.add(rightPanel, BorderLayout.CENTER);
leftPanel.setPreferredSize(new Dimension(70,220));
rightPanel.setPreferredSize(new Dimension(190,230));
leftPanel.setLayout(new GridLayout(10, 1));
rightPanel.setLayout(new GridLayout(10, 1));
Iterator<DataField> i = dataFields.iterator();
while (i.hasNext()) {
DataField dataField = i.next();
JLabel lable = new JLabel();
JTextField textField = new JTextField();
leftPanel.add(lable);
rightPanel.add(textField);
lable.setText(dataField.getName() + ":");
lable.setHorizontalAlignment(JLabel.LEFT);
textField.setText(dataField.getValue());
textField.setEditable(false);
}
return panel;
}
/**
* This inner class handles list-selection events.
*/
class CatalogListListener implements ListSelectionListener {
/**
* Displays the information of the selected product.
*
* @param event the event object.
*/
public void valueChanged(ListSelectionEvent event) {
String code = (String) catalogList.getSelectedValue();
Product product = catalog.getProduct(code);
productPanel.removeAll();
productPanel.setVisible(false); // Use this
productPanel.add( // to update
getDataFieldsPanel(product.getDataFields())); // the panel
productPanel.setVisible(true); // correctly
statusTextArea.setText("Product " + code + " has been displayed");
}
}
}
ssd3 exercise7
需积分: 9 31 浏览量
2009-01-15
14:34:16
上传
评论
收藏 2KB RAR 举报
chinesesoftwaredn
- 粉丝: 2
- 资源: 21
最新资源
- 基于keras+fasterRCNN,在VOC格式的口罩数据集上训练,检测人群中有无戴口罩python源码+模型
- push_version
- 软件自制图像批量压缩工具
- 基于深度学习的抗梯度噪声的缺陷检测器python源码+文档说明+模型的预训练
- 基于python+pytorch+mysql实现停车场车牌识别管理系统源码+文档说明
- 基于QT+MySQl+OpenCV车牌识别搭建停车场管理系统C++源码+文档说明+界面展示
- 基于深度学习的停车场收费系统-车牌识别模块python源码+文档说明+博客教学
- 空白.pages
- 基于Java+Springboot+vue的智能停车场管理系统(源代码+数据库+9000字论文) 本项目前后端不分离+部署教程
- 基于SSM写的停车场管理系统,加入了车牌识别和数据分析+源码+文档说明
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈