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

📄 selectdialog.java

📁 是适合于使用的管理系统 可以在现实中使用
💻 JAVA
字号:
package mysiloer;

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.border.*;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.SQLException;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.Vector;

/**
 * <p>Title: MySiloer</p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2005</p>
 * <p>Company: yskey</p>
 * @author 杨涛
 * @version 1.0
 */

public class SelectDialog extends JDialog {

  private static final int FINDALL = 0;
  private static final int BYID = 1;
  private static final int BYNAME = 2;
  private static final int BYDATE = 3;
  private static final int BYQUANTITY = 4;
  private static final int BYPRICE = 5;

  private int findBy = this.FINDALL;
  private String sql = "";

  private JPanel radioPanel = new JPanel();
  private Toolkit kit = Toolkit.getDefaultToolkit();
  private SiloPanel parentPanel = null;
  private GridLayout gridLayout1 = new GridLayout();
  private ButtonGroup buttonGroup1 = new ButtonGroup();
  private ButtonGroup buttonGroup2 = new ButtonGroup();
  private JRadioButton findAll = new JRadioButton();
  private JRadioButton jRadioButton2 = new JRadioButton();
  private JRadioButton jRadioButton3 = new JRadioButton();
  private JRadioButton findByPrice = new JRadioButton();
  private JRadioButton findByQuantity = new JRadioButton();
  private JRadioButton findByDate = new JRadioButton();
  private JRadioButton findByName = new JRadioButton();
  private JRadioButton findById = new JRadioButton();
  private JRadioButton equalRadio = new JRadioButton("等于");
  private JRadioButton biggerRadio = new JRadioButton("大于");
  private JRadioButton smallerRadio = new JRadioButton("小于");
  private BorderLayout borderLayout1 = new BorderLayout();
  private JPanel mainPanel = new JPanel();
  private BorderLayout borderLayout2 = new BorderLayout();
  private JPanel buttonPanel = new JPanel();
  private JButton cancelButton = new JButton();
  private JButton okButton = new JButton();
  private JPanel contentPanel = new JPanel();
  private JTextField param1 = new JTextField("", 10);
  private JTextField param2 = new JTextField("", 10);
  private FlowLayout flowLayout1 = new FlowLayout();
  private Border border1;
  private FlowLayout flowLayout2 = new FlowLayout();
  private JLabel jLabel1 = new JLabel();
  private Connection con;
//  private DataBase dataBase;

  public SelectDialog(JFrame frame, String title, boolean modal) {
    super(frame, title, modal);
    try {
      jbInit();
      this.setResizable(false);
      pack();
      Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
      Dimension frameSize = getSize();
      if (frameSize.height > screenSize.height) {
        frameSize.height = screenSize.height;
      }
      if (frameSize.width > screenSize.width) {
        frameSize.width = screenSize.width;
      }
      setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2 - 150);
//    setVisible(true);
    }
    catch(Exception ex) {
      ex.printStackTrace();
    }
  }

//  public SelectDialog(JPanel panel, DataBase dataBase) {
  public SelectDialog(JFrame frame, SiloPanel panel, boolean model) {
    this(frame, "原料库查询对话框", model);
    parentPanel = panel;
//    this.dataBase = dataBase;
    this.con = panel.dataBase.connection;
  }
  private void jbInit() throws Exception {
    border1 = BorderFactory.createEmptyBorder(5,10,5,5);
    radioPanel.setLayout(gridLayout1);
    gridLayout1.setHgap(3);
    gridLayout1.setColumns(2);
    gridLayout1.setRows(3);
    param1.addActionListener(new java.awt.event.ActionListener() {
       public void actionPerformed(ActionEvent e) {
         okButton_actionPerformed(e);
       }
    });
    param2.addActionListener(new java.awt.event.ActionListener() {
       public void actionPerformed(ActionEvent e) {
         okButton_actionPerformed(e);
       }
    });
    findAll.setSelected(true);
    findAll.setText("查询全部");
    findAll.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        findAll_actionPerformed(e);
      }
    });
    jRadioButton2.setText("jRadioButton2");
    jRadioButton3.setText("jRadioButton3");
    findByPrice.setText("按价格查询");
    findByPrice.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        findByPrice_actionPerformed(e);
      }
    });
    findByQuantity.setText("按数量查询");
    findByQuantity.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        findByQuantity_actionPerformed(e);
      }
    });
    findByDate.setText("按入库时间查询");
    findByDate.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        findByDate_actionPerformed(e);
      }
    });
    findByName.setText("按名称查询");
    findByName.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        findByName_actionPerformed(e);
      }
    });
    findById.setText("按序号查询");
    findById.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        findById_actionPerformed(e);
      }
    });
    this.getContentPane().setLayout(borderLayout1);
    mainPanel.setLayout(borderLayout2);
//    getContentPane().add(radioPanel, BorderLayout.CENTER);
    cancelButton.setText("取   消");
    cancelButton.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        cancelButton_actionPerformed(e);
      }
    });
    okButton.setText("查   询");
    okButton.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        okButton_actionPerformed(e);
      }
    });
    contentPanel.setLayout(flowLayout1);
    radioPanel.setBorder(border1);
    buttonPanel.setLayout(flowLayout2);
    jLabel1.setText("          ");
    radioPanel.add(findAll, null);
    radioPanel.add(findById, null);
    radioPanel.add(findByName, null);
    radioPanel.add(findByQuantity, null);
    radioPanel.add(findByPrice, null);
    radioPanel.add(findByDate, null);
    //    radioPanel.add(jRadioButton3, null);
//    radioPanel.add(jRadioButton2, null);
    buttonGroup1.add(findAll);
    buttonGroup1.add(findByName);
    buttonGroup1.add(findByQuantity);
//    buttonGroup1.add(jRadioButton3);
    buttonGroup1.add(findById);
    buttonGroup1.add(findByDate);
    buttonGroup1.add(findByPrice);
//    buttonGroup1.add(jRadioButton2);
    this.getContentPane().add(mainPanel, BorderLayout.CENTER);
    mainPanel.add(radioPanel, BorderLayout.NORTH);
    buttonPanel.add(okButton, null);
    buttonPanel.add(jLabel1, null);
    buttonPanel.add(cancelButton, null);
    mainPanel.add(buttonPanel, BorderLayout.SOUTH);
    mainPanel.add(contentPanel,  BorderLayout.CENTER);
  }

  void findByName_actionPerformed(ActionEvent e) {
    setFindBy(this.BYNAME);
    contentPanel.removeAll();
    contentPanel.add(new JLabel("请输入产品名称:"));
    contentPanel.add(param1);
    clearFilds();
    pack();
    validate();
    repaint();
  }

  void findAll_actionPerformed(ActionEvent e) {
    setFindBy(this.FINDALL);
    contentPanel.removeAll();
    clearFilds();
    pack();
    validate();
    repaint();
  }

  void findById_actionPerformed(ActionEvent e) {
    setFindBy(this.BYID);
    contentPanel.removeAll();
    contentPanel.add(new JLabel("请输入查询序号:"));
    contentPanel.add(param1);
    clearFilds();
    pack();
    validate();
    repaint();
  }

  void findByDate_actionPerformed(ActionEvent e) {
    setFindBy(this.BYDATE);
    contentPanel.removeAll();
    contentPanel.add(new JLabel("请输入起始时间:"));
    contentPanel.add(param1);
    contentPanel.add(new JLabel("请输入中止时间时间:"));
    contentPanel.add(param2);
    clearFilds();
    pack();
    validate();
    repaint();
  }

  void findByQuantity_actionPerformed(ActionEvent e) {
    setFindBy(this.BYQUANTITY);
    contentPanel.removeAll();
    contentPanel.add(new JLabel("请输入产品数量:"));
    contentPanel.add(param1);
//    biggerRadio.setSelected(true);
    equalRadio.setSelected(true);
    contentPanel.add(equalRadio);
    contentPanel.add(biggerRadio);
    contentPanel.add(smallerRadio);
    buttonGroup2.add(equalRadio);
    buttonGroup2.add(biggerRadio);
    buttonGroup2.add(smallerRadio);
    clearFilds();
    pack();
    validate();
    repaint();
  }

  void findByPrice_actionPerformed(ActionEvent e) {
    setFindBy(this.BYPRICE);
    contentPanel.removeAll();
    contentPanel.add(new JLabel("请输入产品价格:"));
    contentPanel.add(param1);
//    biggerRadio.setSelected(true);
    equalRadio.setSelected(true);
    contentPanel.add(equalRadio);
    contentPanel.add(biggerRadio);
    contentPanel.add(smallerRadio);
    buttonGroup2.add(equalRadio);
    buttonGroup2.add(biggerRadio);
    buttonGroup2.add(smallerRadio);
    clearFilds();
    pack();
    validate();
    repaint();
  }

  void cancelButton_actionPerformed(ActionEvent e) {
    this.setVisible(false);
    this.dispose();
  }
  private void setFindBy(int findBy){
    this.findBy = findBy;
  }

  public void okButton_actionPerformed(ActionEvent e) {
    switch(findBy){
      case FINDALL: sql = "select * from "+Type.SILOTABLE;
        break;
      case BYID: sql = "select * from "+Type.SILOTABLE+" where 序号="+ param1.getText();
        break;
      case BYNAME: sql = "select * from "+Type.SILOTABLE+" where 原料名 like '"+param1.getText()+"'";
        break;
      case BYDATE: sql = "select * from "+Type.SILOTABLE+" where 入库时间 >'"+param1.getText()+"' and 入库时间<'"+param2.getText()+"'";
        break;
      case BYQUANTITY:
        String quality = param1.getText();
        int pointIndex1 = quality.indexOf(".");
        if(pointIndex1 < 0){
          quality += ".0";
        }
        if(biggerRadio.isSelected()){
          sql = "select * from "+Type.SILOTABLE+" where 数量>"+quality;
        }
        else if(smallerRadio.isSelected()){
          sql = "select * from "+Type.SILOTABLE+" where 数量<"+quality;
        }
        else {
          sql = "select * from "+Type.SILOTABLE+" where 数量="+quality;
        }
        break;
      case BYPRICE:
        String price = param1.getText();
        int pointIndex = price.indexOf(".");
        if(pointIndex < 0){
          price += ".0";
        }
        if(biggerRadio.isSelected()){
          sql = "select * from "+Type.SILOTABLE+" where 单价>"+price;
        }
        else if(smallerRadio.isSelected()) {
          sql = "select * from "+Type.SILOTABLE+" where 单价<"+price;
        }
        else {
          sql = "select * from "+Type.SILOTABLE+" where 单价="+price;
        }
        break;
    }
    try{
      System.out.println(sql);
      Statement sta = con.createStatement();
      ResultSet set = sta.executeQuery(sql);
      ResultSetMetaData meta = set.getMetaData();
      int colCount = meta.getColumnCount();
//    parentPanel.tablePanel.tbModel = new DefaultTableModel();
      int rowCount = parentPanel.tablePanel.tbModel.getRowCount();
//      System.out.print(rowCount);
      for(int i = 0; i < rowCount; i++){
        parentPanel.tablePanel.tbModel.removeRow(0);
      }
      Vector colNames = new Vector();
      Vector row;
      for(int i = 1; i <= colCount; i++){
        colNames.add(meta.getColumnLabel(i));
      }
      parentPanel.tablePanel.tbModel.setColumnIdentifiers(colNames);
      while(set.next()){
        row = new Vector();
        for(int i = 1; i <= colCount; i++){
          String temp = set.getString(i);

//          if(i == 3 || i == 4){//单价和数量取两位小数
          if(i == 4){
            temp = temp.substring(0, temp.length() -2);
          }

          if(temp.indexOf(" ") >= 0){
            temp = temp.substring(0, temp.indexOf(" "));
          }
          row.add(temp);
//          System.out.print(set.getString(i)+"   ");
        }
//        System.out.println();
        parentPanel.tablePanel.tbModel.addRow(row);
      }//end while
      if(parentPanel.tablePanel.tbModel.getRowCount() <=0){
        kit.beep();
        JOptionPane.showMessageDialog(this, "对不起,没有符合查找条件的记录,请修改数据重新查找!!");
      }
    }
    catch(SQLException ex){
      System.err.println(e.toString());
      kit.beep();
      JOptionPane.showMessageDialog(this, "查询错误,请检查输入是否正确!!");
    }
    catch(Exception ex2){
      System.err.println(ex2.toString());
      kit.beep();
      JOptionPane.showMessageDialog(this, "查询错误!!!!");
    }
    validate();
    repaint();
//    System.out.println(sql);
  }
  private Vector getColNames(Connection con){
    Vector colNames = new Vector();
//    Vector colClasses = new Vector();
    try{
      Statement sta = con.createStatement();
      ResultSet set = sta.executeQuery("select * from "+Type.SILOTABLE);
      ResultSetMetaData metaData = set.getMetaData();
      int colCount = metaData.getColumnCount();
      for(int i = 0; i<colCount; i++){
        colNames.add(metaData.getColumnLabel(i+1));
//        colClasses.add(metaData.getColumn());
      }
    }
    catch(Exception e){
      System.err.print(e.toString());
    }
    return colNames;
  }
  private void clearFilds(){
    param1.setText("");
    param2.setText("");
  }
}

⌨️ 快捷键说明

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