You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
java/FlowerListFrame.java

243 lines
8.2 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package flowershop.view;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JSplitPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.SwingConstants;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;
import flowershop.model.Flower;
import flowershop.dao.FlowerDao;
import flowershop.daoimpl.FlowerDaoImpl;
//商品列表窗口
public class FlowerListFrame extends MyFrame{
private JTable table;
private JLabel lblImage;
private JLabel lblPrice;
private JLabel lblDescn;
//商品列表集合
private List<Flower> flowers =null;
//创建商品Dao对象
private FlowerDao dao = new FlowerDaoImpl();
// 购物车键是选择的商品Id值是商品的数量
private Map<String,Integer> cart = new HashMap<String,Integer>();
// 选择的商品索引
private int selectedRow = -1;
public FlowerListFrame(String title, int width, int height) {
super(title, width, height);
// TODO 自动生成的构造函数存根
flowers = dao.findAll();
//添加顶部搜索面板
getContentPane().add(getSearchPanel(),BorderLayout.NORTH);
//创建分栏面板
JSplitPane splitPane = new JSplitPane();
//设置指定分割条位置,从窗格的左边到分割条的右边
splitPane.setDividerLocation(600);
//设置左侧面板
splitPane.setLeftComponent(getLeftPanel());
//设置右侧面板
splitPane.setRightComponent(getRightPanel());
//把分栏面板添加到内容面板
getContentPane().add(splitPane,BorderLayout.CENTER);
}
//初始化右侧面板
private JPanel getRightPanel() {
JPanel rightPanel = new JPanel();
rightPanel.setBackground(new Color(255, 220, 230));
rightPanel.setLayout(new GridLayout(2,1,0,20));
lblImage = new JLabel();
rightPanel.add(lblImage);
lblImage.setHorizontalAlignment(SwingConstants.CENTER);
JPanel detailPanel = new JPanel();
detailPanel.setBackground(new Color(255, 220, 230));
rightPanel.add(detailPanel);
detailPanel.setLayout(new GridLayout(8,1,0,5));
JSeparator separator_1 = new JSeparator();
detailPanel.add(separator_1);
lblPrice = new JLabel();
detailPanel.add(lblPrice);
//设置字体
lblPrice.setFont(new Font("微软雅黑",Font.PLAIN,16));
lblDescn = new JLabel();
detailPanel.add(lblDescn);
//设置字体
lblDescn.setFont(new Font("微软雅黑",Font.PLAIN,16));
JSeparator separator_2 = new JSeparator();
detailPanel.add(separator_2);
JButton btnAdd = new JButton("添加到购物车");
btnAdd.setFont(new Font("微软雅黑",Font.PLAIN,15));
detailPanel.add(btnAdd);
//布局占位使用
JLabel lb1 =new JLabel("");
detailPanel.add(lb1);
JButton btnCheck = new JButton("查看购物车");
btnCheck.setFont(new Font("微软雅黑",Font.PLAIN,15));
detailPanel.add(btnCheck);
//注册【添加到购物车】按钮的ActionEvent事件监听器
btnAdd.addActionListener(e ->{
if(selectedRow <0) {
return;
}
//添加商品到购物车处理
Flower selectFlower = flowers.get(selectedRow);
String productid = selectFlower.getProductid();
if(cart.containsKey(productid)) {//购物车中已经有该商品
//获得商品数量
Integer quantity = cart.get(productid);
cart.put(productid, ++quantity);
}else {//购物车中还没有还商品
cart.put(productid, 1);
}
System.out.println(cart);
});
// //注册【查看到购物车】按钮的ActionEvent事件监听器
btnCheck.addActionListener(e->{
CartFrame cartFrame = new CartFrame(cart,this);
cartFrame.setVisible(true);
this.setVisible(false);
});
return rightPanel;
}
//初始化左侧面板
private JScrollPane getLeftPanel() {
JScrollPane leftScrollPane = new JScrollPane();
//将表格作为滚动面板的各个视口试图
leftScrollPane.setViewportView(getTable());
return leftScrollPane;
}
//初始化左侧面板中的表格控件
private JTable getTable() {
FlowerTableModel model = new FlowerTableModel(this.flowers);
if(table == null) {
table = new JTable(model);
//设置表中内容字体
table.setFont(new Font("微软雅黑",Font.PLAIN,16));
//设置表列标题字体
table.getTableHeader().setFont(new Font("微软雅黑",Font.PLAIN,16));
//设置表行高
table.setRowHeight(51);
table.setRowSelectionAllowed(true);
table.setBackground(new Color(250,220,230));
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
ListSelectionModel rowSelectionModel = table.getSelectionModel();
rowSelectionModel.addListSelectionListener(e ->{
//只处理鼠标释放
if(e.getValueIsAdjusting()) {
return;
}
ListSelectionModel lsm = (ListSelectionModel) e.getSource();
selectedRow = lsm.getMinSelectionIndex();
if(selectedRow < 0) {
return;
}
//更新右侧面板内容
Flower f = flowers.get(selectedRow);
String petImage = String.format("/image/%s",f.getImage());
ImageIcon icon = new ImageIcon(FlowerListFrame.class.getResource(petImage));
lblImage.setIcon(icon);
String descn = f.getDescn();
lblDescn.setText("花语: "+ descn);
double price = f.getPrice();
String sprice = String.format("单支价格:%.2f",price);
lblPrice.setText(sprice);
});
}else {
table.setModel(model);
}
return table;
}
//初始化搜索面板
private JPanel getSearchPanel() {
//TODO
JPanel searchPanel = new JPanel();
searchPanel.setBackground(new Color(255, 220, 230));
FlowLayout flowLayout = (FlowLayout) searchPanel.getLayout();
flowLayout.setVgap(20);
flowLayout.setHgap(40);
JLabel lbl = new JLabel("请选择花卉类别:");
lbl.setFont(new Font("微软雅黑",Font.PLAIN,15));
searchPanel.add(lbl);
String[] categorys= {"春季花卉","夏季花卉","秋季花卉","冬季花卉"};
JComboBox comboBox = new JComboBox(categorys);
comboBox.setFont(new Font("微软雅黑",Font.PLAIN,15));
searchPanel.add(comboBox);
JButton btnGo = new JButton("查询");
btnGo.setFont(new Font("微软雅黑", Font.PLAIN, 15));
searchPanel.add(btnGo);
JButton btnReset = new JButton("重置");
btnReset.setFont(new Font("微软雅黑",Font.PLAIN,15));
searchPanel.add(btnReset);
//注册查询按钮的ActionEvent事件监听器
btnGo.addActionListener(e ->{
//所选择的类别
String category = (String) comboBox.getSelectedItem();
//按照类别查询
flowers = dao.findByCategory(category);
TableModel model = new FlowerTableModel(flowers);
table.setModel(model);
});
// 注册重置按钮的ActionEvent事件监听器
btnReset.addActionListener(e -> {
flowers = dao.findAll();
TableModel model = new FlowerTableModel(flowers);
table.setModel(model);
});
return searchPanel;
}
}