?? goodspriceframe.java
字號(hào):
package goodsinterface;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import data.*;
import java.awt.Rectangle;
public class GoodsPriceFrame extends JFrame implements ActionListener {
JPanel contentPane;
//創(chuàng)建列表框數(shù)據(jù)類和列表框控件
DefaultListModel listData1 = new DefaultListModel();
DefaultListModel listData2 = new DefaultListModel();
JList jList1 = new JList(listData1);
JList jList2 = new JList(listData2);
//創(chuàng)建組合框控件
JComboBox jComboBox1 = new JComboBox();
//創(chuàng)建滾動(dòng)框控件
JScrollPane jScrollPane1 = new JScrollPane();
JScrollPane jScrollPane2 = new JScrollPane();
//創(chuàng)建標(biāo)簽控件
JLabel jLabel1 = new JLabel();
JLabel jLabel2 = new JLabel();
JLabel jLabel3 = new JLabel();
JLabel jLabel4 = new JLabel();
JLabel jLabel5 = new JLabel();
JLabel jLabel6 = new JLabel();
JLabel jLabel7 = new JLabel();
JLabel jLabel8 = new JLabel();
//創(chuàng)建編輯框控件
JTextField jTextField1 = new JTextField();
JTextField jTextField2 = new JTextField();
JTextField jTextField3 = new JTextField();
JTextField jTextField4 = new JTextField();
JTextField jTextField5 = new JTextField();
//創(chuàng)建按鈕控件
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
JButton jButton3 = new JButton();
JButton jButton4 = new JButton();
JButton jButton5 = new JButton();
JButton jButton6 = new JButton();
JButton jButton7 = new JButton();
//創(chuàng)建字體類
Font dialog13 = new java.awt.Font("Dialog", 0, 13);
//聲明數(shù)據(jù)類
GoodsData goodsData = new GoodsData();
//創(chuàng)建商品類別數(shù)組
String[][] categories = new String[0][4];
//創(chuàng)建商品數(shù)組
String[][] goods = new String[0][13];
String user = "user";
//創(chuàng)建字段數(shù)組
String[] fieldNames = {"goodsBarCode", "goodsName", "producer"};
public GoodsPriceFrame() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//傳入用戶變量
public GoodsPriceFrame(String user) {
//設(shè)置用戶名字
this.user = user;
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(null);
this.setSize(new Dimension(645, 380));
this.setTitle("商品價(jià)格與折扣管理窗口(" + user + ")");
//設(shè)置標(biāo)簽控件屬性
jLabel1.setText("商品類別列表:");
jLabel1.setBounds(new Rectangle(28, 19, 97, 16));
jLabel2.setText("商品列表:");
jLabel2.setBounds(new Rectangle(176, 19, 97, 16));
jLabel3.setText("查詢條件:");
jLabel3.setBounds(new Rectangle(344, 45, 97, 16));
jLabel4.setText("查詢值:");
jLabel4.setBounds(new Rectangle(344, 93, 97, 16));
jLabel5.setBounds(new Rectangle(344, 141, 110, 16));
jLabel5.setText("商品條形碼:");
jLabel6.setText("商品名稱:");
jLabel6.setBounds(new Rectangle(344, 190, 72, 16));
jLabel7.setText("商品價(jià)格:");
jLabel7.setBounds(new Rectangle(344, 238, 69, 16));
jLabel8.setText("商品折扣:");
jLabel8.setBounds(new Rectangle(491, 238, 65, 16));
//設(shè)置編輯框控件屬性
jTextField1.setBounds(new Rectangle(427, 93, 102, 22));
jTextField2.setEditable(false);
jTextField2.setBounds(new Rectangle(427, 141, 196, 22));
jTextField3.setEditable(false);
jTextField3.setBounds(new Rectangle(427, 190, 196, 22));
jTextField4.setBounds(new Rectangle(426, 238, 58, 22));
jTextField5.setBounds(new Rectangle(565, 238, 58, 22));
//設(shè)置按鈕屬性
jButton1.setText("顯示類別");
jButton1.setActionCommand("showCategory");
jButton1.setBounds(new Rectangle(28, 249, 139, 25));
jButton2.setText("修改");
jButton2.setActionCommand("updatePriceDiscount");
jButton2.setBounds(new Rectangle(344, 286, 95, 25));
jButton3.setText("確定");
jButton3.setActionCommand("ok");
jButton3.setEnabled(false);
jButton3.setBounds(new Rectangle(440, 286, 61, 25));
jButton4.setText("取消");
jButton4.setActionCommand("cancel");
jButton4.setEnabled(false);
jButton4.setBounds(new Rectangle(503, 286, 61, 25));
jButton5.setText("退出");
jButton5.setActionCommand("exit");
jButton5.setBounds(new Rectangle(565, 286, 61, 25));
jButton6.setText("查詢");
jButton6.setActionCommand("search");
jButton6.setBounds(new Rectangle(546, 93, 77, 25));
jButton7.setText("顯示折扣商品");
jButton7.setActionCommand("showDiscountGoods");
jButton7.setBounds(new Rectangle(28, 286, 139, 25));
//設(shè)置組合框?qū)傩? jComboBox1.setBounds(new Rectangle(427, 45, 196, 22));
jComboBox1.addItem("按商品條形碼查詢");
jComboBox1.addItem("按商品名稱查詢");
jComboBox1.addItem("按生產(chǎn)廠商查詢");
//設(shè)置滾動(dòng)框的屬性
jScrollPane1.getViewport().add(jList1, null);
jScrollPane2.getViewport().add(jList2, null);
jScrollPane1.setBounds(new Rectangle(28, 45, 139, 193));
jScrollPane2.setBounds(new Rectangle(176, 45, 153, 266));
//為列表框加入選擇接收器
jList1.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
jList1_valueChanged(e);
}
});
jList2.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
jList2_valueChanged(e);
}
});
contentPane.add(jScrollPane1, null);
contentPane.add(jScrollPane2, null);
contentPane.add(jLabel1, null);
contentPane.add(jLabel2, null);
contentPane.add(jLabel3, null);
contentPane.add(jLabel4, null);
contentPane.add(jLabel5, null);
contentPane.add(jLabel6, null);
contentPane.add(jLabel7, null);
contentPane.add(jLabel8, null);
contentPane.add(jButton1, null);
contentPane.add(jButton2, null);
contentPane.add(jButton3, null);
contentPane.add(jButton4, null);
contentPane.add(jButton5, null);
contentPane.add(jButton6, null);
contentPane.add(jButton7, null);
contentPane.add(jComboBox1, null);
contentPane.add(jTextField1, null);
contentPane.add(jTextField2, null);
contentPane.add(jTextField3, null);
contentPane.add(jTextField4, null);
contentPane.add(jTextField5, null);
setupFontAndListener();
//顯示全部商品類別的方法
showAllCategories();
}
//設(shè)置窗口類的字體和為按鈕加入動(dòng)作接收器的方法
public void setupFontAndListener(){
Component[] components = contentPane.getComponents();
//創(chuàng)建臨時(shí)按鈕控件
JButton tmpBtn = new JButton();
for(int i = 0; i < components.length; i++){
components[i].setFont(dialog13);
if(components[i].getClass().getName().equals("javax.swing.JButton")){
tmpBtn = (JButton)components[i];
tmpBtn.addActionListener(this);
}
}
}
//顯示全部商品類別的方法
public void showAllCategories(){
listData1.clear();
//取得商品類別數(shù)據(jù)
categories = goodsData.getAllGoodsCategory();
//為商品類別列表框加入商品類別數(shù)據(jù)
for(int i = 0; i < categories.length; i++){
listData1.addElement(categories[i][2]);
}
}
//根據(jù)商品類別顯示商品的方法
public void showCategory(){
//取得當(dāng)前選擇項(xiàng)的位置
int selectedIndex = jList1.getSelectedIndex();
if(selectedIndex == -1){return;}
//取得商品數(shù)據(jù)
goods = goodsData.getGoodsByGoodsCategory(Integer.parseInt(
categories[selectedIndex][0]));
this.showSearchGoods();
}
//顯示查詢商品的方法
public void showSearchGoods(){
listData2.clear();
//為商品列表框加入商品數(shù)據(jù)
for(int i = 0; i < goods.length; i++){
listData2.addElement(goods[i][2]);
}
}
//顯示單個(gè)商品的方法
public void showGood(){
//取得當(dāng)前選擇項(xiàng)的位置
int selectedIndex = jList2.getSelectedIndex();
//當(dāng)列表框不處于選擇狀態(tài),不顯示商品數(shù)據(jù)
if(selectedIndex == -1){
return;
}
//顯示商品條形碼
jTextField2.setText(goods[selectedIndex][0]);
//顯示商品名稱
jTextField3.setText(goods[selectedIndex][2]);
//顯示價(jià)格
jTextField4.setText(goods[selectedIndex][11]);
//顯示折扣
jTextField5.setText(goods[selectedIndex][12]);
}
//清空單個(gè)商品顯示的方法
public void clearGood(){
jTextField2.setText("");
jTextField3.setText("");
jTextField4.setText("");
jTextField5.setText("");
}
//退出方法
public void exit(){
//隱藏窗口
this.setVisible(false);
//清空數(shù)組的內(nèi)容
categories = new String[0][4];
goods = new String[0][13];
//清空列表框的內(nèi)容
listData1.clear();
listData2.clear();
//取得面板上的所有控件
Component[] components = contentPane.getComponents();
//創(chuàng)建臨時(shí)文本框控件
JTextField tmpTextField = new JTextField();
for(int i = 0; i < components.length; i++){
if(components[i].getClass().getName().equals("javax.swing.JTextField")){
tmpTextField = (JTextField)components[i];
//清空編輯框的內(nèi)容
tmpTextField.setText("");
}
}
//System.exit(0);
}
//檢查商品按鈕的狀態(tài)
public void checkGoodsBtn(boolean isManipulated){
if(isManipulated){
jButton2.setEnabled(false);
jButton3.setEnabled(true);
jButton4.setEnabled(true);
}else{
jButton2.setEnabled(true);
jButton3.setEnabled(false);
jButton4.setEnabled(false);
}
}
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
exit();
}
}
//列表1的選擇事件
void jList1_valueChanged(ListSelectionEvent e) {
if(listData1.size() > 0){
this.showCategory();
}
}
//列表2的選擇事件
void jList2_valueChanged(ListSelectionEvent e) {
if(listData2.size() > 0){
this.showGood();
}else{
this.clearGood();
}
}
//單擊事件
public void actionPerformed(ActionEvent e) {
//取得按鈕的動(dòng)作字符串
String actionCommand = e.getActionCommand().trim();
//單擊按鈕的處理代碼
if (actionCommand.equals("showCategory")) {
this.showAllCategories();
}else if(actionCommand.equals("showDiscountGoods")){
//取得折扣商品
goods = goodsData.getDiscountGoods();
this.showSearchGoods();
}else if(actionCommand.equals("search")){
//取得查詢編輯框的內(nèi)容
String searchValue = jTextField1.getText().trim();
//取得查詢選項(xiàng)
int selectedIndex = jComboBox1.getSelectedIndex();
if(searchValue.length() == 0){
JOptionPane.showMessageDialog(null, "請(qǐng)輸入查詢值");
return;
}
//根據(jù)字段取得查詢值
goods = goodsData.getGoodsByField(fieldNames[selectedIndex], searchValue);
this.showSearchGoods();
}else if(actionCommand.equals("updatePriceDiscount")){
//進(jìn)行商品價(jià)格與折扣修改時(shí)檢查商品的選擇狀態(tài)
if (jList2.isSelectionEmpty()) {
JOptionPane.showMessageDialog(null, "請(qǐng)選擇商品.");
return;
}
this.checkGoodsBtn(true);
}else if(actionCommand.equals("ok")){
//取得商品條形碼、價(jià)格和折扣
String goodsBarCode = jTextField2.getText().trim();
String salePriceStr = jTextField4.getText().trim();
String discountStr = jTextField5.getText().trim();
double salePrice = 0;
double discount = 0;
if(goodsData.checkDouble(salePriceStr) == 0){
JOptionPane.showMessageDialog(null, "商品價(jià)格必須是數(shù)字.");
return;
}else{
salePrice = Double.parseDouble(salePriceStr);
}
if(goodsData.checkNumIn0To1(discountStr) == 0){
JOptionPane.showMessageDialog(null, "商品折扣的數(shù)值范圍必須大于0小于等于1.");
return;
}else{
discount = Double.parseDouble(discountStr);
}
int result = goodsData.setGoodsPriceDiscount(goodsBarCode, salePrice, discount);
if(result == 1){
//更新商品數(shù)組的價(jià)格和折扣數(shù)據(jù)
int selectedIndex = jList2.getSelectedIndex();
goods[selectedIndex][11] = salePriceStr;
goods[selectedIndex][12] = discountStr;
}else{
JOptionPane.showMessageDialog(null, "商品價(jià)格與折扣更改失敗.");
}
this.checkGoodsBtn(false);
}else if(actionCommand.equals("cancel")){
this.jList2_valueChanged(null);
this.checkGoodsBtn(false);
}else if(actionCommand.equals("exit")){
exit();
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -