⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 productframe.java

📁 java在线商店的源代码。编写十分规范的哦
💻 JAVA
字号:
/**
 * Copyright (c) 1996-2004 Borland Software Corporation.  All Rights Reserved.
 * 
 * This SOURCE CODE FILE, which has been provided by Borland Software as part
 * of a Borland Software product for use ONLY by licensed users of the product,
 * includes CONFIDENTIAL and PROPRIETARY information of Borland Software.  
 *
 * USE OF THIS SOFTWARE IS GOVERNED BY THE TERMS AND CONDITIONS 
 * OF THE LICENSE STATEMENT AND LIMITED WARRANTY FURNISHED WITH
 * THE PRODUCT.
 *
 * IN PARTICULAR, YOU WILL INDEMNIFY AND HOLD BORLAND SOFTWARE, ITS
 * RELATED COMPANIES AND ITS SUPPLIERS, HARMLESS FROM AND AGAINST ANY
 * CLAIMS OR LIABILITIES ARISING OUT OF THE USE, REPRODUCTION, OR
 * DISTRIBUTION OF YOUR PROGRAMS, INCLUDING ANY CLAIMS OR LIABILITIES
 * ARISING OUT OF OR RESULTING FROM THE USE, MODIFICATION, OR
 * DISTRIBUTION OF PROGRAMS OR FILES CREATED FROM, BASED ON, AND/OR
 * DERIVED FROM THIS SOURCE CODE FILE.
 */
//------------------------------------------------------------------------------
// Copyright (c) 1996-2004 Borland Software Corporation.  All Rights Reserved.
//------------------------------------------------------------------------------

package com.borland.samples.orderentry;

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import com.borland.dbswing.*;
import com.borland.dx.dataset.*;
import com.borland.dx.sql.dataset.*;
import com.borland.dx.text.Alignment;


/**
 * ProductFrame implements the Product Form that is used to maintain
 * Product records. This form allows users to view or update
 * existing products, or enter new products. A standard navigation control
 * is used to allow for record browsing.
 *
 * <P>
 * This class employs the Singleton pattern to ensure that the class has only
 * one instance. You can access the singleton instance by calling the
 * getProductFrame method.
 */
public class ProductFrame extends JFrame {
  private static DataModule1 dm = DataModule1.getDataModule();
  private static ProductFrame myProductFrame;
  private QueryDataSet qdsProduct;
  private QueryDataSet qdsCategory;
  JPanel contentPane;
  JPanel pnlToolbar = new JPanel();
  JPanel pnlProductInfo = new JPanel();
  JdbTextField txtProductID = new JdbTextField();
  JdbTextField txtName = new JdbTextField();
  JdbComboBox cboCategory = new JdbComboBox();
  JdbTextField txtBasePrice = new JdbTextField();
  JdbTextField txtDiscountPct = new JdbTextField();
  JdbTextField txtStockQty = new JdbTextField();
  JdbTextField txtMinReorderQty = new JdbTextField();
  JdbCheckBox chkIsActive = new JdbCheckBox();
  JLabel lblName = new JdbLabel();
  JdbLabel lblCategory = new JdbLabel();
  JdbLabel lblBasePrice = new JdbLabel();
  JdbLabel lblDiscountPct = new JdbLabel();
  JdbLabel lblStockQty = new JdbLabel();
  JdbLabel lblMinReorderQty = new JdbLabel();
  JdbLabel lblProductID = new JdbLabel();
  BorderLayout borderLayout1 = new BorderLayout();
  GridBagLayout gridBagLayout1 = new GridBagLayout();
  JdbNavToolBar jdbNavToolBar1 = new JdbNavToolBar();
  JdbStatusLabel jdbStatusLabel1 = new JdbStatusLabel();
  JButton btnClose = new JButton();
  ResourceBundle res = Res.getBundle("com.borland.samples.orderentry.Res");
  JdbLabel imageLabel1 = new JdbLabel();
  JPanel jPanel1 = new JPanel();
  BorderLayout borderLayout2 = new BorderLayout();
  GridBagLayout gridBagLayout2 = new GridBagLayout();

  /**
   * The constructor is public, but you should use the getProductFrame
   * method to get an instance of this class.
   */
  public ProductFrame() {
    try {
      initData(); // initialize dataset and data-aware components.
      jbInit();   // initialize frame's controls (JBuilder designer)
    }
    catch (Exception e) {
      e.printStackTrace();
    };
  }

  /**
   * Method generated and maintained by JBuilder designer to initialize control
   * properties.
   *
   * @throws Exception exception
   */
  void jbInit() throws Exception{
    contentPane = (JPanel) this.getContentPane();
    this.setSize(new Dimension(400, 350));
    this.setTitle(res.getString("PF_Products"));
    this.addWindowListener(new java.awt.event.WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        this_windowClosing(e);
      }
    });
    chkIsActive.setColumnName("ISACTIVE");
    chkIsActive.setDataSet(dm.getProductDataSet());
    chkIsActive.setText(res.getString("PF_Active"));
    txtProductID.setColumnName("ID");
    txtProductID.setDataSet(dm.getProductDataSet());
    txtProductID.setEditable(false);
    txtName.setColumnName("NAME");
    txtName.setDataSet(dm.getProductDataSet());
    cboCategory.setColumnName("CATEGORY");
    cboCategory.setDataSet(dm.getProductDataSet());
    txtBasePrice.setColumnName("BASEPRICE");
    txtBasePrice.setDataSet(dm.getProductDataSet());
    txtDiscountPct.setColumnName("DISCOUNTPCT");
    txtDiscountPct.setDataSet(dm.getProductDataSet());
    txtStockQty.setColumnName("STOCKQTY");
    txtStockQty.setDataSet(dm.getProductDataSet());
    txtMinReorderQty.setColumnName("MINREORDERQTY");
    txtMinReorderQty.setDataSet(dm.getProductDataSet());
    pnlToolbar.setLayout(gridBagLayout2);
    pnlProductInfo.setLayout(gridBagLayout1);
    lblName.setText(res.getString("PF_Name"));
    lblCategory.setText(res.getString("PF_Category"));
    lblBasePrice.setText(res.getString("PF_Base_Price"));
    lblDiscountPct.setText(res.getString("PF_Discount_%"));
    lblStockQty.setText(res.getString("PF_Stock_Qty"));
    lblMinReorderQty.setText(res.getString("PF_Min_Reorder_Qty"));
    lblProductID.setText(res.getString("PF_Product_ID"));
    jdbNavToolBar1.setDataSet(dm.getProductDataSet());
    jdbNavToolBar1.setButtonStateDitto(JdbNavToolBar.HIDDEN);
    jdbNavToolBar1.setButtonStateSave(JdbNavToolBar.HIDDEN);
    jdbNavToolBar1.setButtonStateRefresh(JdbNavToolBar.HIDDEN);
    jdbNavToolBar1.setFloatable(false);
    jdbStatusLabel1.setOpaque(true);
    jdbStatusLabel1.setDataSet(dm.getProductDataSet());
    btnClose.setText(res.getString("PF_Close"));
    btnClose.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        btnClose_actionPerformed(e);
      }
    });
    imageLabel1.setHorizontalAlignment(SwingConstants.CENTER);
    imageLabel1.setIcon(new ImageIcon(this.getClass().getResource("images/productBanner.jpg")));
    imageLabel1.setBounds(new Rectangle(2, 2, 313, 40));
    contentPane.setLayout(borderLayout1);
    contentPane.add(jPanel1, BorderLayout.NORTH);
    jPanel1.setLayout(borderLayout2);
    pnlToolbar.add(jdbNavToolBar1, new GridBagConstraints(0, 0, 4, 1, 1.0, 0.0
            ,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
    pnlToolbar.add(btnClose, new GridBagConstraints(4, 0, 1, 1, 0.0, 0.0
            ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 5, 0, 5), 0, 0));
    jPanel1.add(imageLabel1, BorderLayout.CENTER);
    jPanel1.add(pnlToolbar, BorderLayout.SOUTH);
    contentPane.add(pnlProductInfo, BorderLayout.CENTER);
    pnlProductInfo.add(txtProductID, new GridBagConstraints(4, 0, 1, 1, 1.0, 0.0
            ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(7, 0, 0, 0), 0, 0));
    pnlProductInfo.add(txtName, new GridBagConstraints(4, 1, 2, 1, 1.0, 0.0
            ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(10, 0, 0, 15), 0, 0));
    pnlProductInfo.add(cboCategory, new GridBagConstraints(4, 2, 2, 1, 1.0, 0.0
            ,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 15), 0, 0));
    pnlProductInfo.add(txtBasePrice, new GridBagConstraints(4, 3, 1, 1, 1.0, 0.0
            ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
    pnlProductInfo.add(txtDiscountPct, new GridBagConstraints(4, 4, 1, 1, 1.0, 0.0
            ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
    pnlProductInfo.add(txtStockQty, new GridBagConstraints(4, 5, 1, 1, 1.0, 0.0
            ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
    pnlProductInfo.add(txtMinReorderQty, new GridBagConstraints(4, 6, 1, 1, 1.0, 0.0
            ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 7, 0), 0, 0));
    pnlProductInfo.add(lblName, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0
            ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(6, 15, 0, 0), 0, 0));
    pnlProductInfo.add(lblCategory, new GridBagConstraints(0, 2, 2, 1, 0.0, 0.0
            ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 15, 0, 0), 0, 0));
    pnlProductInfo.add(lblBasePrice, new GridBagConstraints(0, 3, 3, 1, 0.0, 0.0
            ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 15, 0, 0), 0, 0));
    pnlProductInfo.add(lblDiscountPct, new GridBagConstraints(0, 4, 3, 1, 0.0, 0.0
            ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 15, 0, 0), 0, 0));
    pnlProductInfo.add(lblStockQty, new GridBagConstraints(0, 5, 2, 1, 0.0, 0.0
            ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 15, 0, 0), 0, 0));
    pnlProductInfo.add(lblMinReorderQty, new GridBagConstraints(0, 6, 4, 1, 0.0, 0.0
            ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 15, 7, 0), 0, 0));
    pnlProductInfo.add(chkIsActive, new GridBagConstraints(5, 6, 1, 1, 0.0, 0.0
            ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 110, 7, 15), 0, 0));
    pnlProductInfo.add(lblProductID, new GridBagConstraints(0, 0, 3, 1, 0.0, 0.0
            ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(7, 15, 0, 0), 0, 0));
    contentPane.add(jdbStatusLabel1, BorderLayout.SOUTH);
  }

  /**
   * We created this method to separate code to initialize database and
   * data-aware controls, which otherwise would go in the jbInit() method. Note
   * that by doing so, the properties set here are not visible to the property
   * inspector. And setting any properties via the inspector will add code to
   * jbInit() instead of here.
   *
   * @throws Exception exception
   */
  private void initData() throws Exception {
    qdsProduct = dm.getProductDataSet();
    qdsCategory = dm.getCategoryDataSet();

    qdsProduct.open();
    qdsCategory.open();
  }

  /**
   * Class method to access the singleton instance of the class.
   *
   * @return ProductFrame
   */
  public static ProductFrame getProductFrame() {
    if (myProductFrame == null)
      myProductFrame = new ProductFrame();

    return myProductFrame;
  }

  /**
   * Before closing the window, check for any pending changes, and prompt user
   * if there are changes pending. Note that we need to both check if there is
   * anything to Post, and to check if there is anything to resolve.
   *
   * @param e WindowEvent

   */
  void this_windowClosing(WindowEvent e) {
    try {
      int status = qdsCategory.getStatus();
      if ( (qdsProduct.isEditing() || qdsProduct.isEditingNewRow()) ||
           ((status & (RowStatus.UPDATED|RowStatus.INSERTED|RowStatus.DELETED)) != 0)
         ) {
        int answer = JOptionPane.showConfirmDialog(this,
                                                   res.getString("PF_Save_pending_changes?"),
                                                   res.getString("PF_Save_Pending_Changes"),
                                                   JOptionPane.YES_NO_CANCEL_OPTION,
                                                   JOptionPane.QUESTION_MESSAGE);
        // Post changes and resolve them back to DB
        if (answer == JOptionPane.YES_OPTION) {
          qdsProduct.post();
          // if there are pending changes to be resolved,
          // save the changes via the Database.saveChanges()
          // method. Then refresh the dataset.
          status = qdsProduct.getStatus();
          if ( (status & (RowStatus.UPDATED|RowStatus.INSERTED|RowStatus.DELETED)) != 0) {
            qdsProduct.getDatabase().saveChanges(new DataSet[] {qdsProduct}, true);
          }
        }
        // If user chooses no, then cancel any inserts or edits
        // and refresh the dataset.
        else if (answer == JOptionPane.NO_OPTION) {
          qdsProduct.cancel();
        }
        // If user cancels, just return
        else //if (answer == JOptionPane.CANCEL_OPTION)
          return;
      }

      // Refresh the dataset to ensure that we have updated data
      qdsProduct.refresh();
      setVisible(false);
      dispose();

    }
    catch (Exception ex) {
      ex.printStackTrace();
      new DBExceptionDialog(this, res.getString("PF_Error"), ex, true).show();
    }
  }

  /**
   * Method to insert a new product order record.
   * Useful for inserting a new product from other forms in
   * the application.
   */
  public void newProduct() {
    try {
      // Open the dataset if not already opened.
      qdsProduct.open();
      // Insert a new Product record at the end of the
      // dataset
      qdsProduct.insertRow(false);
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }

  /**
   * Method to insert a new product record
   * and setting the category that is passed as
   * the parameter category.
   *
   * @param category String

   */
  public void newProduct(String category) {
    try {
      // Open the dataset if not already opened.
      qdsProduct.open();
      // Insert a new Product record at the end of the
      // dataset
      qdsProduct.insertRow(false);
      qdsProduct.setString("CATEGORY", category);
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }

  /**
   * When Close button is clicked, call this frame's windowClosing event handler
   * to properly handle all dataset pending updates.
   *
   * @param e ActionEvent

   */
  void btnClose_actionPerformed(ActionEvent e) {
    // close this Window
    this.this_windowClosing(new WindowEvent(this, WindowEvent.WINDOW_CLOSING));
  }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -