?? goodssearchframe.java
字號:
package goodsinterface;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import data.*;
import java.awt.Rectangle;
public class GoodsSearchFrame extends JFrame implements ActionListener {
JPanel contentPane;
//創建標簽控件
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();
JLabel jLabel9 = new JLabel();
JLabel jLabel10 = new JLabel();
JLabel jLabel11 = new JLabel();
JLabel jLabel12 = new JLabel();
JLabel jLabel13 = new JLabel();
JLabel jLabel14 = new JLabel();
JLabel jLabel15 = new JLabel();
JLabel jLabel16 = new JLabel();
//創建編輯框控件
JTextField jTextField1 = new JTextField();
JTextField jTextField2 = new JTextField();
JTextField jTextField3 = new JTextField();
JTextField jTextField4 = new JTextField();
JTextField jTextField5 = new JTextField();
JTextField jTextField6 = new JTextField();
JTextField jTextField7 = new JTextField();
JTextField jTextField8 = new JTextField();
JTextField jTextField9 = new JTextField();
JTextField jTextField10 = new JTextField();
JTextField jTextField11 = new JTextField();
JTextField jTextField12 = new JTextField();
JTextField jTextField13 = new JTextField();
//創建按鈕控件
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
JButton jButton3 = new JButton();
//創建滾動框控件
JScrollPane jScrollPane1 = new JScrollPane();
JScrollPane jScrollPane2 = new JScrollPane();
JScrollPane jScrollPane3 = new JScrollPane();
//創建列表框數據類和列表框控件
DefaultListModel listData1 = new DefaultListModel();
DefaultListModel listData2 = new DefaultListModel();
JList jList1 = new JList(listData1);
JList jList2 = new JList(listData2);
//創建文本框控件
JTextArea jTextArea1 = new JTextArea();
//創建組合框控件
JComboBox jComboBox1 = new JComboBox();
//創建字體類
Font dialog13 = new java.awt.Font("Dialog", 0, 13);
//聲明數據類
GoodsData goodsData = new GoodsData();
//創建商品類別數組
String[][] categories = new String[0][4];
//創建商品數組
String[][] goods = new String[0][13];
//創建動作字符串
String action = "";
String user = "user";
//創建字段數組
String[] fieldNames = {
"goodsBarCode", "goodsName", "goodsNickName", "goodsAssistantName",
"goodsPYName", "unit", "specification", "producer"};
public GoodsSearchFrame() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//傳入用戶變量
public GoodsSearchFrame(String user) {
//設置用戶名字
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(670, 550));
this.setTitle("商品查詢窗口(" + user + ")");
//設置標簽控件
jLabel1.setText("商品類別列表:");
jLabel1.setBounds(new Rectangle(26, 17, 97, 16));
jLabel2.setText("類別序號:");
jLabel2.setBounds(new Rectangle(197, 47, 83, 16));
jLabel3.setText("類別父索引:");
jLabel3.setBounds(new Rectangle(197, 101, 88, 16));
jLabel4.setText("商品類別名稱:");
jLabel4.setBounds(new Rectangle(197, 156, 109, 16));
jLabel5.setText("商品類別描述:");
jLabel5.setBounds(new Rectangle(403, 47, 112, 16));
jLabel6.setText("商品列表:");
jLabel6.setBounds(new Rectangle(26, 184, 97, 16));
jLabel7.setText("查詢條件:");
jLabel7.setBounds(new Rectangle(196, 210, 97, 16));
jLabel8.setText("商品條形碼:");
jLabel8.setBounds(new Rectangle(196, 260, 110, 16));
jLabel9.setText("商品分類標識:");
jLabel9.setBounds(new Rectangle(490, 260, 92, 16));
jLabel10.setText("商品名稱:");
jLabel10.setBounds(new Rectangle(196, 309, 72, 16));
jLabel11.setText("商品別名:");
jLabel11.setBounds(new Rectangle(490, 309, 85, 16));
jLabel12.setText("助記碼:");
jLabel12.setBounds(new Rectangle(196, 359, 76, 16));
jLabel13.setText("拼音碼:");
jLabel13.setBounds(new Rectangle(356, 359, 76, 16));
jLabel14.setText("計量單位:");
jLabel14.setBounds(new Rectangle(490, 359, 76, 16));
jLabel15.setText("規格:");
jLabel15.setBounds(new Rectangle(196, 408, 76, 16));
jLabel16.setText("生產廠商:");
jLabel16.setBounds(new Rectangle(356, 408, 72, 16));
//設置編輯框
jTextField1.setBounds(new Rectangle(288, 47, 93, 22));
jTextField1.setEditable(false);
jTextField2.setBounds(new Rectangle(288, 99, 93, 22));
jTextField2.setEditable(false);
jTextField3.setBounds(new Rectangle(288, 150, 190, 22));
jTextField4.setBounds(new Rectangle(433, 210, 102, 22));
jTextField5.setBounds(new Rectangle(281, 258, 179, 22));
jTextField6.setBounds(new Rectangle(576, 258, 65, 22));
jTextField7.setBounds(new Rectangle(281, 306, 179, 22));
jTextField8.setBounds(new Rectangle(576, 306, 65, 22));
jTextField9.setBounds(new Rectangle(281, 354, 65, 22));
jTextField10.setBounds(new Rectangle(407, 354, 65, 22));
jTextField11.setBounds(new Rectangle(576, 354, 65, 22));
jTextField12.setBounds(new Rectangle(281, 402, 65, 22));
jTextField13.setBounds(new Rectangle(462, 402, 179, 22));
//設置按鈕
jButton1.setText("顯示商品類別");
jButton1.setActionCommand("showCategory");
jButton1.setBounds(new Rectangle(196, 451, 217, 25));
jButton2.setText("查詢");
jButton2.setActionCommand("search");
jButton2.setBounds(new Rectangle(543, 210, 98, 25));
jButton3.setText("退出");
jButton3.setActionCommand("exit");
jButton3.setBounds(new Rectangle(424, 451, 217, 25));
//設置滾動框
jScrollPane1.setBounds(new Rectangle(26, 47, 153, 125));
jScrollPane2.setBounds(new Rectangle(488, 47, 153, 125));
jScrollPane3.setBounds(new Rectangle(26, 210, 153, 266));
jScrollPane1.getViewport().add(jList1, null);
jScrollPane2.getViewport().add(jTextArea1, null);
jScrollPane3.getViewport().add(jList2, null);
//為列表框加入選擇接收器
jList1.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
jList1_valueChanged(e);
}
});
jList2.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
jList2_valueChanged(e);
}
});
//設置下拉列表框
jComboBox1.setBounds(new Rectangle(281, 210, 141, 22));
jComboBox1.addItem("按商品條形碼查詢");
jComboBox1.addItem("按商品名稱查詢");
jComboBox1.addItem("按商品別名查詢");
jComboBox1.addItem("按助記碼查詢");
jComboBox1.addItem("按拼音碼查詢");
jComboBox1.addItem("按計量單位查詢");
jComboBox1.addItem("按規格查詢");
jComboBox1.addItem("按生產廠商查詢");
contentPane.add(jScrollPane1, null);
contentPane.add(jScrollPane2, null);
contentPane.add(jScrollPane3, 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(jLabel9, null);
contentPane.add(jLabel10, null);
contentPane.add(jLabel11, null);
contentPane.add(jLabel12, null);
contentPane.add(jLabel13, null);
contentPane.add(jLabel14, null);
contentPane.add(jLabel15, null);
contentPane.add(jLabel16, null);
contentPane.add(jTextField1, null);
contentPane.add(jTextField2, null);
contentPane.add(jTextField3, null);
contentPane.add(jTextField4, null);
contentPane.add(jTextField5, null);
contentPane.add(jTextField6, null);
contentPane.add(jTextField7, null);
contentPane.add(jTextField8, null);
contentPane.add(jTextField9, null);
contentPane.add(jTextField10, null);
contentPane.add(jTextField11, null);
contentPane.add(jTextField12, null);
contentPane.add(jTextField13, null);
contentPane.add(jButton1, null);
contentPane.add(jButton2, null);
contentPane.add(jButton3, null);
contentPane.add(jComboBox1, null);
//設置窗口類的字體和為按鈕加入動作接收器
setupFontAndListener();
//顯示全部商品類別的方法
showAllCategories();
}
//設置窗口類的字體和為按鈕加入動作接收器的方法
public void setupFontAndListener(){
Component[] components = contentPane.getComponents();
//創建臨時按鈕控件
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();
//取得商品類別數據
categories = goodsData.getAllGoodsCategory();
//為商品類別列表框加入商品類別數據
for(int i = 0; i < categories.length; i++){
listData1.addElement(categories[i][2]);
}
}
//顯示單個商品類別的方法
public void showCategory(){
//取得當前選擇項的位置
int selectedIndex = jList1.getSelectedIndex();
//當列表框不處于選擇狀態,不顯示商品類別數據
if(selectedIndex == -1){
return;
}
//顯示商品類別序號
jTextField1.setText(categories[selectedIndex][0]);
//顯示父索引
jTextField2.setText(categories[selectedIndex][1]);
//顯示商品類別名稱
jTextField3.setText(categories[selectedIndex][2]);
//顯示商品類別描述
jTextArea1.setText(categories[selectedIndex][3]);
//取得商品數據
goods = goodsData.getGoodsByGoodsCategory(Integer.parseInt(
categories[selectedIndex][0]));
this.showSearchGoods();
}
//清空單個商品類別顯示的方法
public void clearCategory(){
jTextField1.setText("");
jTextField2.setText("");
jTextField3.setText("");
jTextArea1.setText("");
}
//顯示查詢商品的方法
public void showSearchGoods(){
listData2.clear();
//為商品列表框加入商品數據
for(int i = 0; i < goods.length; i++){
listData2.addElement(goods[i][0]);
}
}
//顯示單個商品的方法
public void showGood(){
//取得當前選擇項的位置
int selectedIndex = jList2.getSelectedIndex();
//當列表框不處于選擇狀態,不顯示商品數據
if(selectedIndex == -1){
return;
}
//顯示商品條形碼
jTextField5.setText(goods[selectedIndex][0]);
//顯示商品分類標識
jTextField6.setText(goods[selectedIndex][1]);
//顯示商品名稱
jTextField7.setText(goods[selectedIndex][2]);
//顯示商品別名
jTextField8.setText(goods[selectedIndex][3]);
//顯示商品助記碼
jTextField9.setText(goods[selectedIndex][4]);
//顯示商品拼音碼
jTextField10.setText(goods[selectedIndex][5]);
//顯示計量單位
jTextField11.setText(goods[selectedIndex][6]);
//顯示規格
jTextField12.setText(goods[selectedIndex][7]);
//顯示生產廠商
jTextField13.setText(goods[selectedIndex][8]);
}
//清空單個商品顯示的方法
public void clearGood(){
jTextField5.setText("");
jTextField6.setText("");
jTextField7.setText("");
jTextField8.setText("");
jTextField9.setText("");
jTextField10.setText("");
jTextField11.setText("");
jTextField12.setText("");
jTextField13.setText("");
}
protected void processWindowEvent(WindowEvent e) {
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
exit();
}
}
//退出方法
public void exit(){
//隱藏窗口
this.setVisible(false);
//清空數組的內容
categories = new String[0][4];
goods = new String[0][13];
//清空列表框的內容
listData1.clear();
listData2.clear();
//清空文本框的內容s
jTextArea1.setText("");
//取得面板上的所有控件
Component[] components = contentPane.getComponents();
//創建臨時文本框控件
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];
//清空編輯框的內容
tmpTextField.setText("");
}
}
//System.exit(0);
}
//檢查商品類別按鈕的狀態
public void checkCategoryBtn(boolean isManipulated){
if(isManipulated){
}else{
}
}
//檢查商品按鈕的狀態
public void checkGoodBtn(boolean isManipulated){
if(isManipulated){
}else{
}
}
//事件單擊方法
public void actionPerformed(ActionEvent e) {
//取得按鈕的動作字符串
String actionCommand = e.getActionCommand().trim();
//單擊按鈕的處理代碼
if (actionCommand.equals("showCategory")) {
this.showAllCategories();
}else if(actionCommand.equals("search")){
//取得查詢編輯框的內容
String searchValue = jTextField4.getText().trim();
//取得查詢選項
int selectedIndex = jComboBox1.getSelectedIndex();
if(searchValue.length() == 0){
JOptionPane.showMessageDialog(null, "請輸入查詢值");
return;
}
//根據字段取得商品記錄
goods = goodsData.getGoodsByField(fieldNames[selectedIndex], searchValue);
this.showSearchGoods();
}else if(actionCommand.equals("exit")){
exit();
}
}
//列表1的選擇事件
void jList1_valueChanged(ListSelectionEvent e) {
if(listData1.size() > 0){
this.showCategory();
}else{
this.clearCategory();
}
}
//列表2的選擇事件
void jList2_valueChanged(ListSelectionEvent e) {
if(listData2.size() > 0){
this.showGood();
}else{
this.clearGood();
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -