/*
* MainFrame.java
*
* Created on __DATE__, __TIME__
*/
package com.newer.searchproduct.view;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import com.newer.searchproduct.bean.ProductEntity;
import com.newer.searchproduct.dao.ProductDao;
/**
*
* @author __USER__
*/
public class MainFrame extends javax.swing.JFrame {
private ArrayList<ProductEntity> resultList;// 保存按条件查询后的集合
private int index = 0;// 当前商品的索引
private String[] productTypeAry = null;
private String[] orderTypeAry = null;
/** Creates new form MainFrame */
public MainFrame() {
initComponents();
this.init();
}
/**
* 初始化
*/
public void init() {
productTypeAry = new String[] { "productname", "pnce", "discount",
"rest" };
orderTypeAry = new String[] { "asc", "desc" };
this.btnSelect.addActionListener(new BtnSelectListener());
this.btnPre.addActionListener(new BtnPreOrNextListener());
this.btnNext.addActionListener(new BtnPreOrNextListener());
this.jMenuItem1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
exit();
}
});
this.btnNext.setActionCommand("next");
this.btnPre.setActionCommand("pre");
}
/**
* 监听上一条和下一条按钮
*
* @author Administrator
*
*/
public class BtnPreOrNextListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
if (cmd.equals("pre")) {
pre();
} else if (cmd.equals("next")) {
next();
}
}
}
/**
* 上一条按钮的处理方式
*/
public void pre() {
if (index == 0) {
JOptionPane.showMessageDialog(this, "已经到头了", "提示",
JOptionPane.INFORMATION_MESSAGE);
} else {
index--;
this.setValues(resultList.get(index));
}
}
/**
* 下一个按钮的处理方式
*/
public void next() {
if (index == resultList.size() - 1) {
JOptionPane.showMessageDialog(this, "已经到尾了", "提示",
JOptionPane.INFORMATION_MESSAGE);
} else {
index++;
this.setValues(resultList.get(index));
}
}
/**
* 监听查询按钮
*
* @author Administrator
*
*/
public class BtnSelectListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
select();
}
}
/**
* 查询按钮的处理方式
*/
public void select() {
int productIndex = this.cmbProductType.getSelectedIndex();
int orderIndex = this.cmbOrderType.getSelectedIndex();
String productType = this.productTypeAry[productIndex];
String orderType = this.orderTypeAry[orderIndex];
resultList = ProductDao.selectAllProducts(productType, orderType);
if (resultList.size() > 0) {
this.setValues(resultList.get(0));
} else {
JOptionPane.showMessageDialog(this, "没有找到", "提示信息",
JOptionPane.WARNING_MESSAGE);
}
}
/**
* 将查询的结果填充各控件
* @param pro
*/
public void setValues(ProductEntity pro) {
this.txtaName.setText(pro.getProductName());
this.txtPrice.setText(pro.getPnce() + "");
this.txtDiscount.setText(pro.getDiscount() + "");
this.txtRest.setText(pro.getReSt() + "");
this.txtaDesc.setText(pro.getDescription());
}
/**
* 退出按钮的处理方法
*/
public void exit() {
System.exit(0);
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
// GEN-BEGIN:initComponents
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
cmbProductType = new javax.swing.JComboBox();
cmbOrderType = new javax.swing.JComboBox();
jLabel2 = new javax.swing.JLabel();
btnSelect = new javax.swing.JButton();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
jLabel6 = new javax.swing.JLabel();
jScrollPane1 = new javax.swing.JScrollPane();
txtaName = new javax.swing.JTextArea();
txtPrice = new javax.swing.JTextField();
txtDiscount = new javax.swing.JTextField();
txtRest = new javax.swing.JTextField();
jLabel7 = new javax.swing.JLabel();
jScrollPane2 = new javax.swing.JScrollPane();
txtaDesc = new javax.swing.JTextArea();
btnPre = new javax.swing.JButton();
btnNext = new javax.swing.JButton();
jLabel8 = new javax.swing.JLabel();
jLabel9 = new javax.swing.JLabel();
jLabel10 = new javax.swing.JLabel();
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
jMenuItem1 = new javax.swing.JMenuItem();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("\u5546\u54c1\u67e5\u8be2");
jLabel1.setText("\u6309");
cmbProductType.setModel(new javax.swing.DefaultComboBoxModel(
new String[] { "商品名称", "商品价格", "折扣价", "剩余数量" }));
cmbOrderType.setModel(new javax.swing.DefaultComboBoxModel(
new String[] { "升序", "降序" }));
jLabel2.setText("\u6392\u5e8f");
btnSelect.setText("\u67e5\u8be2");
jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
jLabel3.setText("\u5546\u54c1\u540d\u79f0\uff1a");
jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
jLabel4.setText("\u4ef7\u683c\uff1a");
jLabel5.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
jLabel5.setText("\u6298\u6263\u4ef7\uff1a");
jLabel6.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
jLabel6.setText("\u5269\u4f59\u6570\u91cf\uff1a");
txtaName.setColumns(20);
txtaName.setRows(5);
jScrollPane1.setViewportView(txtaName);
jLabel7.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
jLabel7.setText("\u7b80\u4ecb\uff1a");
txtaDesc.setColumns(20);
txtaDesc.setRows(5);
jScrollPane2.setViewportView(txtaDesc);
btnPre.setText("\u4e0a\u4e00\u4e2a");
btnNext.setText("\u4e0b\u4e00\u4e2a");
jLabel8.setText("\u5143");
jLabel9.setText("\u5143");
jLabel10.setText("\u4ef6");
jMenu1.setText("\u7cfb\u7edf");
jMenuItem1.setText("\u9000\u51fa");
jMenu1.add(jMenuItem1);
jMenuBar1.add(jMenu1);
setJMenuBar(jMenuBar1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
getContentPane());
getContentPane().setLayout(layout);
layout
.setHorizontalGroup(layout
.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(
layout
.createSequentialGroup()
.addGroup(
layout
.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(
layout
.createSequentialGroup()
.addGap(
21,
21,
21)
.addGroup(
layout
.createParallelGroup(
javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(
javax.swing.GroupLayout.Alignment.LEADING,
layout
.createSequentialGroup()
.addComponent(
jLabel1)
.addPreferredGap(
javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(
cmbProductType,
javax.swing.GroupLayout.PREFERRED_SIZE,
123,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(
javax.swing.LayoutStyle.ComponentPlacement